[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Does RadioButtonList.items.clear() work properly?

Posted on 2009-05-06
9
Medium Priority
?
763 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 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