1

私は RadListView (Telerik) がある ASP.NET で作業しています。すべての RadListView のアイテム内には、2 つのラジオ ボタンを持つ RadioButtonList があります。私がする必要があるのは:

  • ページが初めてロードされるとき、2 つのラジオ ボタンのいずれかがデフォルトで選択されている必要があります。
  • ポストバック時に、ユーザーが他のものを選択したことを確認する必要があります (CustomValidator で実行しようとしています)。
  • ポストバックでは、RadioButtonLists のステータスを保持する必要があります。

どうすればそれを行うことができますか?

ここに私のコードの一部があります:

<telerik:RadListView ID="rlvContracts" runat="server">
        <ItemTemplate>
            <fieldset style="margin-bottom: 30px;">
                    <table cellpadding="0" cellspacing="0">
                           [...]
                              <asp:RadioButtonList runat="server" EnableViewState="true" ID="rblContract" RepeatDirection="Horizontal">
                              <asp:ListItem Value="1" Text="Accept"></asp:ListItem>
                              <asp:ListItem Value="0" Text="I do not accept" Selected="True"></asp:ListItem>
                              </asp:RadioButtonList>
                           [...]
                              <!-- Custom Validator Here -->
                           [...]
                    </table>
                </fieldset>
        </ItemTemplate>
    </telerik:RadListView>

ヘルプ (チュートリアルへのリンクも含む) は高く評価されます

前もって感謝します、ダニエレ

4

1 に答える 1

2

最初のステップを実行するには、上記のコードに投稿したアイデア (選択した RadioButton の宣言的な設定) に従うか、次の行に沿って何かを実行してプログラムで設定することができます。

//MyRadListView is the name of the RadListView on the page
RadListView myListView = MyRadListView;
RadioButtonList myRadioButtonList = myListView.Items[0].FindControl("MyRadioButtonList") as RadioButtonList;
myRadioButtonList.SelectedIndex = 0;

ご覧のとおり、コントロールの Items コレクションを介して特定の RadListView Item にアクセスする必要があります。興味のある項目を取得したら、コントロールの ID を文字列として取得する FindControl() メソッドを使用できます。

検証部分に関しては、これは可能な実装です:

ASPX:

        <asp:CustomValidator ID="RadioButtonListValidator" runat="server" ControlToValidate="MyRadioButtonList"
           OnServerValidate="RadioButtonListValidator_ServerValidate"
           ErrorMessage="Please select I Accept">
        </asp:CustomValidator>

C#:

    protected void RadioButtonListValidator_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        RadListView myListView = MyRadListView;
        RadioButtonList myRadioButtonList = myListView.Items[0].FindControl("MyRadioButtonList") as RadioButtonList;
        myRadioButtonList.SelectedIndex = 0;

        if (myRadioButtonList.SelectedValue != "1")
        {
            e.IsValid = false;
        }
    }

PostBack で「I Accept」RadioButton が選択されていることを確認します。

于 2011-02-25T20:12:26.060 に答える