2017-06-15 192 views
0

我想做第一項dropdownlist不可選以下是我的代碼。第一項dropdownlist項目不可選asp.net

<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px"> 
    <asp:ListItem>Select One</asp:ListItem> 
    <asp:ListItem>Bug</asp:ListItem> 
    <asp:ListItem>Enhancement</asp:ListItem> 
    <asp:ListItem>Tasks</asp:ListItem> 
    <asp:ListItem>New Feature</asp:ListItem> 
</asp:DropDownList> 

請幫助我該怎麼做。

回答

2

你也可以讓它在代碼中禁用落後於頁面加載事件

protected void Page_Load(object sender, EventArgs e) 
{ 
    IssueList.Items[0].Attributes["disabled"] = "disabled"; 
} 

也使您的第一個項目選擇

    <asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px"> 
         <asp:ListItem Selected="true" Value="">Select One</asp:ListItem> 
         <asp:ListItem>Bug</asp:ListItem> 
         <asp:ListItem>Enhancement</asp:ListItem> 
         <asp:ListItem>Tasks</asp:ListItem> 
         <asp:ListItem>New Feature</asp:ListItem> 
        </asp:DropDownList> 
+0

它的工作,但是當我把這個代碼所需的字段驗證器的下拉列表不起作用,即使當我還沒有選擇任何值 –

+0

仍然相同的問題 –

+0

@RamiFar我可以看到你的Validator定義? – jekcom

2

您可以添加禁用attribte.try本:

<asp:DropDownList runat="server" ID="id"> 
    <asp:ListItem Text="Test" Value="value" disabled="disabled" /> 
</asp:DropDownList> 
+0

其工作。但是當我運行應用程序時,顯示的項目是Bug not Select one。當我刪除列表時選擇一個是完全禁用的,但我希望它在運行時必須顯示請選擇不是Bug。 –

1

您可以添加一個'空'價值物品:

<asp:DropDownList ID="IssueList" runat="server" Height="24px" Style="margin-left: 0px"> 
    <asp:ListItem Selected="True"></asp:ListItem> 
    <asp:ListItem>Select One</asp:ListItem> 
    <asp:ListItem>Bug</asp:ListItem> 
    <asp:ListItem>Enhancement</asp:ListItem> 
    <asp:ListItem>Tasks</asp:ListItem> 
    <asp:ListItem>New Feature</asp:ListItem> 
</asp:DropDownList> 

,那麼你會檢查

if (string.IsNullOrEmpty(IssueList.SelectedValue)) 
+0

我需要把這張支票和我需要添加的內容添加進去if語句 –

+0

我沒有源代碼代碼,所以我不能說出你需要做什麼以及需要做什麼。 –

1

您可以使用屬性爲殘疾人和選擇組合。這可能有助於解決您的問題。

<asp:ListItem Selected="True" disabled="disabled">Select One</asp:ListItem> 
        ^^^^^^^   ^^^^^^^ 
+0

錯誤***禁用***是無效屬性 –

+0

我認爲這可能是警告消息。你有沒有嘗試運行你的應用程序? –