Try this,
It might help you.
Create
unique
Id for the
TextBox
Control as below
and in the
post back
we can use
Request.Form [
^] object to get the values of the particular text boxes, which we can query using the
name
attribute of the Textbox.
int i = 0;
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = new TextBox() { ID = "txtDynamic" + i++ };
e.Row.Cells[0].Controls.Add(txt);
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
var Column1TextBoxes = Request.Form.AllKeys.Where(k => k.Contains("txtDynamic")).ToList();
for (int i = 0; i < Column1TextBoxes.Count; i++)
{
string value = Request.Form[Column1TextBoxes[i]];
}
}
Similarly you can create multiple controls on run time and get the values by using the name of the controls.
Note: After
Postback
you will have to rebind the
GridView
with the updated values, else you wont be able to retain the data.