Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone;

I've got a bit of a dilemma, and I'm not sure how to solve it.

I've got a form, and on it, there's a dropdownlist whose value changes the form dynamically. There's also a submit button. When the submit button is hit, I need the page to refresh in order to show the changes that were made on the form in a gridview. But I also need the dropdown to retain it's value. At current, in the button_click handler, I have the page doing a response.redirect. The problem, as I understand it, is this does not retain the viewstate for the dropdown to use. I tried a server.transfer, once on it's own, and another time using the preserveForm property set to true (this caused a stack overflow). I'm a little stuck at this point and would really appreciate any help. Thanks!
Posted
Comments
Yuriy Loginov 16-May-13 8:06am    
are you doing response.redirect to the same page that the button was clicked on?
Jack R. Schaible 16-May-13 8:08am    
Yeah; "Response.Redirect("~/MyStuff/Languages/Alphabets.aspx");"
Yuriy Loginov 16-May-13 8:17am    
you can do a few things here.
1. On the dropdown list you can set AutoPostBack = true. This will cause the form to refresh every time a selected value changes. Then have an event handler for SelectedIndexChanged and inside the event handler update your page accordingly.
2. If you absolutely must redirect using Response.Redirect then you can add a parameter to your response like this MyStuff/Languages/Alphabets.aspx?dropdownvalue=myvalue and when the form loads again you can read the value like this Page.Request.QueryString["dropdownvalue"]. Once you read the value simply set the value of the drop down.
ZurdoDev 16-May-13 12:11pm    
+5. You should post as the Solution.
Yuriy Loginov 16-May-13 13:05pm    
Thanks!

1 solution

You can do a few things here.

1. On the dropdown list you can set the auto post back property to true and assign an event handler to it. This will cause the form to refresh every time a selected value changes. Then have an event handler for SelectedIndexChanged and inside the event handler update your page accordingly.
Here is the mark up and C# codebehind
XML
<asp:dropdownlist runat="server" id="foo" autopostback="true" onselectedindexchanged="foo_SelectedIndexChanged" xmlns:asp="#unknown"></asp:dropdownlist>

C#
protected void foo_SelectedIndexChanged(Object sender, EventArgs e){
}

2. If you absolutely must redirect using Response.Redirect then you can add a parameter to your response like this MyStuff/Languages/Alphabets.aspx?dropdownvalue=myvalue and when the form loads again you can read the value like this Page.Request.QueryString["dropdownvalue"]. Once you read the value simply set the value of the drop down.

Trigger page reload
C#
Response.Redirect("~/MyStuff/Languages/Alphabets.aspx?dropdownvalue=myvalue");
Handle reload response
C#
protected void Page_Load(object sender, EventArgs e)
{    
    string dropdownvalue = Page.Request.QueryString["dropdownvalue"];  
}
 
Share this answer
 
Comments
Jack R. Schaible 16-May-13 15:29pm    
I didn't even think about using the request's querystring property...that worked! Thank you so much!
Yuriy Loginov 16-May-13 15:58pm    
you are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900