SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    Code:
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        </asp:RadioButtonList>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    .aspx.cs
    Code:
        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)
        {
    
        }

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Does this help?
    Code:
        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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •