处理来自多个网格删除事件与单个事件为处理器asp.net [英] Handle multiple delete events from grid with single events handlers asp.net

查看:212
本文介绍了处理来自多个网格删除事件与单个事件为处理器asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个网格,其中包含删除按钮,我使用RadAjaxManager将火从客户端Ajax请求
一边服务器端OnajaxRequest包含事件处理程序和事件处理程序将调用我删除事件象下面这样:

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

下面的问题是这两个事件都呼吁,但我只是想打电话给个别删除的Remove1和Remove2按钮单击事件。

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

任何人可以帮助我解决这个??

Can anybody help me with this??

推荐答案

您不需要订阅事件在code-背后即不需 this.RemoveEvent + = 。而是写一个名为 RemoveRecord(buttonId,recordId所)一个方法。我已经解释了一步一步地这种做法如下。

You do not need to subscribe to events in your code-behind i.e. no need of this.RemoveEvent+=. Instead write a single method called RemoveRecord(buttonId, recordId). I have explained this approach in a step-by-step manner as below.


  1. 首先更改的OnClientClick 标记。请注意,我们将传递记录的ID,删除以及按钮的服务器端的ID被点击的JavaScript函数 DeleteData

  1. First change the markup for OnClientClick. Note that we will be passing the Id of the record to delete as well as the server-side id of the button being clicked to the JavaScript function DeleteData.

OnClientClick='<%# Eval("Id", "DeleteData('{0}', 'Remove1'); return false;") %>'   

OnClientClick='<%# Eval("Id", "DeleteData('{0}', 'Remove2'); return false;") %>'


  • 更改JavaScript的删除方法。请注意,我们现在通过服务器端的按钮的ID,这样在code-背后,我们可以很容易地知道哪个按钮被点击。

  • Change the JavaScript Delete method. Note that we are now passing the server-side button id, so that in code-behind we can easily know which button was clicked.

    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.
    }
    


  • 在code后面,包括一个 RemoveRecord 法,也没有必要创建事件处理程序,正如你在原来的$ C $这样做的C。所以,你应该从原来的code删除以下方法: Remove1_Click Remove2_Click ,也拆除$ C在的Page_Load $ C,订阅点击事件。

  • In code behind, include a single RemoveRecord method and there is no need of creating event handlers as you have done in your original code. So you should remove the following methods from your original code: Remove1_Click and Remove2_Click and also remove the code in Page_Load that subscribes to click events.

    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 在父页面和删除子页面的方法,然后在他们的标题提到的适当位置添加以下code。的但请注意,在上面的方法中提到的标记变化和JavaScript功能的改变也适用于本办法。

    If you have to have RadAjaxManager2_AjaxRequest in parent page and delete methods in child page, then add the following code at appropriate places as mentioned in their titles. But please note that the markup changes and JavaScript function changes mentioned in above approach also apply to this approach.

    在父页(只是在类外)

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

    这篇关于处理来自多个网格删除事件与单个事件为处理器asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆