web page has a button1 and a label1
sub button1_click()
dim count as integer
count=count+1
label1.text=str(count)
end sub
when i press button1 several times,but the label1.text always is "1"
why,i need help,thanks a lot
Hi ,
wehnever you click the button, the count is declared. so the default value for integer is 0.
So its reseted to 0.
for this you need to do the below one..
sub button1_click()
if(lbl.text == "")
{
lbl.text = "1";
}
else
{
lbl.text = convert.ToInt32(lbl.text) + 1;
}
end sub
In your Code value of count will not persist in between the multiple Postback.
If you want to persist value , your have multiple options ...
- Use Session.
- Use Cookie.
- Use Viewstate.
Note: you can add property for Count that will set/get value from either Session/Cookie Or viewstate.
yugiant:
when i press button1 several times,but the label1.text always is "1"
above posts are right alternatives...
if i define count outside the sub,the label1.text always is "1"
now i work out my problem,i set a hidden label to store the count's value
so may be use session,viewstate are better ways to resolve my problem
thanks a lot
yugiant:
if i define count outside the sub,the label1.text always is "1"
yes...
if you define count outside the sub... then that is now "Page Level Variable"...
so whenever you press any button means whenever any postpack occurs then also count (or any "Page level variable") will be redeclared and initialized to 0. that is why label1.text is showing "1" in this case also...
0 comments:
Post a Comment