c# - 将文本框值传递给 gridview

标签 c# asp.net gridview

我在 aspx 页面中有 gridview 控件和文本框 + 按钮控件。

我想通过单击按钮将文本框值传递给 gridview。

<asp:GridView ID="gvName" runat="server" ViewStateMode="Enabled">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblName" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

并使用这段代码:

protected void btnAddName_Click(object sender, ImageClickEventArgs e)
    {
      foreach (GridViewRow row in gvName.Rows)
        {
            if ((Label)row.FindControl("lblName") is Label)
            {
                ((Label)row.FindControl("lblName")).Text = txtName.Text;
            }
        }
     }

但是不行。 :-( 请帮助我。

最佳答案

Create a rowdatabound command and place the below code. 

protected void gvName_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblName = (Label)row.FindControl("lblName");         
            lblName.Text = txtName.Text;  
        }
}

关于c# - 将文本框值传递给 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14252844/

相关文章:

c# - LINQ 中的查询比 SQL 更复杂

c# - 是否可以为类型参数指定通用约束以从另一种类型转换?

android - 如何在 GridView 的适配器内刷新 imageView

c# - 单击后禁用按钮,直到使用 javascript 发回 ascx 控件

c# - 适当的架构 : Adding Attributes to Domain Model in . NET

c# - Web 表单中的 Treeview 在父节点检查上检查子节点

jquery - 在 ASP.NET MVC 中使用 AJAX 刷新表

asp.net - Bad Request 是哪个系统异常

gridview - 如何在GridView中合并两个单元格

c# - GridView ButtonField 单击是否会导致回发?