Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从gridview访问文本框文本到按钮Onclick事件_C#_Asp.net_Button_Gridview - Fatal编程技术网

C# 如何从gridview访问文本框文本到按钮Onclick事件

C# 如何从gridview访问文本框文本到按钮Onclick事件,c#,asp.net,button,gridview,C#,Asp.net,Button,Gridview,我在gridview中有文本框和按钮。我想要的是,当我点击这些按钮时,textbox中相应的值会更新到数据库中,所以我需要在OnClick事件中访问textbox文本。我该怎么做 Gridview: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderS

我在gridview中有文本框和按钮。我想要的是,当我点击这些按钮时,textbox中相应的值会更新到数据库中,所以我需要在OnClick事件中访问textbox文本。我该怎么做

Gridview:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="#CCCCCC" BorderColor="#999999" 
            BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" 
            ForeColor="Black" OnRowDataBound="GridView1_RowDataBound" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField HeaderText="Save 1">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" Width="30px" runat="server"></asp:TextBox>
                        <asp:Button ID="btnSave1" runat="server" Text="Save"  OnClick="btnSave1_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Save 2">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" Width="30px" runat="server"></asp:TextBox>
                        <asp:Button ID="btnSave2" runat="server" Text="Save"  OnClick="btnSave2_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</asp:GridView>

请帮我解决这个问题。

基本上,这是一个如何获取文本框值的示例:

string CustomerID = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCustomerID")).Text;

基本上,这是一个如何获取文本框值的示例:

string CustomerID = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCustomerID")).Text;
您可以这样做:

protected void btnSave1_Click(object sender, EventArgs e)
{
     GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
     TextBox TextBox1 = row.FindControl("TextBox1") as TextBox; 

      //Access TextBox1 here.
      string myString = TextBox1.Text;
}
您可以这样做:

protected void btnSave1_Click(object sender, EventArgs e)
{
     GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
     TextBox TextBox1 = row.FindControl("TextBox1") as TextBox; 

      //Access TextBox1 here.
      string myString = TextBox1.Text;
}

请看看这个网站,他们有一个非常相似的样本,你试图实现。请看看这个网站,他们有一个非常相似的样本,你试图实现。但由于我的按钮单击事件在gridview之外,因此e.RowIndex将不起作用。单击事件触发更新命令。我提供给你的网站会给你详细信息。这非常接近你想要的网站示例,他们正在使用FooterTemplate,你能告诉我应该在哪里使用它吗?虽然它正在工作,但我必须为此创建我不想要的footer。您还有其他方法吗?请检查DeleteCustomer函数和Gridview模板。他传递了一个命令参数,该参数的值是他想要删除的?从您的示例中,我看不到任何数据源。如果您不需要显示数据库中的数据,而只保存新记录,则可以使用简单的HTML5表格,但由于我的按钮单击事件在gridview之外,因此e.RowIndex将不起作用。单击事件触发更新命令。我提供给你的网站会给你详细信息。这非常接近你想要的网站示例,他们正在使用FooterTemplate,你能告诉我应该在哪里使用它吗?虽然它正在工作,但我必须为此创建我不想要的footer。您还有其他方法吗?请检查DeleteCustomer函数和Gridview模板。他传递了一个命令参数,该参数的值是他想要删除的?从您的示例中,我看不到任何数据源。如果您不需要从数据库中显示数据,而只保存新记录,则可以使用简单的HTML5表格,尽管它正在工作,但文本框值为空。为什么?虽然它正在工作,但文本框值为空。为什么?