I've got a web app in ASP.NET 2.0 in which I need to have a highly customized grid. One of the columns in the grid contains a radio button for each row.
I'm implementing it as a Repeater
control, with a div
in each ItemTemplate
. The problem is that the radio buttons (ASP:RadioButton
tags) are not being grouped like they should; selecting one of them doesn't deselect the rest. I've set the GroupName
property on them already, but I don't see that being rendered in the HTML anywhere via Firebug. A google search tells me that the "name" attribute on <input type='radio>
is what determines its group membership, but ASP is using that already as some kind of unique identifier. Each radio button looks something like this when rendered to HTML:
<input id="{asp_garbage_naming}_ctl01_rbFoo" type="radio"
name="ctl03$controlName$otherControlName$ctl01$name"
value="rbHost" checked="checked" />
Is there a way to make this work? O开发者_运维问答r am I going to have to provide radio button behavior on my own (javascript, etc)?
A coworker ran across this issue and implemented a jQuery solution. Here's an excerpt of it:
That gave me the radio button functionality that I wanted, but it prevented me from getting the selected radio button on postback. So I decided to just implement the radio button functionality manually.
var radios = $("input:radio");
radios.click(function() {
radios.removeAttr('checked');
$(this).attr('checked', 'checked');
return true;
});
Which gave me the correct functionality and I could still get the selected radio button and textbox on postback. Probably not the most elegant solution, but I couldn’t find any other way to do it.
Full post: Radio button within a repeater problem
In the server side do as follows:
protected void rptRepeaterName_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RadioButton rdbRadioButtonName = e.Item.FindControl("rdbRadioButtonName") as RadioButton;
Repeater rptRepeaterName = sender as Repeater;
rdbRadioButtonName.Attributes.Add("onclick", string.Format("return radioSelected('{0}', '{1}')", rptRepeaterName.ClientID, rdbRadioButtonName.ClientID));
}
In the javascript do as follows
function radioSelected(rptpricelevel, rdbPriceLevel)
{
for (cnt = 0; cnt<100; cnt++)
{
var rdbId = rptpricelevel;
if(cnt < 10)
{
rdbId = + '_ctl0' + cnt + '_rdbRadioButtonName';
}
else
{
rdbId = + '_ctl' + cnt + '_rdbRadioButtonName';
}
var rdb = document.getElementById(rdbId);
if(rdb != null)
{
if(rdbId != rdbPriceLevel)
{
rdb.checked = false;
}
}
}
}
Question? Do you need these radio buttons to be server controls (i.e. do you need the runat=server tag)? If not, you could simply have regular html radio buttons on the column and bind any properties to it using the <%#Eval("Property")%>
syntax. Just a thought
精彩评论