I have created dynamic editable gridview in asp.net using "ONINIT" function, columns in gridview will change based on the dropdownlist selection and i need to edit and save records by using button click.
if the gridview column change due to dropdown selection i'm getting following error.
"Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request."
so i tried to set "EnableViewState = false" but i can't able to get gridview rows.
Here is my code for dynamic grid.
protected override void OnInit(EventArgs e)
{
SelectedItemGateway Gateway = new SelectedItemGateway();
List<Entity> listparam = Gateway.getSelectedItem(Session["DropdownSelectedItem"].ToString());
if (listparam.Count > 0)
{
GridView grdDynamic = new GridView();
grdDynamic.ID = "grdName";
grdDynamic.RowDataBound += new GridViewRowEventHandler(grd_RowDataBound);
foreach (var item in listparam)
{
string parameter = item.PARAMETER;
TemplateField TmpParam = new TemplateField();
TmpParam.HeaderText = parameter;
grdDynamic.Columns.Add(TmpParam);
TmpParam.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
TmpParam.ItemTemplate = new TemplateHandler(ListItemType.EditItem, parameter, "TextBox");
}
UpdatePanel.ContentTemplateContainer.Controls.Add(grdDynamic);
}
}
code for save operation. Here i can't able to get gridview rows using `foreach` statement.
protected void btnSave_OnClick(object sender, EventArgs e)
{
GridGateway Gateway = new GridGateway();
List<Entity> listparam = Gateway.getSelectedItem(Session["DropdownSelectedItem"].ToString());
GridView dynamicGrid = (GridView)Master.FindControl("MainContent").FindControl("UpdatePanel").FindControl("grdName");
foreach (GridViewRow row in dynamicGrid.Rows)
{
foreach (var item in listparam)
{
string parameter = item.PARAMETER;
TextBox txtvalue = (TextBox)dynamicGrid.Rows[row.RowIndex].FindControl("txt" + parameter);
Gateway.update(txtvalue.text);
}
}
}