2013-03-04 136 views
0

我正在做一個迷你測驗,我被困在C#編碼中,檢查用戶從DropDownList中選擇哪個ListItem。如何檢查ListItem是否被選中?

  <li><b>What is 231 mod 55?</b> 
       <asp:Label ID="lblQuestionResult2" runat="server" Font-Bold="true" Font-Size="16px" /> 
       <br /> 
       <asp:DropDownList ID="DropDownList1" runat="server" Width="55px"> 
        <asp:ListItem>14</asp:ListItem> 
        <asp:ListItem>6</asp:ListItem> 
        <asp:ListItem>11</asp:ListItem> 
       </asp:DropDownList> 
      </li> 

我沒有工作。如何去檢查用戶選擇的內容?

if (ListItem.Equals(toString(11))) 
    { 
     lblQuestionResult2.ForeColor = System.Drawing.Color.Green; 
     lblQuestionResult2.Text = "Correct"; 
    } 
    else 
    { 
     lblQuestionResult2.ForeColor = System.Drawing.Color.Red; 
     lblQuestionResult2.Text = "Incorrect"; 
    } 
+0

檢查這一個out.http://stackoverflow.com/questions/1829935/get-all-selected-items-from-asp-net-listbox – 2013-03-04 06:17:15

回答

1

DropDownList控制已經是一個服務器側面控制。爲OnSelectedIndexChanged事件添加事件處理程序並對其進行處理。它應該看起來像

<asp:DropDownList ID="DropDownList1" 
     runat="server" Width="55px" AutoPostBack="true" 
     OnSelectedIndexChanged="OnComboSelectionChanged"> 

在後面的代碼,你可以添加這樣

protected void OnComboSelectionChanged(object sender, EventArgs e) 
{ 
    // Your code goes here. 
    string selectedValue = DropDownList1.SelectedValue; 

} 

處理程序請確保您使用

AutoPostBack="true" 
+0

只是好奇,爲什麼我需要AutoPostBack =「true」 – trama 2013-03-04 06:30:20

+0

@then你將如何獲得值的服務器?您需要將值發送給篩選器進行篩選。或者你可以使用jQuery與Ajax調用。 – 2013-03-04 06:34:03

+0

@trama'AutoPostback'指示用戶更改選擇時是否自動發送回服務器。 – 2013-03-04 06:37:26

0

有兩種方式來得到它:

//One way 
string selectedvalue = ddList.SelectedValue; 

//Second Way 
string selectedindex = ddList.SelectedItem.Text; 

的完整的例子:

我已經推杆一個DropDownListButton

<asp:DropDownList ID="ddList" runat="server"> 
     <asp:ListItem>14</asp:ListItem> 
     <asp:ListItem>6</asp:ListItem> 
     <asp:ListItem>11</asp:ListItem> 
</asp:DropDownList> 

<asp:Button ID="btnClick" runat="server" Text="Button" OnClick="btnClick_Click" /> 

,我開了一個方法當用戶點擊按鈕時觸發這些按鈕

protected void btnClick_Click(object sender, EventArgs e) 
{ 

    string selectedvalue = ddList.SelectedValue; 
    string selectedindex = ddList.SelectedItem.Text; 
} 
0

這是什麼可以幫助你

// ASPX側

<asp:DropDownList ID="DropDownList1" runat="server" Width="55px" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 
        <asp:ListItem>14</asp:ListItem> 
        <asp:ListItem>6</asp:ListItem> 
        <asp:ListItem>11</asp:ListItem> 
       </asp:DropDownList> 

// CS側

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (DropDownList1.SelectedValue=="11") 
     { 
      lblQuestionResult2.ForeColor = System.Drawing.Color.Green; 
      lblQuestionResult2.Text = "Correct"; 
     } 
     else 
     { 
      lblQuestionResult2.ForeColor = System.Drawing.Color.Red; 
      lblQuestionResult2.Text = "Incorrect"; 
     } 
    } 
+0

'DropDownList1.SelectedValue'返回一個字符串,所以我們可能能夠擺脫冗餘'.ToString()'方法調用。 – 2013-03-04 06:24:03

0
<asp:DropDownList ID="DropDownList1" runat="server" Width="55px" 
       onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 
       <asp:ListItem>14</asp:ListItem> 
       <asp:ListItem>6</asp:ListItem> 
       <asp:ListItem>11</asp:ListItem> 
      </asp:DropDownList> 

//背後

代碼
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (DropDownList1.SelectedIndex!=0) 
    { 
     //your code implementation for selected value 
    } 
} 
相關問題