0

I have a dropdown list in my form . After submitting the form I don't get selected value from the dropdown list in UI. I only get first value . How to implement this . How to display dropdown selected value after form submit .

Maximious
  • 155
  • 2
  • 12

1 Answers1

0

You need to use ViewState or Sessions to save the variable and reuse it, It would look something like this:

private void Page_Load()
{
        //If page is not being loaded for first time then set the value another 
        //option would be to check if ViewState["dropdownvalue"] == null and if 
        //not then set the value
    if (Page.IsPostBack)
    {       
        DropDownList1.SelectedValue = ViewState["dropdownvalue"]
    }
}
protected void Submit_Click(object sender, EventArgs e)  
{   
    ViewState["dropdownvalue"] = DropDownList1.SelectedValue;  
    // Do everything else
}

barbecu
  • 684
  • 10
  • 28
  • thank you for your reply but I want to display dropdown selected value after form submit – Maximious Mar 19 '20 at 19:07
  • So you want to keep the selected value after leaving the page? – barbecu Mar 19 '20 at 19:12
  • In that case use Sessions, https://stackoverflow.com/questions/5282677/how-to-use-session-variable-in-asp-using-c-sharp they are basically a way to transfer data between pages. ( I define my sessions in Global.asax but i'm not completely sure its necessary, just a practice of mine). Let me know if this is what you are looking for and i'll update my answer – barbecu Mar 19 '20 at 19:18
  • after page load the dropdown selected value is gone . I want to show the selected dropdown value into dropdown UI like selected = selected – Maximious Mar 19 '20 at 19:32
  • You need to save the value of the dropdownlist in a Session (now that I think about it a ViewState would work better) and when you reload the page set the Dropdownlist value to the session value – barbecu Mar 19 '20 at 19:40