views:

6020

answers:

4

Hi,

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.

+4  A: 

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.
Eoin Campbell
Damn... you're on a roll today ! +1 :-)
Cerebrus
Hah cheers dude ;)
Eoin Campbell
+2  A: 

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.

Kezzer
+1  A: 

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).

Cerebrus
A: 

Thanks for help everyone. I was about to follow Cerebrus' suggestion, but then a colleague proposed a different idea. The commented line was replaced with the uncommented line as below.

<SelectParameters>
   <%--<asp:Parameter Name="timevalue" Type="String" DefaultValue="now" />--%>
   <asp:ControlParameter ControlID="timevalueDropDownList" PropertyName="SelectedValue" Name="timevalue" DefaultValue="now" />
</SelectParameters>

Other than that, everything in my first post applies. In pageload I checked the session and applied the changes to the timevalueDropDownList as before. The session was always updated in a SelectedIndexChanged event, etc.

But as always, good ideas there with the responses, and I'll be sure to refer to these in similar issues in the future. I've had a lot of these but just found other ways around them, now I might not have to. ;)

Zan
See my answer above... you should be able to call YourGridview.DataBind() after you programatically set the drop down list value
Eoin Campbell
I actually tried databinding right after setting the session value to the datasource, but it didn't work. It was the first thing I did I think. To be honest, I'm not sure if I realised to put the dropdownlist on autopostback="true" before then, though. As said I appreciate all the answers and as always, I voted you guys up on those. When I say I'll check on this thread in similar issues in the future, I MEAN it. I've done so before, every single time. I only come here and ask when there's a DL coming or I've been stuck for too long and can't figure out the answer on my own anyway. ;)
Zan