Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a text box and a boton when i insert some int in the text box and the value is below 100 after clicking the button again i can insert some value there this value sholu below 100 and the summetion of the 2 value should not be greter than 100 ,unless the total value goes to 100 he can enter the values
Posted

Try this solution
Write below code on your code page
C#
private static int sum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sum = 0;
        }
    }

    protected void BtnGO_Click(object sender, EventArgs e)
    {
        if ((sum + Convert.ToInt16(txtInput.Text)) == 100)
        {
            sum = sum + Convert.ToInt16(txtInput.Text);
            txtInput.Enabled = false;
            lblMessage.Text = "Great ! Sum is 100 now";
        }
        else if ((sum + Convert.ToInt16(txtInput.Text)) > 100)
        {
            lblMessage.Text = "Sum exceeds 100 try to enter lower value";
        }
        else
        {
            sum = sum + Convert.ToInt16(txtInput.Text);
            txtInput.Text = string.Empty;
            lblMessage.Text = "Sum is " + sum + ". Still below 100 enter some more value to make 100";
        }
    }

Write below code in your source page
ASP.NET
<asp:textbox id="txtInput" runat="server"  />
        <asp:button id="BtnGO" runat="server" text="Add Value" onclick="BtnGO_Click" />
        <asp:label id="lblMessage" runat="server" />
 
Share this answer
 
v2
Comments
Member 11970398 2-Nov-15 2:55am    
how to do it in jquery
You can use Jquery Also for that
C#
$(document).ready(function () {
           $("#BtnGO").click(function () {
               var sum = parseInt($("#HFSum").val());
               var input = parseInt($("#txtInput").val());
               if (sum + input == 100) {
                   $("#lblMessage").text("Great ! sum is 100 now");
               }
               else if ((sum + input) > 100) {
                   $("#lblMessage").text("Sum exceeds 100 try to enter less value");
               }
               else {
                   $("#HFSum").val(sum + input);
                   $("#lblMessage").text("Sum is " + $("#HFSum").val() + " still below 100 enter more value to make 100");
                   $("#txtInput").val('');
               }
           });

       });


Source Code
XML
<asp:TextBox id="txtInput" runat="server"  />
     <asp:Button id="BtnGO" runat="server" text="Add Value" />
       <asp:HiddenField ID="HFSum" runat="server" Value="0" />
     <asp:Label id="lblMessage" runat="server" />
 
Share this answer
 
Comments
Member 11970398 2-Nov-15 4:37am    
thanx
done
[no name] 2-Nov-15 4:50am    
No issue. Will you please mark it as answer if you found it really helps you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900