Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

c# - Handle multiple delete events from grid with single events handlers asp.net

I have two grid which contains delete button and I am using RadAjaxManager which will fire ajax request from client side to server side OnajaxRequest which contain event handlers and that event handler will call my delete event like below:

<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);
            }
        }

After this it will call this:

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)
        {
        }

Problem here is both this events are calling but I just want to call individual delete events on click of Remove1 and Remove2 buttons.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you simply just use the RadGrid_ItemCommand and pass the command argument with del button and change the command name for both remove her's a sample code for

<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") %>' />
                                            &nbsp &nbsp
 <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")
    {

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...