使用按钮更新templateField中的文本框

时间:2021-10-04 15:31:28

it's my first time asking a question here in * so please bear with me. I am creating a webpage using asp.net, I have GridView with columns set as databound except 1 template field, within the template field is a textbox and a button. I want to update the value of the textbox using an outside button(a button in the same page, but outside the gridview), can anyone show/tell me how to do that?

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

GridView1 is the ID of the gridView,

GridView1是gridView的ID,

here is the templateField:

这是templateField:

<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" />

here is the Code Behind:

这是守则背后:

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

I think the problem is that the templatefield has a textbox and a button, please correct me if I am wrong.

我认为问题是templatefield有一个文本框和一个按钮,如果我错了,请纠正我。

BTW I can't remove the button in the templatefield, it has to be a textbox with a button. Thanks.

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

3 个解决方案

#1


0  

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";
        }
    }
}

#2


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";
    }

This will replace the text box of each row and you can impose conditions for replacing it in specific rows.

这将替换每行的文本框,您可以强制条件在特定行中替换它。

#3


1  

Without further code from you, something along the lines of:

没有你的进一步代码,有些东西:

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

where index 2 is the row index of your gridview's databound data

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

#1


0  

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";
        }
    }
}

#2


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";
    }

This will replace the text box of each row and you can impose conditions for replacing it in specific rows.

这将替换每行的文本框,您可以强制条件在特定行中替换它。

#3


1  

Without further code from you, something along the lines of:

没有你的进一步代码,有些东西:

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

where index 2 is the row index of your gridview's databound data

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