0

我正在做一个小测验,我被困在 C# 编码上,检查用户从 DropDownList 中选择了哪个 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";
    }
4

5 回答 5

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"
于 2013-03-04T06:19:42.747 回答
0

有两种获取方式:

 //One way
 string selectedvalue = ddList.SelectedValue;

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

完整的例子:

我放了一个DropDownList和一个Button

 <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;
}
于 2013-03-04T06:15:00.807 回答
0

这个 MSDN 页面应该会有所帮助。看起来你需要做什么 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.selected.aspx?cs-save-lang=1&cs-lang=csharp#code -snippet-2

于 2013-03-04T06:17:04.367 回答
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";
        }
    }
于 2013-03-04T06:18:41.270 回答
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
    }
}
于 2013-03-04T06:32:01.397 回答