这段代码怎么识别运算的阿?里面好像没有运算法则什么的,连sum也没有啊,onclick="Add"事件中我把Add改成kk,然后再改void kk(object sender,EventArgs e)发现程序也能运行,那服务器是根据什么进行运算的?<script runat="server" Language="C#">
void Add(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)+
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
void Subtract(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)-
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
void Factor(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)*
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
void Ratio(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)/
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
</script>
<html>
<form runat="server">
<asp:textbox id="tbxInput1" runat="server"/>
<asp:button id="btnAdd" runat="server" text="+" onclick="Add"/>
<asp:button id="btnSubtract" runat="server" text="-" onclick="Subtract"/>
<br/>
<asp:textbox id="tbxInput2" runat="server"/>
<asp:button id="btnFactor" runat="server" text="X" Onclick="Factor"/>
<asp:button id="btnRatio" runat="server" text="/" Onclick="Ratio"/>
<br/>
<b>Answer=<asp:Label id="lblAnswer" runat="server"/></b>
</form>
</html>
void Add(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)+
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
void Subtract(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)-
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
void Factor(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)*
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
void Ratio(object sender,EventArgs e)
{
try
{
double d=Convert.ToDouble(tbxInput1.Text)/
Convert.ToDouble(tbxInput2.Text);
lblAnswer.Text=d.ToString();
}
catch
{
lblAnswer.Text="An error occurred";
}
}
</script>
<html>
<form runat="server">
<asp:textbox id="tbxInput1" runat="server"/>
<asp:button id="btnAdd" runat="server" text="+" onclick="Add"/>
<asp:button id="btnSubtract" runat="server" text="-" onclick="Subtract"/>
<br/>
<asp:textbox id="tbxInput2" runat="server"/>
<asp:button id="btnFactor" runat="server" text="X" Onclick="Factor"/>
<asp:button id="btnRatio" runat="server" text="/" Onclick="Ratio"/>
<br/>
<b>Answer=<asp:Label id="lblAnswer" runat="server"/></b>
</form>
</html>
程序已經寫的很清楚了,這里有個“+”呀。
同理,“-”“*”“/”都有啊。
Add只是个方法的名,你改成什么都行,只要调用的时候onclick=""里的名和他一样就行