- First, add new Website, Open Visual Studio: Select > File > New > Website.
- Give the name of the Website like SampleWeb1.
- Open Solution Explorer and add Web Form by right clickng on the Website name > select Add > Add New Item >Select Web form (Default.aspx).
- Add Label on Default.aspx,
Eg: <asp:Label ID="Label1" runat="server" />
- Open Default.aspx.cs page and add following code on Page Load,
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Page.IsPostBack == false) {
- Label1.Text = DateTime.Now.ToString();
- }
- }
Now, we learn how to calculate the sum, multiplication, and division of 2 numbers:
- Add three textboxes and three buttons on default.aspx page,
Value1: <asp:TextBox ID="TextBox1" runat="server" />
Value2: <asp:TextBox ID="TextBox2" runat="server" />
Answer
- <asp:TextBox ID="TextBox3" runat="server" />
- <asp:Button ID="Button1" Text="Sum" runat="server" OnClick="calculate" />
- <asp:Button ID="Button2" Text="Mul" runat="server" OnClick="calculate" />
- <asp:Button ID="Button3" Text="Divide" runat="server" OnClick="calculate" />
- Add Following code in default.aspx.cs Page,
- protected void calculate(object sender, EventArgs e)
- {
- if (sender == Button1) {
- TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text));
- } else if (sender == Button2) {
- TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(TextBox2.Text));
- } else {
- TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text) / Convert.ToInt32(TextBox2.Text));
- }
- }