Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net (vb) Force Postback in code-behind

I'm in need of a way to force a postback or page reload in codebehind. Tried using some javascript but didn't get it to work. Browsing the net I see the first question is "why"?

Circumstances are that I have a dropdownlist on autopostback, and the gridview datasource's selectparameter is derived from the selected value of that dropdownlist. So the page works fine normally and the contents are updated whenever the selected item is changed. But some of the links and buttons take the customer off the page so they link back later.

The idea is to store the last choice in a session, and to check on the first page.load event if the session option is other than default. Now I can change the selectedindex of the dropdownlist based on that, but apparently the datasource triggers faster than page.load, so unless I can force a reload, this won't help.

Any ideas? A full page postback / reload isn't the only option of course, just forcing the gridview / datasource to refresh is good enough. I just don't know how to do that other than reloading the whole page.

Thanks.

like image 552
Zan Avatar asked May 27 '09 09:05

Zan


3 Answers

You could put an ajax timer on the page, enable it when they return, causing an OnTick autopostback as soon as the page renders, and then disabling it, but to be honest... that's a horrendous work around for a trivial problem.

Why can't you just rebind your GridView after you programatically change the drop down list value. e.g. the time line would be something like.

  • Person Returns To Page (not a post back)
  • GridView Binds with Default Value
  • Page Load
    • Check your session variable
    • If a value is found
    • Set your DropDownList selected value
    • Call .DataBind() again for the GridView/DataSource to force it to rebind.
like image 103
Eoin Campbell Avatar answered Nov 12 '22 16:11

Eoin Campbell


Why can't you use a different page event? Have you tried PreRenderComplete? Remember the orderings of events is important in postbacks. PreRenderComplete is the last event to be called before the page is rendered. Page_Load is actually somewhere in the middle.

I do things like this all the time at work.

Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
    Session("Value") = ddlList.SelectedValue
End Sub

That code probably isn't correct, but it just gives you an idea of the event.

like image 29
Kezzer Avatar answered Nov 12 '22 16:11

Kezzer


Why not handle the GridView's DataBinding event and check the value in Session in that handler ? If it does not equal the dropdownlist parameter, you can change it and let databinding take its course.

See my answer here for a more general sample which changes the SelectCommand of the Datasource control. In the same manner you should be able to change the ControlParameter's value. This answer does the same, but for a (nested) Repeater.

If this does not work, then Eoin's answer would be the best way (re-bind the GridView).

like image 1
Cerebrus Avatar answered Nov 12 '22 14:11

Cerebrus