如何在asp.net中默认选择一个单选按钮

时间:2015-11-05 08:18:44

标签: c# asp.net

我有一个单选按钮,我想默认默认选中第一个项目。我该怎么做呢?下面是单选按钮的源代码。

<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
     <asp:ListItem Value="High">High</asp:ListItem>
     <asp:ListItem Value="Mid">Mid</asp:ListItem>
     <asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>

2 个答案:

答案 0 :(得分:8)

  

ASPX加价

<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
     <asp:ListItem Value="High" Selected="True">Select</asp:ListItem>
     <asp:ListItem Value="High">High</asp:ListItem>
     <asp:ListItem Value="Mid">Mid</asp:ListItem>
     <asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>                                                                       

答案 1 :(得分:7)

  

通过ASPX标记选择默认值:

<asp:RadioButtonList ID="radRiskLevel" runat="server" RepeatDirection="Horizontal">
     <asp:ListItem Value="High" Selected="True">Select</asp:ListItem>
     <asp:ListItem Value="Mid">Mid</asp:ListItem>
     <asp:ListItem Value="Low">Low</asp:ListItem>
</asp:RadioButtonList>
  

通过C#codebehind选择默认值:

if (!IsPostBack) // prevent from selecting default value during postback
{
    if (radRiskLevel.SelectedIndex == -1) //-1 is the indication of none selected
    {   
        radRiskLevel.SelectedIndex = 0; // the index of items in radiobutton in the list you want to select
        //radRiskLevel.Items.FindByText("Select").Selected = true;  //can also be selected by text, need to pass text name as parameter 
        //radRiskLevel.Items.FindByValue("High").Selected = true;  //can also be selected by value, need to pass value name as parameter
    }
}