Increment Decrement TextBox value on plus minus Button Click using C# and VB.Net

tareq16278
 
on Aug 05, 2022 12:43 AM
Sample_771232.zip
638 Views

button Click plus and minus textbox

textbox count + one

button Click1 textboxt number - 1

button Click2 textboxt number + +

how is possible

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <p>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            style="font-weight: 700; font-size: large" Text="-" />
        <asp:TextBox ID="TextBox1" runat="server" Width="49px"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
            style="font-size: x-large" Text="+" />
    </p>
</asp:Content>

 

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.Text = "1";
}

protected void Button1_Click(object sender, EventArgs e)
{

}

protected void Button2_Click(object sender, EventArgs e)
{

}
Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Aug 05, 2022 03:08 AM

Hi tareq16278,

please refer below sample.

HTML

<asp:Button ID="btnMinus" runat="server" OnClick="OnMinus"
    Style="font-weight: 700; font-size: x-large" Text="-" />
<asp:TextBox ID="txtNumber" runat="server" Width="49px"></asp:TextBox>
<asp:Button ID="btnPlus" runat="server" OnClick="OnPlus"
    Style="font-size: x-large" Text="+" />

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txtNumber.Text = "1";
    }
}

protected void OnMinus(object sender, EventArgs e)
{
    if (int.Parse(txtNumber.Text) > 0)
    {
        txtNumber.Text = (int.Parse(txtNumber.Text) - 1).ToString();
    }
}

protected void OnPlus(object sender, EventArgs e)
{
    txtNumber.Text = (int.Parse(txtNumber.Text) + 1).ToString();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        txtNumber.Text = "1"
    End If
End Sub

Protected Sub OnMinus(ByVal sender As Object, ByVal e As EventArgs)
    If Integer.Parse(txtNumber.Text) > 0 Then
        txtNumber.Text = (Integer.Parse(txtNumber.Text) - 1).ToString()
    End If
End Sub

Protected Sub OnPlus(ByVal sender As Object, ByVal e As EventArgs)
    txtNumber.Text = (Integer.Parse(txtNumber.Text) + 1).ToString()
End Sub

Screenshot