ASP.NET의 계산기 예제를 통해 정수<-->문자열 형변환과 메시지 출력과 switch 문 사용에 대해서 정리를 해 보았습니다.
aspx 페이지 소스
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> <style type="text/css"> .style1 { width: 47%; } .style2 { width: 360px; } .style3 { border-bottom-style: solid; } .style4 { width: 360px; border-bottom-style: solid; } </style> </head> <body> <form id="form1" runat="server"> <div> 계산기 예제 <table class="style1"> <tr> <td class="style2"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style4"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> <td class="style3"> <asp:Label ID="Label1" runat="server" Text="="></asp:Label> </td> </tr> <tr> <td class="style2"> <asp:Button ID="Button1" runat="server" Text="계산" onclick="Button1_Click" /> </td> <td> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> </table> </div> </form> </body> </html>
Button1_Click 버튼 클릭 이벤트 내용입니다.
protected void Button1_Click(object sender, EventArgs e) { string calop = TextBox3.Text; int result = 0 ; switch (calop) { case "*": result = int.Parse(TextBox1.Text) * int.Parse(TextBox2.Text); break; case "+": result = int.Parse(TextBox1.Text) + int.Parse(TextBox2.Text); break; case "/": result = int.Parse(TextBox1.Text) / int.Parse(TextBox2.Text); break; case "-": result = int.Parse(TextBox1.Text) - int.Parse(TextBox2.Text); break; default : string message = "연산자가 잘 못 입력되었습니다."; string sScript = "<script language=\"javascript\">\n"; sScript += "<!--\n"; sScript += " alert(\"" + message + "\");\n"; sScript += "//-->\n"; sScript += "</script>\n"; Response.Write(sScript); break; } TextBox4.Text = result.ToString(); }
|
|
|