简体   繁体   English

asp net repeater单选按钮组名称和获取单选按钮值

[英]asp net repeater radio button group name and get radio button value

I have an aspx c# page for bank account numbers and credit card installment list in one page 我在一个页面中有一个用于银行帐号和信用卡分期付款列表的aspx c#页面

if user want to bank transfer it choose bank account else if user want to pay credit card installment it did it. 如果用户想要银行转账,则选择银行账户,如果用户想要支付信用卡分期付款就行了。

the problem is radio buttons group name. 问题是单选按钮组名称。 I didn't group the radio buttons and didn't get the selected radio button value. 我没有对单选按钮进行分组,也没有获得所选的单选按钮值。

Here is my codes. 这是我的代码。

This İs Bank Account List 这是Banks银行账户清单

<asp:Repeater ID="RptBankalar" runat="server">
                        <ItemTemplate>
                            <div class="BankaListBox">
                                <table width="100%" height="100" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                        <td width="200" align="center" valign="middle">
                                            <img alt="<%#Eval("BankName").ToString() %>" src="../../Files/<%#Eval("Logo").ToString() %>" /></td>
                                        <td>
                                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                <tr>
                                                    <td width="120" height="22"><strong>Account Owner</strong></td>
                                                    <td width="15"><strong>:</strong></td>
                                                    <td><%#Eval("AcOwner").ToString() %></td>
                                                </tr>
                                                <tr>
                                                    <td width="120" height="22"><strong>Branch No</strong></td>
                                                    <td width="15"><strong>:</strong></td>
                                                    <td><%#Eval("Branch").ToString() %></td>
                                                </tr>
                                                <tr>
                                                    <td width="120" height="22"><strong>Account Number</strong></td>
                                                    <td width="15"><strong>:</strong></td>
                                                    <td><%#Eval("AccountNum").ToString() %></td>
                                                </tr>
                                                <tr>
                                                    <td width="120" height="22"><strong>IBAN</strong></td>
                                                    <td width="15"><strong>:</strong></td>
                                                    <td><%#Eval("Iban").ToString() %></td>
                                                </tr>
                                            </table>
                                        </td>
                                        <td width="100" align="center" valign="middle">
                                            <asp:RadioButton ID="RadioBtn" Text='<%#Eval("id").ToString() %>' runat="server" />
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>

Same Page Credit card installment list 同一页面信用卡分期付款清单

<asp:Repeater ID="RptTaksitList" OnItemDataBound="RptTaksitList_ItemDataBound" runat="server">
                                            <ItemTemplate>
                                                <tr runat="server" id="arka_tr">
                                                    <td align="center" height="22" width="40" runat="server" id="td1">
                                                        <b style="display: none; visibility: hidden">
                                                            <asp:Literal ID="lt_id" Text='<%#Eval("BankaID").ToString() %>' runat="server"></asp:Literal>
                                                        </b>
                                                        <asp:Literal ID="lt_taksit_sayisi" Text='<%#Eval("TaksitSayisi").ToString() %>' runat="server"></asp:Literal></td>
                                                    <td width="150" runat="server" id="td2">
                                                        <asp:Literal ID="LblSepetTotal" runat="server" Text='<%#Session["SepetUrunToplami"] %>'></asp:Literal></td>
                                                    <td runat="server" id="td3"><b style="display: none; visibility: hidden">
                                                        <asp:Literal ID="lt_komisyon_orani" Text='<%#Eval("KomisyonOrani").ToString() %>' runat="server"></asp:Literal>
                                                    </b>
                                                        <asp:Literal ID="lt_toplam_tutar" runat="server" Text=''></asp:Literal>
                                                        <b style="display: none; visibility: hidden">
                                                            <asp:Literal ID="lt_alt_limit" Text='<%#Eval("AltLimit").ToString() %>' runat="server"></asp:Literal>
                                                        </b>
                                                    </td>
                                                </tr>
                                            </ItemTemplate>
                                        </asp:Repeater>

How can i get the values and grouping this radio buttons. 如何获取值并将此单选按钮分组。

Normally i did in classic asp but i change my coding language. 通常我在经典的asp中做过,但我改变了我的编码语言。

Kindly regards. 亲切的问候。

Thanks for your helps. 谢谢你的帮助。

i never help in Stack. 我从不在Stack中帮忙。 So i will contribute for the first time after been helped so many times by the community. 因此,在社区多次帮助之后,我将第一次做出贡献。

I hove you like. 我喜欢你。 I have a more simple solution. 我有一个更简单的解决方案。

Surround your radio button with some element (DIV) with a className that you might like just to pick all the radio in jQuery. 使用带有className的元素(DIV)围绕您的单选按钮,您可能只想在jQuery中选择所有无线电。

EDITED IN: The aspnet wasnt persisting the viewstate when i changed that way. 编辑:当我改变这种方式时,aspnet并没有持久于视图状态。 Now it works just fine :) 现在它工作得很好:)

$(document).ready(function () {
    $(".radiofix").each(function () {
        var objRadiosContainer = $(this);
        objRadiosContainer.find("input[type=radio]").each(function () {
            var objRadio = $(this);
            objRadio.change(function () {
                if(objRadio.get(0).checked)
                {
                    objRadiosContainer.find("input[type=radio]").each(function () {
                        if($(this).get(0).id != objRadio.get(0).id)
                        {
                            $(this).get(0).checked = false;
                        }
                    });
                }
            });
        });
    });
});

Let's take a look in the HTML file (or ASPX file): 我们来看看HTML文件(或ASPX文件):

<asp:Repeater ID="questions" runat="server">
        <ItemTemplate>
            <h3>%# getQuestionNumber() %>) <%# Eval("questionDescription").ToString() %></h3>
            <div class="nameOfTheClass">
                <asp:Repeater ID="answers" runat="server">
                    <ItemTemplate>
                        <asp:RadioButton ID="radioId" runat="server" />
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </ItemTemplate>

This you will work just fine. 这你工作得很好。 Just create some javascript file (.js) and call it, fixTheRadios.js and choose a name for "nameOfTheClass" that you like. 只需创建一些javascript文件(.js)并调用它,fixTheRadios.js并选择你喜欢的“nameOfTheClass”的名称。

Important: I didnt tested the code inside an update panel. 重要提示:我没有在更新面板中测试代码。 I suggest you do that. 我建议你那样做。

Here's how I do it. 这是我如何做到的。

In the markup I declare the radio button as server side html control: 在标记中,我将单选按钮声明为服务器端html控件:

<input id="rbMyRadio" type="radio"runat="server" class="myClass" />

And in the code access the value: 并在代码中访问值:

foreach (RepeaterItem item in RptBankalar.Items)
{
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        HtmlInputRadioButton rbMyRadio = (HtmlInputRadioButton)item.FindControl("rbMyRadio");
        if (rbMyRadio != null && rbMyRadio.Checked)
        {
            //Do tasks
        }
    }
}

Grouping is done using jquery in page head: 使用页眉中的jquery完成分组:

<script type="text/javascript">
    $(document).ready(function () {
        $('input[type=radio]').click(function () {
            var cname = $(this).attr('class');

            $('.' + cname).each(function () {
                $(this).prop('checked', false);
            });

            $(this).prop('checked', true);
        });
    });
</script>

And reference jquery in the head: 并在头部引用jquery:

<script src="Scripts/jquery-1.8.2.js"></script>

Very late answer but you can also try adding the radio buttons like this in the repeater: 答案非常晚,但您也可以尝试在转发器中添加这样的单选按钮:

        <asp:TemplateColumn>
            <ItemTemplate>
                <div style="text-align: center">
                    <input type="radio" id="rptMyRadio" runat="server" />
                </div>
            </ItemTemplate>
        </asp:TemplateColumn>

Then in a script block add: 然后在脚本块中添加:

        $('input[type=radio]').click(function () {
            $('input[type=radio]').removeAttr("checked");
            $(this).prop('checked', true);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM