Hi Chuck,
Thanks for posting here. As for the BUG you mentioned in ASP.NET server
control. I think we can use some clientside javascript to workaround it.
Since we can specify the GroupName for the RadioButton Control and it is
contained in a certain DataBind List control( such as Repeater, DataList or
DataGrid), though when rendered to client, the html source will be changed
because of the INamingContainer control will make all the control's id and
name unique, but they have some similiar features. Thus, we can make use of
this, first we loop through all the controls with the specified type(radio
or checkbox or...) , then use Regex to compare their name, if is what we
want, then uncheck it. At last, we check the current select one. For
example , here is a js function to perform the function:
function SetUniqueRadioButton(nameregex, current)
{
re = new RegExp(nameregex);
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements
if (elm.type == 'radio')
{
if (re.test(elm.name))
{
elm.checked = false;
}
}
}
current.checked = true;
}
And we register the "onclick" client event for the RadioButtons in the
certain Repeater(or DataList or..) such as:
private void rptMain_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
string script = "SetUniqueRadioButton('rptMain.*MyGroup',this)";
RadioButton rb = (RadioButton)e.Item.FindControl("rbSelect");
rb.Attributes.Add("onclick",script);
}
To make it clearly, here is a demo page I've made ,please refer to it if
you have anything unclear:
==================aspx page====================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>RadioColumn</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
function SetUniqueRadioButton(nameregex, current)
{
re = new RegExp(nameregex);
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements
if (elm.type == 'radio')
{
if (re.test(elm.name))
{
elm.checked = false;
}
}
}
current.checked = true;
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:Repeater id="rptMain" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"name") %>
:
<asp:RadioButton ID="rbSelect" Runat="server"
GroupName="MyGroup"></asp:RadioButton>
<br />
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
==============code behind page class=============
public class RadioColumn : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater rptMain;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind_Data();
}
}
protected void Bind_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("description");
for(int i=1;i<=10;i++)
{
DataRow row = tb.NewRow();
row["index"] = i.ToString();
row["name"] = "Name" + i.ToString();
row["description"] = "Description" + i.ToString();
tb.Rows.Add(row);
}
rptMain.DataSource = tb;
rptMain.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.rptMain.ItemDataBound += new
System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptMain_ItemDataBoun
d);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void rptMain_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
string script = "SetUniqueRadioButton('rptMain.*MyGroup',this)";
RadioButton rb = (RadioButton)e.Item.FindControl("rbSelect");
rb.Attributes.Add("onclick",script);
}
}
======================================================
Hope helps.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx