请问,怎样给datagrid模板列里面的button编事件,我有两在两个模板列里面分别放了两个按钮,要分别给这两个按钮编写事件,而且,事件里面需要用到datagrid的关键字即(datagrid.datakeys(e.item.itemIndex)

解决方案 »

  1.   

    没有学到那一步,不过都是可以通过 itemcommand 事件来做啊根据 button 得 commandname和arguments就知道是点击的哪一个也挺方便的。
      

  2.   

    button 有CommandName 的属性。没有就用LinkButton 一定有。
    在DataGird 的itemcommand 事件中判断 CommandName 的值来决定调用哪个方法。
      

  3.   

    <ItemTemplate>
    <asp:Button id="btn1" runat="server" Text="Button" CommandName="myCommand1"></asp:Button>
    <asp:Button id="btn2" runat="server" Text="Button" CommandName="myCommand1"></asp:Button>
    </ItemTemplate>
    ******************************
     private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                if(e.CommandName == "myCommand1")
                {
                    string id = DataGrid1.DataKeys[e.Item.DataSetIndex];
                    //........
                }
                else if(e.CommandName == "myCommand2")
                {
                    //.....
                }
            }
      

  4.   

    this.button.Click += new System.EventHandler(this.button_Click);
      

  5.   

    有一点笔误,更正:
    <ItemTemplate>
    <asp:Button id="btn1" runat="server" Text="ButtonAAA" CommandName="myCommand1"></asp:Button>
    <asp:Button id="btn2" runat="server" Text="ButtonBBB" CommandName="myCommand2"></asp:Button>
    </ItemTemplate>
    ******************************
     private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                if(e.CommandName == "myCommand1")
                {
                    string id = DataGrid1.DataKeys[e.Item.DataSetIndex];
                    //........
                }
                else if(e.CommandName == "myCommand2")
                {
                    //.....
                }
            }
      

  6.   

    当你在datagridview中嵌入button控件时,给它定义一个ButtonName.
    然后在
    (DataGridView中的有一事件:OnItemCommand="DataGridView_ItemCommand")
    Private void DataGridView_ItemCommand(Object Sender,Eventargs e)
    {
    switch e.CommandName
    case ButtonName1:
    //你的处理程序
    break;
    case ButtonName2:
    //你的处理程序
    }
      

  7.   

    fondcontrol找到那个控件,再对控件添加事件
      

  8.   

    直接用按纽列,然后编写datagrid的itemcommand事件的代码