Search code examples
c#asp.netdropdownbox

Keeping selected item from dropdown box after page refresh in asp web form


I have a aspx page that has several drop down list. So when the users forget to input some values i have to show an error to user, but every DDL are initialized again, and the users information are lost, how can I avoid this ?

When the users forget to input values i return them to current url

url1=www.spadsystem.com
Response.Redirect(Url1);

I heard that we can avoid this problem by using something like absolute i am not sure.


Solution

  • Use SessionState object to store the SelectedIndex property of ComboBox (e.g. Cmb), then apply it in Page_Load() event.

    Example 1: Store value in Session

    int _idx = Cmb.SelectedIndex;
    Session["idx"] = _idx.ToString();
    

    Example 2: Read from Session and apply to ComboBox:

    if(!IsPostBack) {
         Cmb.SelectedIndex = (int)(Session["idx"]);
    }
    

    More details at: http://msdn.microsoft.com/en-us/library/vstudio/ms178581%28v=vs.100%29.aspx

    Rgds,