?
Solved

Does RadioButtonList.items.clear() work properly?

Posted on 2009-05-06
9
Medium Priority
?
749 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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