Stupid beginner question...

  • Thread starter Thread starter MonkeeFace
  • Start date Start date
M

MonkeeFace

Hi,
With every click on Button1 i want to change (increase) number written on
it, and i try to do it with this simple code:

private void Button1_Click(object sender, System.EventArgs e)
{

string a=u.ToString();

Button1.Text=a;

u=u+1;

}

With first click i get initial value "0" written on Button1, but with next
clicks nothing changes: "0" stays all the time...

Why? Must I do some "refreshing" or what? Please help...

Thanks.
 
Hi MonkeeFace,

I suspect something else in your code is not right, as the click event seems ok.

btw since a has no purpose other than to transfer the value of u to the button you can just do

Button1.Text = u.ToString();

u=u+1 can also be written much easier as
u++;

Can you show us the rest of your program?
 
Is this a web application? If so, you must remember that the "state" of
your variable (u) is not persisted between page calls. Every time you
reload the page it starts from scratch. You must store the value of the
variable between page calls so that the second time the page is loaded, it
can know that the last time the page was loaded u was 1 and so on.

Try this (forgive me if the syntax isn't correct as I don't write c#):

private void Button1_Click(object sender, System.EventArgs e)
{
int a = CType(Button1.Text, int);
a +=1;
Button1.Text=a;
}

Since ASP.NET does persist the value of web form controls between page
calls, just increase the number based on the last thing that was showing on
the button.
 
Also, if you have a web application, you can use Session and Application objects.

Put

Session["u"] = 0;

when you load the page (no postback), then in the click event

int u = (int)Session["u"];
Button1.Text = u.ToString();

Session["u"] = ++u; // note the ++ before u, not after
 
But, in general, you should try to find a way that doesn't consume the
resources that Sessions and Application.Cache items do.
 
MonkeeFace said:
Hi,
With every click on Button1 i want to change (increase) number written on
it, and i try to do it with this simple code:

private void Button1_Click(object sender, System.EventArgs e)
{

string a=u.ToString();

Button1.Text=a;

u=u+1;

}

With first click i get initial value "0" written on Button1, but with next
clicks nothing changes: "0" stays all the time...

Why? Must I do some "refreshing" or what? Please help...

Thanks.
How is the variable u declared? Your code worked fin for me when I declared
u as:

private static int u = 0;
 
Scott M. said:
But it wouldn't work if it is a web application.
True, but I didn't see anything in the original post about web apps, nor was
it posted to a group concerned with ASP.NET, so I wondering about the other
response assumptions of a web app.
 
I didn't see anything in the original post about web apps, nor was
it posted to a group concerned with ASP.NET, so I wondering about the other
response assumptions of a web app.

I admit, my mistake: i have forgotten to mention it, but it is web
application and i forgotten to post my question on ASP.NET group... Sorry,
and thanks for your replies, ill try all of them and im sure something of it
will be the solution...

Thanks.
 
MonkeeFace said:
I admit, my mistake: i have forgotten to mention it, but it is web
application and i forgotten to post my question on ASP.NET group... Sorry,
and thanks for your replies, ill try all of them and im sure something of it
will be the solution...

Thanks.
No problem.
Now, Scott's comment on my suggestion *is* applicable. What I posted won't
work for you.
 
Probably due to the fact that the Text property of a button is a string,
while your variable is an integer. Convert to integer, add one, then convert
back to string.
 
hi
This happens because every button click sends the page to the server and
reloads the initial values
One way to do it is by defining the variable u as a control in the form ,
then the ViewState keeps it.
Other way is to define the variable u as static (shared in vb)
 
Other way is to define the variable u as static (shared in vb)

As I stated in an earlier reply in this thread, a static variable won't work
because each time the page is finished processing, all variable (except
session or cached) are lost.
 
Back
Top