开发者

radio button list in repeater control

I have a repeater control in my page. I need to have a radio button in all the rows(Item template) on checking an radio button the remaining radio buttons must unchecked.

How to do t开发者_运维百科his?

Thanks in advance, Tor


Unfortunately, it's a known bug ( http://support.microsoft.com/kb/316495 ) that the GroupName property doesn't work as expected when used in a Repeater. The problem is that the Repeater implements the INamingContainer interface which requires all nested controls to have a unique name when rendered out to HTML. This causes the radio buttons to break because in order for them to work properly they must have identical names.

There are 2 work-arounds that I've come across:

1 - The first is a client-side javascript solution. It was provided by Microsoft support. Or an easier to read version here. The instructions are as follows. Include the following javascript in the HEAD:

function SetUniqueRadioButton(nameregex, current)
{
      re = new RegExp(nameregex);
      for(i = 0; i < document.forms[0].elements.length; i++)
      {
            elm = document.forms[0].elements[i]
            if (elm.type == 'radio')
            {
                  if (re.test(elm.name))
                  {
                          elm.checked = false;
                  }
             }
      }
      current.checked = true;
}

Now the function needs to be linked to the Radio Buttons in the OnDataItemBound event of the repeater. Replace "RadioButton" with the name of your RadioButton control and "RadioGroup" with the GroupName you have chosen:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
      if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
      RadioButton rb = (RadioButton) e.Item.FindControl("RadioButton");
      string script = "SetUniqueRadioButton('Repeater1.*RadioGroup',this)";
      rb.Attributes.Add("onclick", script);
}

2 - The second solution is a server-side solution using a custom usercontrol that inherits from RadioButton. The tutorial and source code can be downloaded here: http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx


Just add a OnCheckedChanged event to the radio button, loop all the radiobuttons in the repeater to uncheck them. You can use UpatePanel if you do not want postback.

.aspx

<asp:Repeater ID="Repeater1" runat="server" >
    <ItemTemplate>
        <asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_OnCheckedChanged" AutoPostBack="true" />
    </ItemTemplate>
</asp:Repeater>

.cs

protected void RadioButton1_OnCheckedChanged(object sender, EventArgs e)
{
    foreach (RepeaterItem item in Repeater1.Items)
    {
        RadioButton rbtn = (RadioButton)item.FindControl("RadioButton1");
        rbtn.Checked = false;
    }
    ((RadioButton)sender).Checked = true;
}


I know this is old, but the reason for the buttons not working is because the name attribute is being overwritten. If using jQuery, you could assign the radio button to a class then set the name attribute to override the new name.

$('.MyRadioClass').attr("name","MyFixedName");


Another simpler alternative, where no fancy formatting is required, is to use a RadioButtonList:

<asp:RadioButtonList ... />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜