RadioButtonList keep selected after postback

Hello all,

in the below code I am adding options to the radio button list in the page_load event. When an option is selected and the Button is clicked, the option is deselected.

What is the best way of keeping the selected option selected after the button click?

.aspx


    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    </asp:RadioButtonList>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

.aspx.cs


    protected void Page_Load(object sender, EventArgs e)
    {
        RadioButtonList1.Items.Clear();
        ListItem item = new ListItem();
        item.Text = "option1";
        item.Value = "1";
        RadioButtonList1.Items.Add(item);

        item = new ListItem();
        item.Text = "option2";
        item.Value = "2";
        RadioButtonList1.Items.Add(item);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

Does this help?


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadioButtonList1.Items.Clear();
            ListItem item = new ListItem();
            item.Text = "option1";
            item.Value = "1";
            RadioButtonList1.Items.Add(item);

            item = new ListItem();
            item.Text = "option2";
            item.Value = "2";
            RadioButtonList1.Items.Add(item);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

The button is causing a postback which fires your RadioButtonList1.Items.Clear(); and refills the radio button list with your items again.