我有两个包含删除按钮的网格,我正在使用RadAjaxManager,它将从客户端向服务器端激发ajax请求OnajaxRequest,其中包含事件处理程序,该事件处理程序将调用我的删除事件,如下所示:
<telerik:RadAjaxManager ID="RadAjaxManager2" runat="server" meta:resourcekey="RadAjaxManager1Resource1" OnAjaxRequest="RadAjaxManager2_AjaxRequest">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadAjaxManager2">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="Grid1" />
<telerik:AjaxUpdatedControl ControlID="Grid2" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadGrid ID="Grid1" runat="server">
---
---
<telerik:GridTemplateColumn HeaderText="Action" UniqueName="Action" HeaderStyle-Width="130px">
<ItemTemplate>
<asp:ImageButton runat="server" ID="Remove1" Text="Delete" OnClientClick='<%# Eval("Id", "javascript:return DeleteData(\"{0}\");") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:RadGrid ID="Grid2" runat="server">
---
---
<telerik:GridTemplateColumn HeaderText="Action" UniqueName="Action" >
<ItemTemplate>
<asp:ImageButton runat="server" ID="Remove2" Text="Delete" OnClientClick='<%# Eval("Id", "javascript:return DeleteData(\"{0}\");") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
function DeleteData(Id) {
var ajaxManager = null;
var action = 'Remove';
ajaxManager = $find("ctl00_cphMain_RadAjaxManager2");
var arg = action + "," + Id; //Remove,1(1 indicates id of record to remove from grid)
ajaxManager.ajaxRequest(arg);This line will fire below method.
}
public event EventHandler RemoveEvent;
protected void RadAjaxManager2_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
var argument = (e.Argument);
var stringArray = argument.Split(",".ToCharArray());//[0]="Remove",[1]=1
if (stringArray[0] == "Remove")
{
RemoveEvent(stringArray[1], null);
}
}
在此之后它会调用:
protected void Page_Load(object sender, EventArgs e)
{
this.RemoveEvent += Remove1_Click;
this.RemoveEvent += Remove2_Click;
if (!IsPostBack)
{
}
}
protected void Remove1_Click(object sender, object obj)
{
}
protected void Remove2_Click(object sender, object obj)
{
}
这里的问题是这两个事件都在调用,但我只想在点击Remove1和Remove2按钮时调用单个删除事件。
答案 0 :(得分:1)
你只需使用RadGrid_ItemCommand并使用del按钮传递命令参数并更改命令名称以删除她的
示例代码<telerik:GridTemplateColumn HeaderText="Actions">
<ItemTemplate>
<asp:ImageButton ID="Button3" Style="width: 14px" Height="14px" ImageUrl="~/Images/Erase.png" runat="server" Text="Delete1" OnClientClick="return confirm('Are you sure you want to delete this item?');" CommandName="Delete1" CommandArgument='<%# Eval("Pid") %>' />
   
<asp:ImageButton ID="Button3" Style="width: 14px" Height="14px" ImageUrl="~/Images/Erase.png" runat="server" Text="Delete2" OnClientClick="return confirm('Are you sure you want to delete this item?');" CommandName="Delete2" CommandArgument='<%# Eval("Pid") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Delete1")
{
}
if (e.CommandName == "Delete2")
{
}
}
答案 1 :(得分:1)
您无需订阅代码隐藏中的事件,即无需this.RemoveEvent+=
。而是编写一个名为RemoveRecord(buttonId, recordId)
的方法。我已逐步解释了这种方法,如下所示。
首先更改OnClientClick
的标记。请注意,我们将传递要删除的记录的ID以及被单击到JavaScript函数DeleteData
的按钮的服务器端ID。
OnClientClick='<%# Eval("Id", "DeleteData('{0}', 'Remove1'); return false;") %>'
OnClientClick='<%# Eval("Id", "DeleteData('{0}', 'Remove2'); return false;") %>'
更改JavaScript Delete
方法。请注意,我们现在传递服务器端按钮ID,因此在代码隐藏中我们可以轻松地知道单击了哪个按钮。
function DeleteData(Id, buttonId) {
var ajaxManager = null;
ajaxManager = $find("<%=RadAjaxManager2.ClientID%>");
var arg = buttonId + "," + Id; //Remove1,1 (1 indicates id of record to remove from grid)
ajaxManager.ajaxRequest(arg);//This line will fire below method.
}
在后面的代码中,包含一个RemoveRecord
方法,并且不需要像在原始代码中那样创建事件处理程序。因此,您应该从原始代码中删除以下方法:Remove1_Click
和Remove2_Click
,并删除订阅点击事件的Page_Load
中的代码。
protected void RadAjaxManager2_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
var stringArray = e.Argument.Split(",".ToCharArray());//[0]="Remove1" or "Remove2",[1]=id of record to delete
RemoveRecord( stringArray[0], stringArray[1]);
}
//the method below will delete data
private void RemoveRecord( buttonId, recordId)
{
if(buttonId== "Remove1")
{
//code to delete record with id of recordId
}
else if ( buttonId == "Remove2")
{
//code to delete record with id of recordId
}
}
另一种方法
如果必须在父页面中使用RadAjaxManager2_AjaxRequest
并删除子页面中的方法,请在标题中提到的适当位置添加以下代码。 但请注意,上述方法中提到的标记更改和JavaScript函数更改也适用于此方法。
在父页面(在课堂外)
public class RemoveEventArgs : EventArgs
{
public RemoveEventArgs ( string buttonId, string recordId)
{
this.ButtonId = buttonId;
this.RecordId = recordId;
}
public string ButtonId {get;set;}
public string RecordId {get;set;}
}
此外,在同一个父页面中,将方法RadAjaxManager2_AjaxRequest
更改为下面给出的内容。
在父页面中(更改此现有方法)
protected void RadAjaxManager2_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
var stringArray = e.Argument.Split(",".ToCharArray());//[0]="Remove1",[1]=1 OR [0]="Remove2", [1] = 212
if (stringArray[0] == "Remove1" || stringArray[0] == "Remove2")
{
if(RemoveEvent!=null)
{
RemoveEvent(this, new RemoveEventArgs(stringArray[0], stringArray[1]));
}
}
}
在子页面中(更改现有方法)
protected void Remove1_Click(object sender, RemoveEventArgs obj)
{
if( obj!=null && obj.ButtonId == "Remove1")
{
string recordId = obj.RecordId;
//write your code for deleting
}
}
protected void Remove2_Click(object sender, RemoveEventArgs obj)
{
if( obj!=null && obj.ButtonId == "Remove2")
{
string recordId = obj.RecordId;
//write your code for deleting
}
}