3

これはばかげた質問かもしれませんが、RadioButtonList既存のデータに基づいて値を事前に選択するにはどうすればよいですか?

aspx ファイル内に次のコードがあります。

<asp:TemplateField ItemStyle-CssClass="ItemCommand" >
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
         <asp:RadioButtonList runat="server" ID="rbLevel" RepeatLayout="Flow" RepeatDirection="Horizontal" >
            <asp:ListItem Text="Read" Value="0"></asp:ListItem>
            <asp:ListItem Text="Edit" Value="1"></asp:ListItem>
        </asp:RadioButtonList>
    </ItemTemplate>
</asp:TemplateField>

しかし、リストの値を設定できません。プロパティがなく、設定しRadioButtonListても効果がなく、特定のアイテムではなくリストでデータバインディングが発生するため、値を 1 つずつ設定することはできません (: のようなものを使用 )。SelectedValueDataValueFieldSelected='<%# ((Rights)Container.DataItem).Level == 1 %>'

4

4 に答える 4

3

使えます

rbLevel.SelectedIndex = 1;

また

各ラジオボタンにIDを割り当ててから使用できます

rbLevel.FindControl("option2").Selected = true;

これがうまくいくことを願っています:)

于 2013-05-28T09:11:47.223 に答える
3

GridView のRowDataBound()メソッドを使用して、それに応じて RadioButton リストを設定します。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {               
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButtonList rbl = (RadioButtonList)e.Row.FindControl("rbLevel");
            // Query the DataSource & get the corresponding data....
            // ...
            // if Read -> then Select 0 else if Edit then Select 1...
            rbLevel.SelectedIndex = 0;
        }
    }
于 2013-05-28T10:08:26.450 に答える
0

これは私が見つけた最高の答えです

protected void GridView1_DataBound(object sender, EventArgs e)
{
    foreach (GridViewRow gvRow in GridView1.Rows)
    {
        RadioButtonList rbl = gvRow.FindControl("rblPromptType") as RadioButtonList;
        HiddenField hf = gvRow.FindControl("hidPromptType") as HiddenField;

        if (rbl != null && hf != null)
        {
            if (hf.Value != "")
            {
                //clear the default selection if there is one
                rbl.ClearSelection();
            }

            rbl.SelectedValue = hf.Value;
        }
    }
}
于 2014-09-04T16:30:40.883 に答える