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

6

5

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.

Rheumatic answered 27/5, 2009 at 9:57 Comment(0)
L
9

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.
Leporide answered 27/5, 2009 at 10:6 Comment(2)
Damn... you're on a roll today ! +1 :-)Cooper
Great, succinct solution that directly answers the question. +1!Rettke
S
2

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.

Seem answered 27/5, 2009 at 10:19 Comment(0)
C
1

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

Cooper answered 27/5, 2009 at 10:28 Comment(0)
R
0

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

Rheumatic answered 27/5, 2009 at 11:8 Comment(2)
See my answer above... you should be able to call YourGridview.DataBind() after you programatically set the drop down list valueLeporide
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. ;)Rheumatic
T
0

I had a similar problem...Kezzer's fix worked for me.

Just put:

Chart1.databind()

In the PreRenderComplete event in your code-behind.

Since PreRender complete runs as the very last thing before the chart gets rendered, if you "bind" your chart here, it insures that what is visually "showing" in your dropdowns will in fact be what is used as the values to render your chart.

Our issue was with auto-postback cascading dropdowns. We had Country, State, City dropdowns. If the user selected "United States", "Ohio", "Clevland", everything worked fine, but then if they went back up and changed country to "France" it would cause an error on the chart, even though the dropdowns had auto adjusted for this change, and reset the State and City to the first values available.

Adding the autopostback in PreRenderComplete forced the chart to "get: its values after the dropdowns had been auto-corrected...awesome!

Thatch answered 8/6, 2011 at 20:1 Comment(0)
O
0

Another way, if you are still looking for a post back to do a response.redirect("~/ImOnThisPageAlready"); to the same page.
pretty simple, not sure how busy your site is, but it works very quickly for my small application.

Oakleil answered 27/3, 2012 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.