Dinesh
Dinesh

Reputation: 45

keep the selected item of dropdown list when the page is being reloaded

i have asp.net DropDownList and Button. Once i click on the button it will set some value of java script variable. when i select the item from drop down list according to that previous set java script variable, query string should be updated.

i have done query string updated.but the problem is drop down selected item will change to the default item due to the post back.

Note: i need to do window.location.href = newQStr; inorder to affect the updated query string.

what i want is keep the selected item of drop down list without changing due to the post back.

i'm using jquery for clint side operations.

any idea..?

Thank in advance

Upvotes: 1

Views: 722

Answers (2)

Leandro Bardelli
Leandro Bardelli

Reputation: 11578

why do you dont work with page_load is postback and select again the item? I think is a cycle problem, not a code problem. Maybe you may think again the logic.

Upvotes: 0

James Johnson
James Johnson

Reputation: 46047

The way you're doing it, the page does not do a postback in the traditional sense. You're just redirecting back to the same url with an updated QueryString parameter. I'm not sure if there's a reason why you're doing it that way, but why not add an event handler for the button to set the selected value of the select? Here's an example:

<asp:Button ID="Button1" runat="server" Text="Hello" OnClick="Button1_Click" ... />

And in the code behind:

protected void Button1_Click(object sender, EventArgs e)
{
    //do some logic to figure out what the selected value should be

    ListItem item = DropDownList1.Items.FindByValue("SomeValue");
    if (item != null)
        item.Selected = true;
}

Upvotes: 1

Related Questions