.NETGURU
Stupid beginner question...
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.adonet.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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.

Reply to this message...
 
    
Patty O'Dors (VIP)
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.

"MonkeeFace" wrote:

[Original message clipped]

Reply to this message...
 
    
Scott M.
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.

"MonkeeFace" <Click here to reveal e-mail address> wrote in message
news:cgq3co$k7c$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Morten Wennevik
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

--
Happy coding!
Morten Wennevik [C# MVP]
Reply to this message...
 
    
Scott M.
But, in general, you should try to find a way that doesn't consume the
resources that Sessions and Application.Cache items do.

"Morten Wennevik" <Click here to reveal e-mail address> wrote in message
news:opsdf5tddrklbvpo@stone...
[Original message clipped]

Reply to this message...
 
    
tami
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)

"MonkeeFace" <Click here to reveal e-mail address> wrote in message
news:cgq3co$k7c$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Scott M.
> 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.

Reply to this message...
 
    
Peter van der Goes
"MonkeeFace" <Click here to reveal e-mail address> wrote in message
news:cgq3co$k7c$Click here to reveal e-mail address...
[Original message clipped]

u as:

private static int u = 0;

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Reply to this message...
 
    
Scott M.
But it wouldn't work if it is a web application.

"Peter van der Goes" <p_vandergoes@toadstool.u> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Peter van der Goes
"Scott M." <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

it posted to a group concerned with ASP.NET, so I wondering about the other
response assumptions of a web app.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Reply to this message...
 
    
MonkeeFace
[Original message clipped]

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.

Reply to this message...
 
    
Peter van der Goes
"MonkeeFace" <Click here to reveal e-mail address> wrote in message
news:cgsmm2$gma$Click here to reveal e-mail address...
[Original message clipped]

Now, Scott's comment on my suggestion *is* applicable. What I posted won't
work for you.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Reply to this message...
 
    
Morten Wennevik
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?

--
Happy coding!
Morten Wennevik [C# MVP]
Reply to this message...
 
 
System.EventArgs
System.Windows.Forms.Application




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification