2015-08-26 97 views
2

我有在DropDownListAsp.net下拉列表中選擇項目價值

<asp:DropDownList ID="Etkin_Drop" runat="server" OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged"> 
    <asp:ListItem Text="Seç" Value="-1" Selected="True"></asp:ListItem> 
    <asp:ListItem Text="Aktif" Value="1"></asp:ListItem> 
    <asp:ListItem Text="Deaktif" Value="0"></asp:ListItem> 
</asp:DropDownList> 

列表第一項的值SelectedItem一個問題是-1但是當我想在if語句來檢查其沒有工作

protected void Etkin_Drop_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (Convert.ToInt32(Etkin_Drop.SelectedItem.Value) == -1) 
    { 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Lütfen Bir Seçim Yapınız');", true); 
    } 
    else 
    { 
     Label4.Text = Etkin_Drop.SelectedItem.Value; 
    } 
} 

我不能確定問題

+0

你如何確定它不工作?你嘗試過調試嗎? – ssilas777

+0

你可以把一個斷點,並檢查你得到什麼值,而不是-1? –

+0

是的,我確認了,腳本管理員應該給出警告,但不會進入if語句 – OykunYenal

回答

4

添加AutoPostBack屬性您DropDownList和集此屬性爲True。像這樣:

<asp:DropDownList ID="Etkin_Drop" runat="server" 
    OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged" AutoPostBack="True"> 
+1

這是有道理的。 – Oluwafemi