Search code examples
c#asp.netsessiongridviewradiobuttonlist

keep radio button selected across postback


I have a gridview with radio buttons to select a single row. The gridview is populated from a SQL DB according to a value selected from a drop down list. What I want to do is keep the selected radio button when the user moves to a different category, so if he wants to move back via the DDL the value he selected previously is still selected.

What is the best way to do this? The application will have users and I have read sessions could help me with this but I have no idea how it works!

Thanks in advance


Solution

  • I'm not sure really why you'd want to do it this way but you'd then save it in session this way:

    String selectedValue = rblYourRadioButtons.SelectedValue;
    Session["SelectedRadioButtonValue"] = selectedValue;
    

    Then, on postback or whenever, on the Item Databound of the gridview you check if the current row Id matches the value stored in the session, if it does then select that value in the radiobutton list.

    Personally I'd store the value in the DB rather than session.