About inserting record into dynamically created gridview in asp.net c#
I have created gridview dynamically on click event of button.I have 4 fields per row(First name,last name, relation,DOB) with link button for add(lnkadd).I used Itemtemplate, edittemplate, EditItemTemplate, FooterTemplate to design gridview. I used function to add rows to gridview in lnkadd click event as follows:
tabemp = (DataTable)Session["tabempsession"开发者_运维技巧];
if (tabemp.Rows.Count == 1)
{
if (Convert.ToString(tabemp.Rows[0].ItemArray.GetValue(0)) == "")
{
tabemp.Rows.Clear();
}
}
DataRow drow = tabemp.NewRow();
//create new veriable of(textbox,dropdownlist etc) in ItemTemplate in grid view
TextBox txtFName1 = (TextBox)mygridview.FooterRow.FindControl("txtFName1");
TextBox txtLName1 = (TextBox)mygridview.FooterRow.FindControl("txtLName1");
DropDownList ddlRelation = (DropDownList)mygridview.FooterRow.FindControl("ddlRelation");
TextBox txtDOB = (TextBox)mygridview.FooterRow.FindControl("txtDOB");
//insert values into rows in tabemp table
drow[0] = txtFName1.Text;
drow[1] = txtLName1.Text;
drow[2] = ddlRelation.SelectedItem.Text;
drow[3] = txtDOB.Text;
tabemp.Rows.Add(drow);
Session["tabempsession"] = tabemp;
//int f = tabemp.Rows.Count;
mygridview.DataSource = (DataTable)Session["tabempsession"];
mygridview.DataBind();
But i am not getting textbox values .ie(txtFName1.Text), its getting as null. Please give me suggestion for getting values of all controls inside gridview. Asp.net C# Thank you.
As per my guess, the reason is you are losing data during post back. i.e. At first your grid is filled but when you click on the button, the page is posted back and the grid appears to you is new grid after postback. Try to use (!isPostBack). M not sure but this may help you a bit to get the hint.
精彩评论