Click here to Skip to main content
16,015,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
protected override void OnInit(EventArgs e)
  {
       SelectedItemGateway Gateway = new SelectedItemGateway();
       //get columns name for selected dropdown
       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.

C#
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);
                  }
          }

  }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900