愚蠢的初学者问题...... [英] Stupid beginner question...

查看:58
本文介绍了愚蠢的初学者问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



每次点击Button1我想改变(增加)写在

上的数字吧,我尝试用这个简单的代码来做:


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

{


string a = u.ToString ();


Button1.Text = a;


u = u + 1;


}


首次点击我得到的初始值为0。写在Button1上,但是下一个

点击没有任何变化:0一直都待...


为什么?我必须做一些刷新吗?或者是什么?请帮忙...


谢谢。

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.

推荐答案

你好MonkeeFace,

我怀疑你的代码中的其他内容是不对的,因为click事件似乎没问题。


顺便说一句,因为除了将u的值转移到你可以做的按钮


Button1.Text = u.ToString();


u = u + 1也可以写得更容易作为

u ++;


你能告诉我们你的其他课程吗?


-

快乐编码!

Morten Wennevik [C#MVP]
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]


这是一个Web应用程序吗?如果是这样,你必须记住状态。

您的变量(u)在页面调用之间不会持久存在。每当你重新加载页面时,它就会从头开始。你必须在页面调用之间存储

变量的值,以便第二次加载页面时,它可以知道页面最后一次加载时你是1等等。


试试这个(原谅我,如果语法不正确,因为我不写c#):


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

{

int a = CType(Button1.Text,int);

a + = 1;

Button1.Text = a;

}


因为ASP.NET确实保留了web的值在页面之间进行表单控制

调用,只需根据显示在

按钮上的最后一项内容来增加数量。


" MonkeeFace" < GF ** @ vip.hr>在消息中写道

news:cg ********** @ bagan.srce.hr ...
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" <gf**@vip.hr> wrote in message
news:cg**********@bagan.srce.hr...

每次点击在Button1上我想更改(增加)写在
上的数字,我尝试使用这个简单的代码:

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

字符串a = u.ToString();

Button1.Text = a;

u = u + 1;

}

第一次点击我得到的初始值为0。写在Button1上,但是下一步
点击没有任何变化:" 0"一直待在那儿......

为什么?我必须做一些刷新吗?或者是什么?请帮忙......

谢谢。
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.



另外,如果你有一个Web应用程序,你可以使用会话和应用程序对象。





会话[" u]] = 0;

$ b加载页面时没有$ b(没有回发),然后是点击事件


int u =(int)Session [" u"];

Button1.Text = u.ToString();


Session [" u"] = ++ u; //在你之前注意++,而不是之后


-

快乐的编码!

Morten Wennevik [C#MVP]
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]


这篇关于愚蠢的初学者问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆