Loading ...

web page application | CodeAsp.Net

web page application

Posted in: ASP.NET Topics  »  Getting Started / General ASP.NET
Subscribe to RSS
  • saiyedyasir

    we want to make a page in which we declare two textbox.....

    i want to enter some integer value in that boxes......

    we make albel submitt.........

    when we submitted it then show the addition into a third textbox.............

    (is it possible without creating database)

     

     
  •   Answered
    raghav_khunger

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form runat="server">
        <asp:TextBox ID="txtBox1" runat="server"></asp:TextBox> <br />
        <asp:TextBox ID="txtBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnSum" runat="server" Text="Sum" OnClientClick="return Sum();" /><br />
        <asp:Label ID="lblSum" runat="server" Text=""></asp:Label>
        </form>
    </body>
    
    <script type="text/javascript">
    
        function Sum()
        {
            var txtBox1 = document.getElementById('<%= txtBox1.ClientID %>');
            var txtBox2 = document.getElementById('<%= txtBox2.ClientID %>');
            var lblSum = document.getElementById('<%= lblSum.ClientID %>');
    
            var sum = parseInt(txtBox1.value) + parseInt(txtBox2.value);
            lblSum.innerHTML = sum;
    
            return false;
        }
    
    </script>
    
    </html>
    

    <b>[<a href="https://mvp.support.microsoft.com/profile/Raghav">ASP.NET MVP</a>]  [<a href="https://www.microsoftcommunitycontributor.com">Microsoft® Community Contributor</a>] [<a href="http://www.dzone.com/aboutmvb">Dzone MVB</a>] [<a href="https://twitter.com/#!/RaghavKhunger">Follow me on Twitter</a>]</b> <br/><br/><b>[<a href="http://codeasp.net/wiki/view/10/how-to-ask-questions-on-forums">How to ask questions on forums</a>]</b> <br/><br/> "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous <br/> "Success is simply a matter of luck. Ask any failure." Anonymous
     
  •   Answered
    raghav_khunger

    Or with codebehind

    In ASPX:

     

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form runat="server">
        <asp:TextBox ID="txtBox1" runat="server"></asp:TextBox> <br />
        <asp:TextBox ID="txtBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnSum" runat="server" Text="Sum" OnClientClick="return Sum();" 
            onclick="btnSum_Click" /><br />
        <asp:Label ID="lblSum" runat="server" Text=""></asp:Label>
        </form>
    </body>
    
    </html>
    

     

     

    In Codebehind:

     

        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                
    
            }
    
            protected void btnSum_Click(object sender, EventArgs e)
            {
                lblSum.Text =(Convert.ToInt32(txtBox1.Text) + Convert.ToInt32(txtBox2.Text)).ToString();
            }
    
       }



     

    <b>[<a href="https://mvp.support.microsoft.com/profile/Raghav">ASP.NET MVP</a>]  [<a href="https://www.microsoftcommunitycontributor.com">Microsoft® Community Contributor</a>] [<a href="http://www.dzone.com/aboutmvb">Dzone MVB</a>] [<a href="https://twitter.com/#!/RaghavKhunger">Follow me on Twitter</a>]</b> <br/><br/><b>[<a href="http://codeasp.net/wiki/view/10/how-to-ask-questions-on-forums">How to ask questions on forums</a>]</b> <br/><br/> "Success does not come to those who wait . . . and it does not wait for anyone to come to it." Anonymous <br/> "Success is simply a matter of luck. Ask any failure." Anonymous
     
  •   Answered
    hajan

    Hello everyone (this is my first post here).

    The solution that @raghav provided is almost everything you need in order to help you understand how you can do SUM of two values using client-side (javascript) or server-side (C#.net code-behind) code.

    Moreover, I would like to add up few more lines of code in order to make some server-side validations.

            protected void btnSum_Click(object sender, EventArgs e)
            {
                int txt1 = 0; //declaring INT value txt1 - here I will store if number is found
                int txt2 = 0; //declaring INT value txt2 - 
                if (!String.IsNullOrEmpty(txtBox1.Text) && !String.IsNullOrEmpty(txtBox2.Text)) //checking if both textBoxes have some data, otherwise go to #10
                {
                    if (Int32.TryParse(txtBox1.Text, out txt1) && Int32.TryParse(txtBox2.Text, out txt2)) //Using Int32.TryParse, I'm trying to parse the data inside textboxes, if the value is in range of Int32 integer value, and its same for both textBoxes, return the number to the previously declared INT variables
                    {
                        lblSum.Text = Convert.ToString(txt1 + txt2); //SUM the values
                    }
                    else
                    {
                        Response.Write("Only numbers please!"); //if some non-numerical character is found, write error message
                        lblSum.Text = ""; //remove anything that may appear in the label
                    }
                }
                else Response.Write("Textboxes are required!"); // #10 - If one of the textBoxes is empty, Write message that Textboxes are required.
            }


    Read the comments, and you will understand all lines of the above code.

    Hope this helps.
    Regards,
    Hajan

       
    Don't forget to Mark As Answer the answers that helped you solve your issue
     
  • Page 1 of 1 (4 items)