tags:

views:

2092

answers:

4

Basically, I have a drop down list and a dynamically added user control. The user control loads a grid view depending on the choice that was made in the drop down list. The drop down list is not part of the user control.

Now, the question is, how do i simulate (isControlPostback = false) every time the user changes the selection in the drop down list? It looks like ViewState remembers the control.

Inside my user control I have:

protected bool IsUserControlPostBack
{
    get
    {
     return this.ViewState["IsUserControlPostBack"] != null;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsUserControlPostBack)
    {
     ViewState.Add("IsUserControlPostBack", true);
     //load stuff in the grid view and bind it
    }
}

When the user changes the selection on the drop down list, i have a javascript confirm box, and the page posts back. So OnSelectedIndexChanged event for drop down list doesn't get triggered. I would like to remove to do something like this every time the selected index changes: ViewState.Remove("IsUserControlPostBack");

A: 

Add the control to the page sometime before OnLoad. E.g. OnInit. Between OnInit and OnLoad, the viewstate is loaded and postback events are run.

Robert C. Barth
its already being added inside OnInit
gnomixa
+1  A: 

You can make changes to the control in prerender event. When this event is fired all other actions are made. Or you can do public property in user control and when setting required to value react on appropriately.

Sly
+1  A: 

The ViewState you access in your user control is not the same one you access on the page. If you need your page to communicate with your user control, I suggest you add a public method on your user control for this purpose.

If, for some reason, you prefer something similar to your ViewState approach, you can try Context.Items. Note that Context.Items is not preserved between requests.

Thomas Eyde
when i load a control on a page, I am doing something like Control control = Load("controlname.ascx");Would the control have access to the public methods?The thing is I don't want the control to refresh every time i am loading it - but only when drop down list selection changes.
gnomixa
Viewstate on the page is different from the Viewstate on the control? That's news to me. Do you have an MSDN link that explains it? Isn't the point of Viewstate to keep it all in one place? Totally confused now.
gnomixa
The viewstate data will all be serialized in one big chunk, which is stored on the page. But the data is organized as branches in a tree, where each control get their own branch. Nevertheless, ViewState is not for sharing state between controls.
Thomas Eyde
Try this: MyControl control = (MyControl) Load("controlname.ascx");Now you have access to your method.
Thomas Eyde
A: 

For anyone who is interested to know the answer: I ended up implementing a public property inside user control and load the control inside the server drop down list SelectedIndexChanged event rather than OnInit. This eliminated the need for explicit Viewstate use.

gnomixa