如何在中继器项中找到选中的 RadioButton? [英] How to find checked RadioButton inside Repeater Item?

查看:21
本文介绍了如何在中继器项中找到选中的 RadioButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASPX 页面上有一个 Repeater 控件,定义如下:

I have a Repeater control on ASPX-page defined like this:

<asp:Repeater ID="answerVariantRepeater" runat="server"
    onitemdatabound="answerVariantRepeater_ItemDataBound">
    <ItemTemplate>
        <asp:RadioButton ID="answerVariantRadioButton" runat="server"
            GroupName="answerVariants" 
            Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"/>
    </ItemTemplate>
</asp:Repeater>

为了允许及时选择一个单选按钮,我使用了一种技巧形式 这篇文章.

To allow select only one radio button in time I have used a trick form this article.

但是现在提交表单时,我想确定选中的是哪个单选按钮.

But now when form is submitted I want to determine which radio button is checked.

我可以这样做:

RadioButton checkedButton = null;

foreach (RepeaterItem item in answerVariantRepeater.Items)
{
    RadioButton control=(RadioButton)item.FindControl("answerVariantRadioButton");
    if (control.Checked)
    {
        checkedButton = control;
        break;
    }
}

但希望它可以以某种更简单的方式完成(可能通过 LINQ to 对象).

but hope it could be done somehow simplier (maybe via LINQ to objects).

推荐答案

由于您已经使用 javascript 来处理客户端上的单选按钮单击事件,您可以同时使用所选值更新隐藏字段.

Since You are using javascript already to handle the radio button click event on the client, you could update a hidden field with the selected value at the same time.

然后,您的服务器代码将只访问隐藏字段中的选定值.

Your server code would then just access the selected value from the hidden field.

这篇关于如何在中继器项中找到选中的 RadioButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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