質問

次のように定義されたASPXページにRepeaterコントロールがあります:

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

時間内に1つのラジオボタンのみを選択できるように、トリックフォームを使用しましたこの記事

しかし、フォームが送信されたときに、どのラジオボタンがチェックされているかを判断したいのです。

これを行うことができます:

RadioButton checkedButton = null;

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

ただし、何らかの方法でもっと簡単にできることを願っています(おそらくLINQ to objectsを使用)。

役に立ちましたか?

解決

すでにクライアントでラジオボタンクリックイベントを処理するためにJavaScriptを使用しているため、非表示フィールドを選択した値で同時に更新できます。

サーバーコードは、非表示フィールドから選択した値にアクセスするだけです。

scroll top