2010-07-23 66 views
0

当我试图绑定数据列表中的单选按钮时,它变成了多选,因为即使当我使用GroupName相同时,它的名称属性也会变得不同。与DataList控件绑定时无法使用单选按钮

我该如何让它充当单选按钮。

<asp:DataList ID="dlRoomNo" runat="server" RepeatColumns="4"> 
     <ItemTemplate> 
      <div class="orboxfour"> 
       <ul class="boxfour"> 
        <li> 
         <asp:RadioButton ID="rdoRoomNo" GroupName="roomNo" 
Text='<%#Eval("Room No")%>' runat="server" /> 
        </li> 
       </ul> 
      </div>      
     </ItemTemplate> 
    </asp:DataList> 

回答

0

有在回答this question了一些建议。


我用一点jQuery解决了它,虽然我做的方式可能不是最好的方法!

在我的标记,我有

function SetUniqueRadioButton(current) 
{ 
    $('input:radio').attr('checked', false); 

    current.checked = true; 
} 

一个脚本块,然后在ItemDataBound事件

String uniqueRadioButtonScript; 
RadioButton radioButton; 

uniqueRadioButtonScript = "javascript:SetUniqueRadioButton(this);"; 

if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    radioButton = (RadioButton)e.Row.FindControl("MyRadioButton"); 

    radioButton.Attributes.Add("onclick", uniqueRadioButtonScript) 
} 
0

最好的选择是连接脚本单选按钮在我的代码隐藏像这样: 1.添加脚本

function fnrad(rbtn) { 


      var radioList = document.getElementsByTagName("input"); 

      for (var i = 0 ; i < radioList.length; i++) { 

       if (radioList[i].type == "radio") { 

        radioList[i].name = 'a'; 
        radioList[i].setAttribute("Checked",""); 

       } 
      } 

      rbtn.setAttribute("Checked", "checked"); 
     } 

Datalist会是这样的:

<asp:DataList ID="dlEmails" RepeatLayout="Flow" runat="server"> 
     <HeaderTemplate> 
      <table> 
       <tr> 
        <th>Select Email Address </th> 

        <th>Status</th> 

       </tr> 
     </HeaderTemplate> 
     <ItemTemplate> 

      <tr> 
       <td> 

        <asp:RadioButton ID="rbtnSelect" Text='<%#Eval("Emails") %>' onclick='fnrad(this);' GroupName="a" Checked='<%#Eval("Primary") %>' runat="server" /><br /> 
        (<asp:Label ID="lblId" runat="server" Text='<%#Eval("Verified") %>'> </asp:Label>) 
       </td> 

       <td valign="middle"> 
        <asp:Label ID="Label2" Style="display: none;" runat="server" Text='<%#Eval("Id") %>'> </asp:Label> 
        <asp:Label ID="Label1" runat="server" Text='<%#Eval("Main") %>'> </asp:Label> 

       </td> 

      </tr> 


     </ItemTemplate> 
     <FooterTemplate> 
      </table> 

     </FooterTemplate> 
    </asp:DataList>