I have a loop where radio buttons are displayed depending on certain information.

I need to make the ID a combination for two variables, Resource and a counter, i.

I have it looping

for (int i = 0; i < ResourceCount; i++)
{%>
    <TD>
        <%string ResourceName = Resource[i].ToString();
        <%=ResourceName%>
    </TD>

    <%for(int i2 = 0; i2 < 10; i2++)
    {
         <asp:RadioButton runat="server" ID="<%=ResourceName%>-<%=i2%>" GroupName="GName" />
    }

<%}

Is there a way to do this?

Thank you.

Recommended Answers

All 7 Replies

>I have it looping

That is improper.

>Is there a way to do this?

for (int i = 0; i < ResourceCount; i++){ 
   string ResourceName = Resource[i].ToString();
   for(int i2 = 0; i2 < 10; i2++){
        RadioButton btn=new RadioButton();
        btn.ID=ResourceName + "_" + i2;
        btn.GroupName="GName";
        form1.Controls.Add(btn);
    }
}

>I have it looping

That is improper.

>Is there a way to do this?

for (int i = 0; i < ResourceCount; i++){ 
   string ResourceName = Resource[i].ToString();
   for(int i2 = 0; i2 < 10; i2++){
        RadioButton btn=new RadioButton();
        btn.ID=ResourceName + "_" + i2;
        btn.GroupName="GName";
        form1.Controls.Add(btn);
    }
}

I need it displayed within a table. Is there a way to add the button this way and still have it in a table?

I need it displayed within a table. Is there a way to add the button this way and still have it in a table?

You can put an asp: PlaceHolder element wherever you want the radio buttons to appear on your page and give it an ID. Example:

<div>
        <asp:PlaceHolder runat="server" ID="placeholder" />
    </div>

The div could easily be a paragraph tag, table cell, span, etc. You would programmatically add to it much like the previous code of adding to the form.

protected void Page_Load(object sender, EventArgs e)
    {
        RadioButton button = new RadioButton();
        button.Text = "Demo 1";
        button.GroupName = "DemoGroup";
        button.ID = "demo1";
        placeholder.Controls.Add(button);
    }

The challenge with the method of adding invidual radio buttons is that you would also have to include a method to force HTML line breaks if you want the list to have a good format. Example:

protected void Page_Load(object sender, EventArgs e)
    {
        RadioButton button = new RadioButton();
        button.Text = "Demo 1";
        button.GroupName = "DemoGroup";
        button.ID = "demo1";
        placeholder.Controls.Add(button);

        LiteralControl literalControl = new LiteralControl("<br />");
        placeholder.Controls.Add(literalControl);

        RadioButton button2 = new RadioButton();
        button2.Text = "Demo 2";
        button2.GroupName = "DemoGroup";
        button2.ID = "demo2";
        placeholder.Controls.Add(button2);
    }

An alternate solution would be to use a RadioButtonList, as you can add many ListItem options to it and can control how they are laid out on the page (vertically, horizontally, text on right, text on left, etc) via properties of the RadioButtonList control. The downside is that it wouldn't really support what I believe you're doing in your code, which is having a group broken up into multiple parts (with headers between each part), yet only one option is ultimately selectable from the entire group.

I need it displayed within a table. Is there a way to add the button this way and still have it in a table?

Also, how would I refer to it later? Would I have to recreate the loops for the resources and all that? I'd like to not to have to do this, but it seems to be the only option.

To track the buttons' checked values on a postback, be sure that you are creating the buttons on Page_Init and not Page_Load. The reason is that a page's viewstate is managed between Init and Load, and if your elements are not added prior to the viewstate management taking place, the user's selections will not be available when you wish to check them. Also, for ease of tracking, you can add the RadioButtons as you create them to a List<RadioButton> (from the System.Collections.Generic namespace). Here's a working example of creating the controls on Page_Init, adding them to the generic list, and then getting the checked item on postback.

placeholdertest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="placeholdertest.aspx.cs" Inherits="placeholdertest" %>
<!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">
    <div>
        <asp:PlaceHolder runat="server" ID="placeholder" />
        <br /><br />
        <asp:Button ID="btnDemo" runat="server" Text="Demo Submit" OnClick="btnDemo_Click" />
        <br /><br />
        <asp:Literal ID="litOutput" runat="server" />
    </div>
    </form>
</body>
</html>

placeholdertest.aspx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

public partial class placeholdertest : System.Web.UI.Page
{
    List<RadioButton> radioButtons = new List<RadioButton>();

    protected void Page_Init(object sender, EventArgs e)
    {
        for (int i = 1; i < 5; i++)
        {
            RadioButton button = new RadioButton();
            button.Text = "Demo " + i.ToString ();
            button.GroupName = "DemoGroup";
            button.ID = "demo" + i.ToString();
            placeholder.Controls.Add(button);

            LiteralControl literalControl = new LiteralControl("<br />");
            placeholder.Controls.Add(literalControl);

            radioButtons.Add(button);
        }
    }

    protected void btnDemo_Click(object sender, EventArgs e)
    {
        foreach (RadioButton button in radioButtons)
        {
            if (button.Checked)
            {
                litOutput.Text = "You selected " + button.ID;
            }
        }
    }
}

To track the buttons' checked values on a postback, be sure that you are creating the buttons on Page_Init and not Page_Load. The reason is that a page's viewstate is managed between Init and Load, and if your elements are not added prior to the viewstate management taking place, the user's selections will not be available when you wish to check them. Also, for ease of tracking, you can add the RadioButtons as you create them to a List<RadioButton> (from the System.Collections.Generic namespace). Here's a working example of creating the controls on Page_Init, adding them to the generic list, and then getting the checked item on postback.

placeholdertest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="placeholdertest.aspx.cs" Inherits="placeholdertest" %>
<!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">
    <div>
        <asp:PlaceHolder runat="server" ID="placeholder" />
        <br /><br />
        <asp:Button ID="btnDemo" runat="server" Text="Demo Submit" OnClick="btnDemo_Click" />
        <br /><br />
        <asp:Literal ID="litOutput" runat="server" />
    </div>
    </form>
</body>
</html>

placeholdertest.aspx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

public partial class placeholdertest : System.Web.UI.Page
{
    List<RadioButton> radioButtons = new List<RadioButton>();

    protected void Page_Init(object sender, EventArgs e)
    {
        for (int i = 1; i < 5; i++)
        {
            RadioButton button = new RadioButton();
            button.Text = "Demo " + i.ToString ();
            button.GroupName = "DemoGroup";
            button.ID = "demo" + i.ToString();
            placeholder.Controls.Add(button);

            LiteralControl literalControl = new LiteralControl("<br />");
            placeholder.Controls.Add(literalControl);

            radioButtons.Add(button);
        }
    }

    protected void btnDemo_Click(object sender, EventArgs e)
    {
        foreach (RadioButton button in radioButtons)
        {
            if (button.Checked)
            {
                litOutput.Text = "You selected " + button.ID;
            }
        }
    }
}

Thank you so much. That is exactly what I was trying to do. I'm new to .NET and C#, so I am very greatful for your help.

To track the buttons' checked values on a postback, be sure that you are creating the buttons on Page_Init and not Page_Load. The reason is that a page's viewstate is managed between Init and Load, and if your elements are not added prior to the viewstate management taking place, the user's selections will not be available when you wish to check them. Also, for ease of tracking, you can add the RadioButtons as you create them to a List<RadioButton> (from the System.Collections.Generic namespace). Here's a working example of creating the controls on Page_Init, adding them to the generic list, and then getting the checked item on postback.

placeholdertest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="placeholdertest.aspx.cs" Inherits="placeholdertest" %>
<!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">
    <div>
        <asp:PlaceHolder runat="server" ID="placeholder" />
        <br /><br />
        <asp:Button ID="btnDemo" runat="server" Text="Demo Submit" OnClick="btnDemo_Click" />
        <br /><br />
        <asp:Literal ID="litOutput" runat="server" />
    </div>
    </form>
</body>
</html>

placeholdertest.aspx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

public partial class placeholdertest : System.Web.UI.Page
{
    List<RadioButton> radioButtons = new List<RadioButton>();

    protected void Page_Init(object sender, EventArgs e)
    {
        for (int i = 1; i < 5; i++)
        {
            RadioButton button = new RadioButton();
            button.Text = "Demo " + i.ToString ();
            button.GroupName = "DemoGroup";
            button.ID = "demo" + i.ToString();
            placeholder.Controls.Add(button);

            LiteralControl literalControl = new LiteralControl("<br />");
            placeholder.Controls.Add(literalControl);

            radioButtons.Add(button);
        }
    }

    protected void btnDemo_Click(object sender, EventArgs e)
    {
        foreach (RadioButton button in radioButtons)
        {
            if (button.Checked)
            {
                litOutput.Text = "You selected " + button.ID;
            }
        }
    }
}

One more question. Since the display of the radio buttons is in the Page_Init, and the data changes by the click of btn_Demo. How would I refresh the information created in the Page_Init? I need that to be dynamic. Also, there are certain parameters in the page that must be supplied: location and Date, but the radio button information cannot be supplied again because it would cause a PK violation.

Here's exactly what I'm doing. It's a reservation-type system. So a user selects the location and date. I display a grid that depends on hours and availability (this grid changes depending on data in a schedule table, the time of day it is now, and the status for the item). If the item is available during that time, a radio button displays. The user then enters an ID, and selects a time. When they click the button, a record is added for the user into the reservations. I've got all that working; but after the reservation is added, it doesn't display in the grid until the page is refreshed.

I tried Response.Redirect, calling the page and passing the two necessary pieces of information in the QueryString, but that doesn't seem to be the way to go.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.