2015-11-05 174 views
0

我有一個單選按鈕,我想默認選中默認的第一項。我該如何去做呢?以下是我單選按鈕的源代碼。如何在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> 

回答

6

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

選擇默認經由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#代碼隱藏

選擇默認值:

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 
    } 
} 
相關問題