C# 单击按钮访问GridView的数据

C# 单击按钮访问GridView的数据,c#,asp.net,mysql,visual-studio-2010,C#,Asp.net,Mysql,Visual Studio 2010,我有一个包含一些列的gridview和一个包含按钮的template field列,我想在单击按钮时调用一个过程,但是我想将一个列的值传递给该过程,但是我得到了一个错误,下面是按钮的操作侦听器:(gridview中的列名是team_ID) 错误:诸如Eval()、XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用。 错误行:int team_ID=Convert.ToInt32(Eval(“team_ID”) 首先,要处理在TemplateField中单击的按钮,您需

我有一个包含一些列的gridview和一个包含按钮的template field列,我想在单击按钮时调用一个过程,但是我想将一个列的值传递给该过程,但是我得到了一个错误,下面是按钮的操作侦听器:(gridview中的列名是team_ID) 错误:诸如Eval()、XPath()和Bind()之类的数据绑定方法只能在数据绑定控件的上下文中使用。 错误行:int team_ID=Convert.ToInt32(Eval(“team_ID”)


首先,要处理在TemplateField中单击的按钮,您需要订阅
RowCommand
方法:

<asp:GridView runat="server" ID="gv" OnRowCommand="yourMethod">
代码隐藏

protected void yourMethod(object sender, GridViewCommandEventArgs e) {
    if (e.CommandName == "yourButtonName") {

        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

        TextBox someTextBox = row.FindControl("tb") as TextBox;
        string textValue = someTextBox.Text;
    }
}
<asp:TemplateField>
      <ItemTemplate>
            <asp:Button CommandName="yourButtonName" runat="server" />
protected void yourMethod(object sender, GridViewCommandEventArgs e) {
    if (e.CommandName == "yourButtonName") {

        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

        TextBox someTextBox = row.FindControl("tb") as TextBox;
        string textValue = someTextBox.Text;
    }
}