Does RadioButtonList.items.clear() work properly?

When btnCopy_Click is fired it correctly sets lblSQL.Text to the selected radiobutton but the app then adds a complete set of radio buttons to the existing radio buttons in the radiobuttonlist control every time I click btnCopy. Obviously I don't want my radio button to grow every time I click btnCopy. I thought radList1.Items.Clear(); in the Page_load event would clear the control and repopulate it.

Help?
protected System.Web.UI.WebControls.Panel PnlControl;
 
    RadioButtonList radList1;
    ListItem rad1;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        radList1 = new RadioButtonList();
        if (!IsPostBack)
        {
            subBuildRadioListStores();
        }
        else
        {
            radList1.Items.Clear();
           subBuildRadioListStores();
        }
}
 
    public void subBuildRadioListStores()
    {
//DATA CONNECTION CODE HERE WORKS BUT NOT DISPLAYED FOR THIS EXAMPLE
        SqlDataReader thisReader = thisCommand.ExecuteReader();
 
        while (thisReader.Read())
        {
            rad1 = new ListItem();
            rad1.Text = thisReader["StoreName"].ToString();
            rad1.Value = thisReader["ID"].ToString();
            radList1.Items.Add(rad1);
            pnlStoresWithMenus.Controls.Add(radList1);
        }
    }
 
    protected void btnCopy_Click(object sender, EventArgs e)
    {
        lblSQL.Text = radList1.SelectedValue.ToString();
    }

Open in new window

Chuck98RT10Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
wht1986Connect With a Mentor Commented:
Glad I could help. Please dont forget to accept the solution. I love coding, but points make me feel all warm and fuzzy inside.
0
 
wht1986Commented:
According to the code above, every postback you are creating a new RadioButtonList (Line 8)

In addition you are calling subBuildRadioListStores every postback as well (Lines 11 & 16)

In this method, you are filling your new RadioButtonList with ListItems, and then adding the RadioButtonList to the controls collection of a panel.

In effect you are not just adding items each postback, but you are creating a new RadioButtonList class. Is this what you want to do? (1) does the RadiobuttonList control have to be dynamic or can it be in the panel on the aspx page in the designer?  I ask because my solution will be geared toward a dynamic or predefined control.
0
 
Chuck98RT10Author Commented:
Radiobuttonlist must be dynamic. The number of radiobutton is determined by the number of records in the database table.

Thanks for responding so quickly. I will look again at what you pointed out but I'm getting sleepy.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Chuck98RT10Author Commented:
BTW I've moved stuff around since my original post. I am getting the same duplicate radiobuttons with the following code...

   protected void Page_Load(object sender, EventArgs e)
    {
        radList1 = new RadioButtonList();
        if (!IsPostBack)
        {
        }
        else
        {
            radList1.Items.Clear();
        }
           subBuildRadioListStores();
0
 
wht1986Commented:
Ok with dynamic controls, try to avoid creating them in page_load, use the initialization, its also importantant to assign the same ID to the control.

Below is my code behind to add a dynamic radio button list to a panel.
=============
Default.aspx
=============
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Panel ID="Panel1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>
</body>
</html>
 
==============
Default.aspx.cs
==============
 
public partial class _Default : System.Web.UI.Page
{
    protected RadioButtonList list1 = null;
 
    protected override void OnInit(EventArgs e)
    {
        list1 = new RadioButtonList();
        list1.ID = "MyDynamicRadioButtonList";
        PopulateList();
        this.Panel1.Controls.Add(list1);
        base.OnInit(e);
 
    }
 
    private void PopulateList()
    {
        list1.Items.Add(new ListItem("Item1"));
        list1.Items.Add(new ListItem("Item2"));
        list1.Items.Add(new ListItem("Item3"));
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.Button1.Text = this.list1.SelectedItem.Text;
    }
}

Open in new window

0
 
Chuck98RT10Author Commented:
Awesome! Thanks. One question: why not Page_Init instead of protected override void OnInit ?
0
 
wht1986Commented:
Page_Init would work as well, just old habits from what I first learned
0
 
Chuck98RT10Author Commented:
FWIW I tried Page_Init and it didn't work as well as your solution. If I recall correctly the button had to be clicked twice for the value to be shown. Anyway, I went with you solution. Much thanks!!!
0
 
Chuck98RT10Author Commented:
Excellent. Exactly what I needed.
0
All Courses

From novice to tech pro — start learning today.