Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在GridView中的RadioButtonList中预先选择值_C#_Asp.net - Fatal编程技术网

C# 如何在GridView中的RadioButtonList中预先选择值

C# 如何在GridView中的RadioButtonList中预先选择值,c#,asp.net,C#,Asp.net,这可能是一个愚蠢的问题,但如何根据现有数据预先选择RadioButtonList值 我在aspx文件中有以下代码: <asp:TemplateField ItemStyle-CssClass="ItemCommand" > <HeaderTemplate></HeaderTemplate> <ItemTemplate> <asp:RadioButtonList runat="server" ID="rbLev

这可能是一个愚蠢的问题,但如何根据现有数据预先选择
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
没有
SelectedValue
属性,设置
DataValueField
没有效果,我无法逐个设置值(使用类似于:
Selected=''
),因为数据绑定发生在列表中的不是特定项目。

请尝试使用
ListItem
Selected属性,如下所示

 <asp:RadioButtonList runat="server" ID="rbLevel" RepeatLayout="Flow" RepeatDirection="Horizontal" >
            <asp:ListItem Text="Read" Value="0" Selected ="True"></asp:ListItem>
            <asp:ListItem Text="Edit" Value="1"></asp:ListItem>
        </asp:RadioButtonList>
如果所选项目依赖于数据源,则在数据绑定后,您可以找到该项目并将选择设置为以下形式的代码隐藏

rbLevel.Items.FindByValue(searchText).Selected = true; 
你可以用

rbLevel.SelectedIndex=1

可以为每个单选按钮分配id,然后使用

rbLevel.FindControl(“option2”)。Selected=true


希望这能起作用:)

使用GridView的
RowDataBound()
方法相应地设置单选按钮列表:

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

这是迄今为止我找到的最好的答案

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

我需要根据数据源中的数据预先选择值(有些行为0,有些行为1)。这是您需要选择的第一项吗?并非总是如此,正如我在问题中所说,这取决于我从数据库中获得的数据。您可以使用Gridview_RowDatabound事件根据数据源预先选择值。
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;
        }
    }
}