Search code examples
c#asp.netautopostback

ASP.NET AutoPostBack is clearing form data


I have a radio button list and I want to perform some action when a user makes a selection.

<asp:RadioButtonList id="docList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="loginUser" />

However, I get an empty value for docList.SelectedValue. I am guessing this is due to form data getting cleared upon Autopostback. Is there a way I can have AutoPostBack and not lose form data?


Solution

  • Yes you can by implementing it like this:

    <asp:RadioButtonList id="docList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="loginUser" />
    
    
    public string SelectedDoc {get;set;}
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostBack){
       }
       else
       {
          SelectedDoc = docList.SelectedValue; //this will be set on postback and will contain the selected value.
       }
    }