Groupname for Radiobutton kills Listener from Buttons?

G

Guest

I have the following problem (especially with the groupname-attribut of the
RadioButton-Control) when developing in Visual Studio 2005 Beta.

I load an ascx-control in an aspx-page. This ascx-control itself creates
dynamically many radiobuttons in this way:
foreach (DataRow radiobutton_item in rbl_values.Rows)
{
RadioButton single_radiobutton = new RadioButton();
single_radiobutton.ID = "idname";
single_radiobutton.GroupName = "groupname";
this.Page.Controls.Add(single_radiobutton);
}

When loading the aspx-Page (and its controls) everything is fine and
groupname works as it should (I could only select one of the group). But when
a postback to this site occurs it seems that the listener of the
submit_button (placed on the aspx-Page) is killed. When I do not define the
Groupname of the RadioButton the submit_button works as it should (listeners
are not killed?!) but I can select more than only one radiobutton as I don't
have a group which they belong to.

I tried it already with an alternative:

single_radiobutton.Attributes.Add("name", mygroupname) instead of
single_radiobutton.GroupName = "groupname"; (seen in
http://msdn.microsoft.com/newsgroup...79eba1-694a-431a-8ae4-a458895a37ec&sloc=en-us)

but this didn't help.

Do you knwo this issue?
 
G

Guest

Where is your call to build the button list?
Is this being triggered from the Page_Load? If so, did you do the IsPostBack
check?
 
G

Guest

Hi Curt,

I've added my minimalized version below.

I experimented a bit more and can now substantiate the error (when it occurs
and how to reproduce).

If I use the attached code as it is (the line "single_radiobutton.GroupName
= rbl.ID;" in the ascx-file "inputRadiobuttonlist.ascx" is not commented out)
and I take the following steps:
1) choose none of the radios
2) hit "submit"
--> button-method "Button1_Click" (from "default.aspx") is called correctly
(displays <DateTime.Now>)
3) choose one of the radios (only one possible)
4) hit "submit"
--> button-method "Button1_Click" (from "default.aspx") is NOT called

The same happens if close the site and reopen it and then choose at the
first step one radio (button-method won't be called).

If I comment only the line "single_radiobutton.GroupName = rbl.ID;" out
everything works fine (except that I can choose both radiobuttons) but the
button-method will be called everytime when hitting "submit".

File "Default.aspx" contains the following placeholder and button:

<form id="form1" runat="server">
<asp:placeHolder ID="ph" runat="server"></asp:placeHolder>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="submit" />
</form>

File "Default.aspx.cs":

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control c = (Control)this.LoadControl("inputRadiobuttonlist.ascx");

this.ph.Controls.Add(c);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("button method called "+
DateTime.Now.ToLongTimeString());
}
}

File "inputRadiobuttonlist.ascx":

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="inputRadiobuttonlist.ascx.cs" Inherits="inputRadiobuttonlist" %>
<tr><td colspan=3><asp:Label ID="rbl_text" runat="server"
Text="Label"></asp:Label>
<asp:CustomValidator ID="valid_rbl" runat="server"
ErrorMessage="CustomValidator"
EnableClientScript="False"></asp:CustomValidator></td>
</tr>
<tr><td colspan=3><asp:Table ID="rbl" runat="server">
</asp:Table></td></tr>

File "inputRadiobuttonlist.ascx.cs":

public partial class inputRadiobuttonlist : System.Web.UI.UserControl
{
private const string mandatory = "mandatory";

protected void Page_Load(object sender, EventArgs e)
{
this.rbl_text.ID = "snippetid";
this.rbl_text.Text = "snippet_text";
this.rbl.ID = "radiobuttons";

if (mandatory.IndexOf("mandatory") != -1)
{
this.rbl_text.Text += "*";

}

//just to have some radiobbutons to add
DataTable rbl_values = new DataTable();
rbl_values.Columns.Add("choice_text");
rbl_values.Columns.Add("choice_value");
DataRow first_row = rbl_values.NewRow();
first_row["choice_text"] = "yes";
first_row["choice_value"] = "y";
rbl_values.Rows.Add(first_row);
DataRow scnd_row = rbl_values.NewRow();
scnd_row["choice_text"] = "no";
scnd_row["choice_value"] = "n";
rbl_values.Rows.Add(scnd_row);


//adding the radiobuttons dynamically
TableRow single_row = new TableRow();
foreach (DataRow radiobutton_item in rbl_values.Rows)
{

TableCell single_cell0 = new TableCell();

RadioButton single_radiobutton = new RadioButton();

string radiobutton_id =
radiobutton_item["choice_value"].ToString();
single_radiobutton.ID = radiobutton_id;
//single_radiobutton.Attributes.Add("name", rbl.ID);
single_radiobutton.GroupName = rbl.ID;

single_cell0.Controls.Add(single_radiobutton);
single_row.Cells.Add(single_cell0);

TableCell single_cell1 = new TableCell();

Label radiobutton_text = new Label();
radiobutton_text.Text =
radiobutton_item["choice_text"].ToString();

single_cell1.Controls.Add(radiobutton_text);
single_row.Cells.Add(single_cell1);

}

rbl.Controls.Add(single_row);

this.rbl_text.ForeColor = System.Drawing.Color.Black;

this.valid_rbl.ErrorMessage = "";

}
}
 
G

Guest

Well, it comes out that a constant string
single_radiobutton.GroupName = "abcdef";
works instead of
single_radiobutton.GroupName = rbl.ID;

However, sometimes even the datatype 'string' doesn't work but 'int' does
it. So I coded this 'workaround':

string st_rblid = rbl.ID;
single_radiobutton.GroupName = st_rblid.GetHashCode().ToString();

And this finally works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top