Solved

Does RadioButtonList.items.clear() work properly?

Posted on 2009-05-06
9
733 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now