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";
}
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"
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;
}
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
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";
}
}
<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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.