asp.net怎么用控件实现加减乘除
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
新建aspx页面:代码如下:
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="+" Value="+"></asp:ListItem>
<asp:ListItem Text="-" Value="-"></asp:ListItem>
<asp:ListItem Text="*" Value="*"></asp:ListItem>
<asp:ListItem Text="/" Value="/"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="计算值" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
在Button1的单击事件下写后台代码:
protected void Button1_Click(object sender, EventArgs e)
{
string value = "";
float numA = Convert.ToSingle(TextBox1.Text);
float numB = Convert.ToSingle(TextBox2.Text);
switch (DropDownList1.SelectedValue)
{
case "+":
value = (numA+numB).ToString();
break;
case "-":
value = (numA - numB).ToString();
break;
case "*":
value = (numA * numB).ToString();
break;
case "/":
value = (numA / numB).ToString();
break;
}
Label1.Text = TextBox1.Text + DropDownList1.SelectedValue + TextBox2.Text + "=" + value;
}
运行效果如下: