how can i show response of aspx.cs file in perticular html tag in the asp.net?

Viewed 37

as you can see here the addition of two numbers is showing on the top of the page but I want to show the addition of two numbers in the table tag so how can I do that? how can i show result of two number in the table tag what should i do?

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body >
    <form id="form1" runat="server">
        <div>
            <table id="tbl1" align="center">
                <tr>
                    <th>
                        Enter the Number1:
                        <asp:TextBox ID="textbox1" runat="server"></asp:TextBox>
                    </th>
                </tr>
                <tr>
                    <th>
                        Enter the Number2:
                        <asp:TextBox ID="textbox2" runat="server"></asp:TextBox>
                    </th>
                </tr>
                <tr>
                    <th>
                         <asp:Button ID="Button2" runat="server" Text="Add" OnClick="Button1_Click" />
                    </th>
                </tr>
            </table>
         
        </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System. Linq;
using System. Web;
using System.Web.UI;
using System. Web. UI.WebControls;

namespace sample1
{
    public partial class First: System.Web. UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int no1 = Convert.ToInt32(textbox1.Text);
            int no2 = Convert.ToInt32(textbox2.Text);
            int c = no1 + no2;
            Response.Write(c);
        }
    }
}
1 Answers

Several ways. Most easy is to drop in a control - with server side tag.

So, lets put the total into a Textbox3, and ALSO say into a "h3" tag.

so, this markup:

        <div style="text-align:center">

            <div style="width: 25em; margin-left: auto; margin-right: auto">
                <div style="text-align: right">
                    Enter the Number 1: 
                    <asp:TextBox ID="textbox1" runat="server"></asp:TextBox>
                    <br />
                    Enter the Number 2:
                    <asp:TextBox ID="textbox2" runat="server"></asp:TextBox>
                    <div style="border-top: 3px solid; margin-top: 5px; margin-bottom: 8px"></div>

                    <asp:Button ID="Button2" runat="server" Text="Add" OnClick="Button2_Click"
                        Style="margin-right: 25px" CssClass="btn" />
                    Total:
                    <asp:TextBox ID="textbox3" runat="server"></asp:TextBox>
                    <h3 id="MyH3" runat="server"></h3>
                </div>
            </div>
        </div>

And this code behind:

    protected void Button2_Click(object sender, EventArgs e)
    {
        int MyTotal = 0;
        MyTotal = Convert.ToInt32(textbox1.Text) + Convert.ToInt32(textbox2.Text);
        textbox3.Text = MyTotal.ToString();
        MyH3.InnerText = MyTotal.ToString();
    }

Output:

enter image description here

So, as a general rule, just add a "id" and a runat = server to the tag in question. It can then be used in code behind - even with inteli-sense.

Related