0

私はミニクイズを作成していますが、ユーザーが DropDownList から選択した ListItem をチェックする C# コーディングに固執しています。

            <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

入手方法は2つあります。

 //One way
 string selectedvalue = ddList.SelectedValue;

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

完全な例:

私はaDropDownListと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" />

そして、ユーザーがボタンをクリックしたときに起動するメソッドを作成しました

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 -スニペット-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 に答える