简体   繁体   中英

Generate textbox in gridview on button click? ASP.NET C#

I have a gridview in which textbox is generated dynamically on RowDataBound event.I want to generate 3 more textbox below gridview on button click. Here is my gridview 链接

Here is my code behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string s = Session["num"].ToString();
int num = Int32.Parse(s);
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i <num; i++)
{
 TextBox txt = new TextBox();
 txt.Height = 25;
 txt.Width = 150;
 txt.Font.Size = 10;
 txt.ID = "txt" + i;
 txt.Text = e.Row.Cells[i].Text.Replace("&nbsp;", "");
 e.Row.Cells[i].Controls.Add(txt);
}
}
}

Not sure what your exact problem is, but this Link might help you. Anyway, you can simply generate TextBox dynamically using Button Click Event.

The example shown below generates three TextBox Control on a Click Event:

TextBox txt;
static int i = 0;
protected void Button1_Click(object sender, EventArgs e)
{
    if(i%2==0)
    {
        for (int j = 0; j < 3; j++)
        {
            txt= new TextBox();
            txt.Height = 25;
            txt.Width = 150;
            txt.Font.Size = 10;
            txt.ID = j.ToString();
            PlaceHolder1.Controls.Add(txt);
         }
     }
     i++;                                           
}

Note: When you dynamically add controls to an ASP.NET page at runtime the object references are lost at postback because they have no object reference variable in the codebehind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM