---
title: "Repeater中radiobutton使用"
author: "haimingyoung"
date: "2013-06-27T17:41:47Z"
tags: []
categories: ["编程语言"]
slug: "repeater-radiobutton"
draft: false
lastmod: "2013-06-27T17:46:05Z"
id: 1895334
url: "https://haimingyoung.iteye.com/blog/1895334"
---

今天在编写代码时遇到Repeater控件中的Radiobutton(单选按钮)不能单选，是因为Repeater生成的Radiobutton

的Id是动态生成的，无法单选。我在网上找到一个解决办法，大家有遇到相同问题的，可以来看看。

 

 

```
<script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript">
        function selectSingleRadio(rbtn1, GroupName) {
            $("input[type=radio]").each(function (i) {
                if (this.name.substring(this.name.length - GroupName.length) == GroupName) {
                    this.checked = false;
                }
            })
            rbtn1.checked = true;
        }
    </script>
```

 

 

```
<asp:Repeater ID="r_economicList" runat="server">  
                    <ItemTemplate>  
                        <input type="radio" id="economic" name="economic" value='<%# Eval("Id") %>' runat="server"  
                            onclick="return selectSingleRadio(this,'economic');" />  
                            <%#Eval("EconomicName") %>  
                        &nbsp;&nbsp;&nbsp;&nbsp;  
                    </ItemTemplate>  
  </asp:Repeater>
```

 

 

```
            string economicId;
            foreach (RepeaterItem item in r_economicList.Items)
            {
                HtmlInputRadioButton economic = (HtmlInputRadioButton)item.FindControl("economic");
                if (economic.Checked)
                {
                    economicId = economic.Value.ToString();
                }
            }
```

 economicId就是我要的值