【问题标题】:Updating Textbox in a templateField with button使用按钮更新模板字段中的文本框
【发布时间】:2013-12-22 04:30:45
【问题描述】:

这是我第一次在 stackoverflow 中提问,所以请多多包涵。我正在使用 asp.net 创建一个网页,我将 GridView 的列设置为数据绑定,除了 1 个模板字段,模板字段内是一个文本框和一个按钮。我想使用外部按钮(同一页面中的按钮,但在 gridview 之外)更新文本框的值,任何人都可以显示/告诉我该怎么做吗?

GridView1是gridView的ID,

这是模板字段:

<asp:TemplateField HeaderText="Template Field1">
    <ItemTemplate>
         <asp:TextBox runat="server" ID="textboxs">
         </asp:TextBox>
         <asp:Button runat="server" ID="buttons" Text="S"/>
     </ItemTemplate>
     <HeaderStyle Width="15px" />
     <ItemStyle Wrap="False" />
</asp:TemplateField>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Button" />

这是背后的代码:

protected void Button1_Click1(object sender, EventArgs e)
{
    GridViewCode.Rows[0].Cells[10].Text = "Some Text";
}

我认为问题在于模板字段有一个文本框和一个按钮,如果我错了,请纠正我。

顺便说一句,我无法删除模板字段中的按钮,它必须是带有按钮的文本框。谢谢。

【问题讨论】:

  • 欢迎来到 SO。您需要发布您拥有的代码以及您尝试过的内容吗?
  • 谢谢 Seany84,我更新了我的帖子。

标签: c# asp.net gridview templatefield


【解决方案1】:
You can use a code like this:

    for (int i = 0; i < GridView.Rows.Count; i++)
    {
        GridViewRow gvr = GridView.Rows[i];
        Textbox txtBox= (Textbox)gvr.Cells[0].FindControl("textbox_id");
        txtBox.Text = "Your new text here";
    }

这将替换每一行的文本框,您可以在特定行中设置替换条件。

【讨论】:

    【解决方案2】:

    无需您提供更多代码,类似于以下内容:

    protected void Button1_Click1(object sender, EventArgs e)
    {
       TextBox textboxs = GridView.Rows[2].FindControl("textboxs");
       textboxs.Text = "New Text Here";
    }
    

    其中 index 2 是您的 gridview 数据绑定数据的行索引

    【讨论】:

      【解决方案3】:
      protected void Button1_Click1(object sender, EventArgs e)
      {
          // First bind you grid again over here other wise it will lost data
      
          GridView1.DataSource = you List of Objects or DataTable;
          GridView1.DataBind();
      
          foreach (GridViewRow gvr in GridView1.Rows)
          {
              if (gvr.RowType == DataControlRowType.DataRow)
              {
                  // to fill all text boxes
                  TextBox textboxs = gvr.FindControl("textboxs") as TextBox;
                  textboxs.Text = "You text";
      
      
                  // to fill specific text box
                  // use gvr.DataItem to check aging specific data item using if and then
                  // textboxs.Text = "You text";
              }
          }
      }
      

      【讨论】:

      • 非常感谢Jignesh Suvariya。你的代码对我帮助很大。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      相关资源
      最近更新 更多