0

I am making a mini quiz and I am stuck on the C# coding with checking which ListItem a user selected from a DropDownList.

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

What I have doesn't work. How do go about checking what the user selects?

    if (ListItem.Equals(toString(11)))
    {
        lblQuestionResult2.ForeColor = System.Drawing.Color.Green;
        lblQuestionResult2.Text = "Correct";
    }
    else
    {
        lblQuestionResult2.ForeColor = System.Drawing.Color.Red;
        lblQuestionResult2.Text = "Incorrect";
    }
trama
  • 1,271
  • 3
  • 14
  • 24

5 Answers5

1

Your DropDownList control is already a server side control. Add an event handler for the OnSelectedIndexChanged event and handle it. It should look like

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

In the code behind you can add a handler like this

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

}

Make sure you use

AutoPostBack="true"
James Poulose
  • 3,569
  • 2
  • 34
  • 39
  • Just curious, why do I need the AutoPostBack="true" – trama Mar 04 '13 at 06:30
  • @then how do you going to get the values to server ? you need to send the values to the seerver for filterining. or else you can use jquery with ajax calls. – Ravi Gadag Mar 04 '13 at 06:34
  • @trama 'AutoPostback' indicates whether a postback to the server will automatically occur when the user changes the selection. – James Poulose Mar 04 '13 at 06:37
  • @Ravi I am not sure i understood your question. If you are refering to all the items in the dropdown box, you can get it from the 'Items' property (which is a collection). – James Poulose Mar 04 '13 at 06:39
0

There is two ways to get it:

 //One way
 string selectedvalue = ddList.SelectedValue;

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

The full example:

I have putted a DropDownList and a 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" />

And i created a method which fired when the user clicks on the button

protected void btnClick_Click(object sender, EventArgs e)
{

     string selectedvalue = ddList.SelectedValue;
     string selectedindex = ddList.SelectedItem.Text;
}
Silagy
  • 3,053
  • 2
  • 27
  • 39
0

This MSDN page should help. Looks like what you need to do http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.selected.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

Kyle
  • 81
  • 5
0

this might help you

//aspx side

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

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";
        }
    }
AJ17
  • 308
  • 5
  • 17
  • 'DropDownList1.SelectedValue' returns a string, so we might be able to get rid of that redundant '.ToString()' method call. – James Poulose Mar 04 '13 at 06:24
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>

//The code behind

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedIndex!=0)
    {
        //your code implementation for selected value
    }
}
janani
  • 123
  • 1
  • 2
  • 14