开发者

Radio buttons not working when binded with DataList control

When i tries to bind开发者_运维知识库 a radio button in a datalist it becomes multiselect as its name property becomes different even when i used GroupName to be same.

How can i make it act as radio button only.

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


There's a number of suggestions in the answers to this question.


I've solved it with a bit of jQuery, though the way I did it probably isn't the best way!

In my markup, I have a script block with

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

    current.checked = true;
}

and then attached the script to a radio button in my code-behind in the ItemDataBound event

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


the best option is like this: 1. Add script

 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 will be like this:

<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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜