Button Click counter + textbox

I’m new to C# and visual studio, I started learning recently…at the moment I’m trying to make one button that counts the number of clicks and a text box which you can type in any number and it will be reflected in the button counter while adding 1, right now it works but I want the button to add 1 every time you click, here is my code,

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

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

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.Text = (TextBox1.Text);
            btn.Text = (Int32.Parse(btn.Text) + 1 ) .ToString();
            
           

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            string var;
            var = TextBox1.Text;

        }
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to separate the counter on the button from the value that you get from the textbox and recalculate the button text using the updated values.

In an ASP.NET, the class instance of a page is recreated whenever an event happens that requires a callback to the server. You can see this behavior easily if you put a breakpoint inside the page constructor.

So you need something that keeps around the state of your counter between each click. There are many possibilities, hidden fields, static variables or session variables.

A possible approach using a static variable:

public partial class ButtonCounter : System.Web.UI.Page
{
    // keeps the count of the clicks on the button
    private static int btnCounter;

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        btnCounter++;
        Button btn = (Button)sender;
        btn.Text = (Int32.Parse(TextBox1.Text) + btnCounter ) .ToString();
    }

But static variables have some troublesome aspects in ASP.NET apps. They are shared between all users, so a new user will see the counter value reached from the clicks from another user. If, for any reason, the IIS server needs to recycle the application pool, the static variables are lost and they restart from 0.

The Session instead is a memory area assigned to the current user where one can store values or references to information that needs to be kept around until the session ends. So a solution through a Session variable could be

    protected void Button1_Click(object sender, EventArgs e)
    {
        int btnCounter = 0;
        if(Session["counter"] != null)
            counter = Int32.Parse(Session["counter"]);
        counter++;
        Session["counter"] = btnCounter;

        Button btn = (Button)sender;
        btn.Text = (Int32.Parse(TextBox1.Text) + btnCounter ) .ToString();
    }

A simpler solution is through the use of an HiddenField control where you keep the counter and use it more or less like the Session example above.

    protected void Button1_Click(object sender, EventArgs e)
    {
        int btnCounter = Int32.Parse(hiddenCounter.Text);
        btnCounter++;
        hiddenCounter.Text = btnCounter.ToString();

        Button btn = (Button)sender;
        btn.Text = (Int32.Parse(TextBox1.Text) + btnCounter ) .ToString();
    }

This will work because ASP.NET keeps a ViewState for all the controls in your page reinitializing them with the previous values (and this is not so good as it seems because it augments the information that needs to be exchanged between the server and the client)

If you really still want to work with ASP.NET it is of uttermost importance to understand the ASP.NET Page Life cycle but remember that this is an obsolete technology for Microsoft. All the new enhancements are in the realm of ASP.NET Core and the new programming models like ASP.NET MVC and ASP.NET Razor pages.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x