Solved

Does RadioButtonList.items.clear() work properly?

Posted on 2009-05-06
9
740 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Chuck98RT10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24321732
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
 

Author Comment

by:Chuck98RT10
ID: 24321794
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
 

Author Comment

by:Chuck98RT10
ID: 24321815
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 12

Expert Comment

by:wht1986
ID: 24321847
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
 

Author Comment

by:Chuck98RT10
ID: 24331521
Awesome! Thanks. One question: why not Page_Init instead of protected override void OnInit ?
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24332152
Page_Init would work as well, just old habits from what I first learned
0
 

Author Comment

by:Chuck98RT10
ID: 24332368
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
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24332483
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
 

Author Closing Comment

by:Chuck98RT10
ID: 31578822
Excellent. Exactly what I needed.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Linq Select From List 3 129
"The resource cannot be found" 1 41
C# rename a class and update all references efficiently 6 51
.NET Redemption/Outlook Examples 3 29
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question