-1

I want to keep selected item after page reload:

Excerpt from .aspx:

    <asp:DropDownList ID="MyDropDown" runat="server" AutoPostBack="true" 
        onselectedindexchanged="MyDropDown_SelectedIndexChanged">

    </asp:DropDownList>

Exerpt from .cs in page_load

        if (!IsPostBack)
        {
            PopulateDropDownList();
        }

with

    private void PopulateDropDownList()
    {
        MyDropDown.Items.Add("1");
        MyDropDown.Items.Add("2");
    }

    protected void MyDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
user310291
  • 36,946
  • 82
  • 271
  • 487
  • 1
    You are redirecting to same page, its not a post back. – Kundan Singh Chouhan Oct 06 '12 at 11:51
  • 3 questions with the same subject ! :) http://stackoverflow.com/questions/12740738/why-asp-net-dropdown-with-autopostback-doesnt-keep-my-selected-value http://stackoverflow.com/questions/12737240/asp-net-response-redirectrequest-rawurl-doesnt-work and this one.... – Aristos Oct 06 '12 at 11:59
  • not the same: each time there is a progress and different concept. – user310291 Oct 06 '12 at 12:05

2 Answers2

1

Response.Redirect refresh the page and you will loose view state that will have selected index. You can put the selected index in session before redirecting.

protected void MyDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["MyDropDownSelectedIndex"] = MyDropDown.SelectedIndex.ToString();
    Response.Redirect(Request.RawUrl);
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • This is bad practice and not solve anything because that way is need again to select that value on dropdownlist page load. And what if the user have two same page open ? the same value on the session, big mess.... – Aristos Oct 06 '12 at 12:00
  • I agree but why are are using Response.Redirect in MyDropDown_SelectedIndexChanged? – Adil Oct 06 '12 at 12:08
  • I use Response.Redirect because I need to reload the page as the content depends on what is selected from dropdown (database name) – user310291 Oct 06 '12 at 12:13
  • @Aristos bad practice or not it seems that Adil is the only one giving a solution that may work (have to try yet) so if you have better one ... :) – user310291 Oct 06 '12 at 12:15
  • @user310291 Ether try UpdatePanel, ether add the value from the drop down list as url parametre... more comple... eg redirect to `page.aspx?sel=12` - Adil give a solution, ok, the bad practice is for the user. – Aristos Oct 06 '12 at 12:32
  • If I add url parameter then I should better code like in PHP without viewstate, autopostback and update panel: that seems a lot overcomplicated don't you think I thought asp.net could be smarter ;) – user310291 Oct 06 '12 at 12:47
  • @user310291 I do not understand you. Php and asp.net can be as smart as you can programming them. Its not about the language, its about what you won to do. What you won to do, to make redirect and keep the value of your dropdownlist must be do with a url parametre. – Aristos Oct 06 '12 at 13:16
  • What I want is very simple functionality: list a table from a database chosen in a dropdown box and nobody seems to really know how to do this in pure asp.net (I don't want ajax). – user310291 Oct 06 '12 at 13:18
  • @user310291 Why you want to redirect page, that may give us some hint to get better solution? – Adil Oct 06 '12 at 15:25
  • @user310291 Its is simple if you do not make redirect. If you make redirect is also simple, you just need to add the dropdown selection to the url, or do what adil suggest. – Aristos Oct 06 '12 at 16:18
  • OK redirect was stupid, I was mistaken by solution given in aother thead. It works by putting db refresh in selection change thanks for your patience to all :) – user310291 Oct 07 '12 at 23:22
1

You need to populate the drop down list in the Page init event. If you do that during Page load event the view state cannot be restored correctly (because the drop down list is not populated before Page load event) so the on selected index changed event cannot be fired.

EDIT: you may want to cache the data which populate the drop down list to save some around trip to the database. I think you do not need to redirect in the on selected index changed event too.

airmanx86
  • 992
  • 1
  • 8
  • 16
  • I need to reload the page because the content depends on what is selected from dropdown. – user310291 Oct 06 '12 at 12:07
  • I tried to put in init, but it still doesn't remember my selection – user310291 Oct 06 '12 at 12:12
  • That's because of the redirection, removing the redirection and replace it with the code to render different contents base on the selected index. – airmanx86 Oct 06 '12 at 12:15
  • I need to do redirection because my content has to be updated from another database (the dropdown selects a new db) so the page needs to be reloaded (so redirection). – user310291 Oct 06 '12 at 12:17
  • 1
    @user310291 you do not need redirection your postback has already reloaded the page when the selected index changed. The post back events occur before the page is rendered. You can query the database in the SelectedIndexChanged event and repopulate the drop downs which will then render and your view state will be maintained with the previous values of existing controls. The ASP.Net Page life cycle can be a little confusing at first but you need to grasp the basics to get anywhere, perhaps read; http://msdn.microsoft.com/en-us/library/ms972976.aspx – Dave Anderson Oct 06 '12 at 13:26
  • @DaveAnderson, completely agree with your comment. – airmanx86 Oct 07 '12 at 00:35