在Repeater控件单选按钮列表 [英] radio button list in repeater control

查看:103
本文介绍了在Repeater控件单选按钮列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网页Repeater控件。我需要在所有行上检查的单选按钮,剩下的单选按钮单选按钮(项目模板)必须选中。

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.

如何做到这一点?

在此先感谢,
Tor的

Thanks in advance, Tor

推荐答案

不幸的是,这是一个已知的bug(的http://支持。 microsoft.com/kb/316495 ),在一个中继器使用时,如预期GroupName属性不起作用。问题是,直放站实现,需要所有嵌套控件有一个的唯一的名称作INamingContainer接口的时候呈现出来的HTML。这将导致单选按钮,以便打破,因为他们的正常工作,他们必须具备的相同名称

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.

有2,我已经遇到的变通办法:

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

1 - 第一个是客户端JavaScript的解决方案。它是由<一个提供href=\"https://groups.google.com/forum/#!topic/microsoft.public.dotnet.framework.aspnet/IqIZ_Tg-jbA\">Microsoft支持。或更容易阅读的版本<一个href=\"http://weblogs.asp.net/joseguay/archive/2008/07/24/having-radiobuttons-on-a-repeater-or-datalist.aspx\">here.
的指令如下。在HEAD部分包括以下JavaScript:

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;
}

现在的功能需要被链接到单选按钮中的中继器的OnDataItemBound事件。替换单选按钮与您的单选按钮控件的名称和RadioGroup中与您所选择的组名:

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 - 第二个解决方案是使用从单选继承的自定义用户控件服务器端的解决方案。本教程和源$ C ​​$ C可以在这里下载:<一href=\"http://www.$c$cproject.com/KB/webforms/How_group_RButtons.aspx\">http://www.$c$cproject.com/KB/webforms/How_group_RButtons.aspx

这篇关于在Repeater控件单选按钮列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆