views:

120

answers:

1

Hi,

I have a problem regarding to asp.net lifecylce hierarchy.

Basically, I have one user control which has a GridView in it. And this GridView is dynamically generated based on a public property on the control (named Parameter in the simplified code below).

Everything is fine when I insert this control in an aspx page, set its Parameter property and call DataBind(Parameter) on it. GridView is generated and populated on the UI.

Problem occurs when I post-back the page. I have to regenerate the GridView structure so that the data in the ViewState of the control can be used to populate the GridView. So that I can achieve its content. But as long as the GridView structure is generated dynamically and it is based on the Parameter property set on it, this is not possible. Because the OnInit of the user control is called before the OnInit of the page and that is why the Parameter property is set after the generation of the GridView structure. As a result I get an empty Gridview in the end.

Here is the code in a simplified way.

Can you give me some recommendation how to overcome this?

Can I explicitly force asp.NET to re-load the ViewState of a gridview?

Page HomePage.aspx has an OnInit event handler in such a way, where it sets a property of the user control ctlMyUSerControl

protected override void OnInit(EventArgs e)
    {

              ctlMyUserControl.Parameter = new Parameter()
                       name="Orhan",
                       surname= "Pamuk"};

        }

And in ctlMyUserControl's OnInit I have

    protected override void OnInit(EventArgs e)
{
        if (Page.IsPostBack && Parameter !=null && SomeGridViewRowsExistOnUI)
        {
                        // Generate dynamic columns based on Parameter property
                        // So that gridview can be populated 
                       // with the post-backed data which 
                        // should contain the ViewState of the gridview 
                       GenerateGridViewColumns(Parameter);
        }

    base.OnInit(e);
}
A: 

I have sold it guys.

What I have done is regenerating my GridView columns on the container page of the user control.

So, On the OnInit of the page I regenerate my columns and it is still before the call of LoadViewState() method.

protected override void OnInit(EventArgs e)
    {

              Parameter parameter = new Parameter()
                       name="Orhan",
                       surname= "Pamuk"};

               ctlMyUserControl.GenerateGridViewColumns(parameter);


        }
burak ozdogan