最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - How to find checked RadioButton inside Repeater Item? - Stack Overflow

programmeradmin1浏览0评论

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.

I could do this:

RadioButton checkedButton = null;

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

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

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.

I could do this:

RadioButton checkedButton = null;

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

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

Share Improve this question edited Nov 15, 2008 at 8:00 Alexander Prokofyev asked Nov 14, 2008 at 13:06 Alexander ProkofyevAlexander Prokofyev 34.5k33 gold badges100 silver badges118 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

You could always use Request.Form to get the submitted radio button:

var value = Request.Form["answerVariants"];

I think the submitted value defaults to the id of the <asp:RadioButton /> that was selected, but you can always add a value attribute - even though it's not officially an <asp:RadioButton /> property - and this will then be the submitted value:

<asp:RadioButton ID="answerVariantRadioButton" runat="server"
    GroupName="answerVariants" 
    Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"
    value='<%# DataBinder.Eval(Container.DataItem, "SomethingToUseAsTheValue")%>' />

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.

I'm pretty sure that the only thing you could use LINQ to Objects for here would be to take the conditions from within the foreach loop and move them to a where clause.

RadioButton checked = 
    (from item in answerVariantRepeater.Items
    let radioButton = (RadioButton)item.FindControl("answerVariantRadioButton")
    where radioButton.Checked
    select radioButton).FirstOrDefault();
发布评论

评论列表(0)

  1. 暂无评论