我在GridView里面添加了一个模板列 里面是一个按钮  
按钮的单击事件 protected void Button1_Click(object sender, EventArgs e)
从参数e里面无法获取 我单击的是那一行的按钮 
因为不是GridViewCommandEventArgs e
那么我怎么获取单击的是那一行的序号啊 ???

解决方案 »

  1.   

    另外 为什么我设置好了SqlDataSource1的UpdataQuery属性 
    不能用  调用SqlDataSource1.Update()方法更新数据  
    但是在Gridview 里面自带的编辑,更新 可以更新数据啊  ??
      

  2.   

    Gridview 里面自带的编辑,更新 可以更新数据啊
      

  3.   

    事件上传了,用rowcommand
    ---------------------------------------------------------------scow(怡红快绿)帅哥:
    大概知道你的意思了 你能给段代码让我理解下吗?
    我不知道怎么实现啊 
      

  4.   

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (sender is Button)
            {
                Button btn = sender as Button;
                ........
            }
        }
      

  5.   

    <%@ Page language="C#" %><script runat="server">  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if(e.CommandName=="Add")
        {
          // Convert the row index stored in the CommandArgument
          // property to an Integer.
          int index = Convert.ToInt32(e.CommandArgument);
                
          // Retrieve the row that contains the button clicked 
          // by the user from the Rows collection.
          GridViewRow row = CustomersGridView.Rows[index];
                
          // Create a new ListItem object for the customer in the row.     
          ListItem item = new ListItem();
          item.Text = Server.HtmlDecode(row.Cells[2].Text);
                
          // If the customer is not already in the ListBox, add the ListItem 
          // object to the Items collection of the ListBox control. 
          if (!CustomersListBox.Items.Contains(item))
          {
            CustomersListBox.Items.Add(item);
          }           
        }
      }  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
      {
        
        // The GridViewCommandEventArgs class does not contain a 
        // property that indicates which row's command button was
        // clicked. To identify which row's button was clicked, use 
        // the button's CommandArgument property by setting it to the 
        // row's index.
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Retrieve the LinkButton control from the first column.
          LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
              
          // Set the LinkButton's CommandArgument property with the
          // row's index.
          addButton.CommandArgument = e.Row.RowIndex.ToString();
        }  }
        
    </script><html>
      <body>
        <form runat="server">
            
          <h3>GridView RowCommand Example</h3>
                
          <table width="100%">         
            <tr>                
              <td width="50%">
                        
                <asp:gridview id="CustomersGridView" 
                  datasourceid="CustomersSource"
                  allowpaging="true" 
                  autogeneratecolumns="false"
                  onrowcommand="CustomersGridView_RowCommand"
                  onrowcreated="CustomersGridView_RowCreated"  
                  runat="server">
                    
                  <columns>
                    <asp:buttonfield buttontype="Link" 
                      commandname="Add" 
                      text="Add"/>
                    <asp:boundfield datafield="CustomerID" 
                      headertext="Customer ID"/>
                    <asp:boundfield datafield="CompanyName" 
                      headertext="Company Name"/> 
                    <asp:boundfield datafield="City" 
                      headertext="City"/>         
                  </columns>
                    
                </asp:gridview>
                        
              </td>
                        
              <td valign="top" width="50%">
                        
                Customers: <br/>
                <asp:listbox id="CustomersListBox"
                  runat="server"/> 
                        
              </td>  
            </tr>      
          </table>
                
          <!-- This example uses Microsoft SQL Server and connects  -->
          <!-- to the Northwind sample database. Use an ASP.NET     -->
          <!-- expression to retrieve the connection string value   -->
          <!-- from the Web.config file.                            -->
          <asp:sqldatasource id="CustomersSource"
            selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
            connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
            runat="server"/>
                
        </form>
      </body>
    </html>MSDN上的帮助..
      

  6.   

    Button在模版列里面的话,将CustomersGridView_RowCreated里面的
    LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
    换为
    LinkButton xxButton = (LinkButton )e.Row.Cells[0].FindControl("LinkButton_XXNR")
      

  7.   

    非常感谢 superman_yc(心有灵犀) 兄弟
    及回帖的帅哥门