Dynamic gridview, ItemTemplate, Page life cycle, ViewState

Last post 06-08-2009 2:08 AM by chandu_504. 4 replies.

Sort Posts:

  • Dynamic gridview, ItemTemplate, Page life cycle, ViewState

    03-29-2009, 7:07 AM
    • Member
      point Member
    • Musta
    • Member since 08-04-2008, 7:53 AM
    • Posts 13

     Hi all,

    I have the issue and I'm unable to solve it.

    I have programatically created gridview, where is Templatefield and dropdownlist inside.

    I created TemplateField following this manual:   http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.templatefield.aspx

     I declare my gridview in OnInit() method and do databind() in Page_PreRender(). The problem is the creating of dropdownlists in Templatefield is fire up on databind() in Page_PreRender() and ViewState can't catch it, so I'm unable to reach changed values after PostBack.

    Please can you tell me the correct way how to manage it ?

        override protected void OnInit(EventArgs e)
        {
          //...
          GridView gridView = new GridView();
          gridView.ID = "gv" + depItem["dptId"];
          gridView.RowCommand += new GridViewCommandEventHandler(delItem);
          gridView.EnableViewState = true;
          
          TemplateField field = new TemplateField();
          field.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, headerTextFromLocRes);
          gridView.Columns.Add(field);
          pnContent.Controls.Add(gridView);
          //...
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
          //...
          GridView gridView = (GridView)pnContent.FindControl("gv" + depItem["dptId"]);
          gridView.DataSource = tasks;
          gridView.DataBind();
          //...
        }
    
    //-------
    public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;
    
        public GridViewTemplate(DataControlRowType type, string colname)
        {
            templateType = type;
            columnName = colname;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = "<b>" + columnName + "</b>";
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    DropDownList priorityList = new DropDownList();
                    priorityList.DataBinding += new EventHandler(this.PriorityList_DataBinding);
                    container.Controls.Add(priorityList);
                    break;
                default:
                    break;
            }
        }
        private void PriorityList_DataBinding(Object sender, EventArgs e)
        {
            DropDownList priorityList = (DropDownList)sender;
            GridViewRow row = (GridViewRow)priorityList.NamingContainer;
            
            //...
            
            priorityList.DataSource = loop;
            priorityList.ID = "ddlPriority" + dptId;
            priorityList.SelectedValue = selectedValue.ToString();
            string order = (selectedValue - 1).ToString();
            //..
        }
    }
    
  • Re: Dynamic gridview, ItemTemplate, Page life cycle, ViewState

    03-29-2009, 7:27 AM
    Answer

     The templatefields are created when the data is bind to the GridView. Before that the control doesn't have a chance to know how many objects of your template field it needs. It is creating one template per row. So you will have to bind your data to the control before the viewstatew will be initialized! Why do you bind the data to the control in the PreRender event and not in the OnInit?

    Mark as Answer if this reply solved your problem
  • Re: Dynamic gridview, ItemTemplate, Page life cycle, ViewState

    03-29-2009, 7:45 AM
    • Member
      point Member
    • Musta
    • Member since 08-04-2008, 7:53 AM
    • Posts 13

     Hi,

    DataBind is on Page_PreRender(), beacause user can perform an action like delete row or add new row. If it was in  OnInit I would have to probably reload page twice or do some not looking good code. But I guess I will have to. 
  • Re: Dynamic gridview, ItemTemplate, Page life cycle, ViewState

    05-28-2009, 2:00 PM
    • Member
      4 point Member
    • GarthMartin
    • Member since 05-28-2009, 2:50 PM
    • Posts 3

     I am having similar problems with a Gridview created with dynamic template columns as Item Templates containing textboxes.  On postback I can never see the values input by the user even after recreating the grid in OnInit and binding to the original datasource (what else could I bind it to?).

     

    Funny thing is, when I allow the page to postback and simply reload (while recreating the Gridview dynamically and rebind to the dataset) the values input by the user remain!  How do I get these values out so I can save them in the database?  No matter what stage I try to view the textbox values (Load, PreRender, ButtonClick etc) the text boxes are empty event though the column I am looking at is populated (either in the original datasource or the user input information) -- the only exception is the BoundField I have as the key.

     

      I am running out of options at this point...   

     

    Thanks in advance

  • Re: Dynamic gridview, ItemTemplate, Page life cycle, ViewState

    06-08-2009, 2:08 AM
    • Member
      6 point Member
    • chandu_504
    • Member since 01-19-2009, 8:38 AM
    • Posts 3

    I have worked on Similar ikind of problem you can easily solve this problem using below code

      Protected Overrides Sub LoadViewState(ByVal savedState As Object)

                MyBase.LoadViewState(savedState)                 BindGridData()   End Sub

     

     

Page 1 of 1 (5 items)