用c# addbutton命令创建一个下拉列表

本文关键字:一个 下拉列表 addbutton 命令 创建 | 更新日期: 2023-09-27 18:09:38

我试图创建一个下拉列表与asp。一旦用户单击下拉列表,他应该能够选择加号或减号。由于某种原因,代码不能工作。谁能告诉我发生了什么事?

文本框示例

<script src="Scripts/jquery-1.10.2.js"></script>
    <script src="Scripts/jquery.validate.min.js"></script>
    <script src="Scripts/jquery.validate.js"></script>
    <script src="Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="Scripts/jquery.validate.unobtrusive.min.js"></script>
    <script runat="server">
        protected void AddButton_Click(Object sender, EventArgs e)
        {
            int Answer;
            if(DropDownList1.SelectedItem.Text == "+")
                     {Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text);
                     AnswerMessage.Text = Answer.ToString();}
            else if (DropDownList1.SelectedItem.Text == "-")
                   {
                    Answer = Convert.ToInt32(Value1.Text) - Convert.ToInt32(Value2.Text);
                    AnswerMessage.Text = Answer.ToString();
                   }

        }
    </script>
</head>
<body>
  <form id="form1" runat="server">
    <h3> TextBox Example </h3>
    <p>
        <asp:TextBox ID="Value1" Columns="2" MaxLength="3" Text="1" runat="server"/>
        +<select id="Select1">
            <option></option>
            </select>
            <asp:DropDownList ID="DropDownList1" runat="server">
               <asp:ListItem Text="Default text" Value="Default value" />
                 <asp:ListItem Text="+"  > +   </asp:ListItem>
                 <asp:ListItem  Text="-"  > - </asp:ListItem>

            </asp:DropDownList>

        <asp:TextBox ID="Value2" Columns="2" MaxLength="3" Text="1" runat="server"/>
        =
        <asp:Label ID="AnswerMessage" runat="server"/>
    </p>
    <p>
        <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/>
    </p>
    <p>
        <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1"
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"
             runat="server"/>
        <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2"
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"
             runat="server"/>
        <asp:RangeValidator ID="Value1RangeValidator" ControlToValidate="Value1"
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
             runat="server"/>
        <asp:RangeValidator ID="Value2RangeValidator" ControlToValidate="Value2"
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"    
             runat="server"/>
    </p>
      <asp:scriptmanager id="ScriptManager1" runat="server"/>

  </form>
</body>
</html>

用c# addbutton命令创建一个下拉列表

有几个问题

  1. 你的HTML格式不正确,下拉列表应该在选择标签
  2. 之外
<select id="Select1">
   <option></option>
 </select>
 <asp:DropDownList ID="DropDownList1" runat="server">
      <asp:ListItem Text="Default text" Value="Default value" />
      <asp:ListItem text="+"></asp:ListItem>
      <asp:ListItem text ="-"></asp:ListItem>
 </asp:DropDownList>
  1. 设置+和-符号作为你的ListItem的文本属性

  2. 你的If语句里面的按钮点击事件应该有"=="操作符而不是"="