为什么在按钮按下并转储变量内容时触发Load_Page()? [英] Why does Load_Page() fire on a button press and dump variable contents?

查看:99
本文介绍了为什么在按钮按下并转储变量内容时触发Load_Page()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ASP.NET 2.0中创建了一个应用程序,该应用程序从SQL表加载其内容.它显示得足够好,但是当用户在页面上按下(以编程方式生成的)按钮时,Load_Page()再次运行,但是变量的内容已删除!

(这特别糟糕,因为该按钮应该提示将某些信息转储回SQL并创建一个依赖于这些内容的文件.)

为什么此按钮会导致Page_Load()再次触发?我应该怎么做才能防止这种情况发生,或者至少要确保不丢弃可变内容?

请让我知道我是否遗漏了任何有助于诊断此问题的信息.

I have created an app in ASP.NET 2.0 that loads its contents from a SQL table. It displays them well enough, but when the user bonks a (programmatically-generated) button on the page, Load_Page() runs again, but with the contents of the variables removed!

(This is especially bad, since the button is supposed to prompt dumping some information back into SQL and creating a file that depends on those contents.)

Why would this button cause Page_Load() to fire again? What can I do to prevent it, or at least to make sure that variable contents aren''t dumped?

Please let me know if there''s any information I''ve left out that might help with diagnosing this problem.

推荐答案

PageLoad事件被触发,因为控件生成了一个回发请求,即ASP.NET服务器控件(如按钮)的工作方式.由于控件是通过编程方式创建的,因此需要再次重新创建控件,以及以这种方式创建的任何其他控件.这就是它的工作方式,没有解决方法.在PageLoad事件中,应使用IsPostBack属性确定它是在处理初始加载还是回发.
The PageLoad event is being fired because the control has generated a postback request, that''s how ASP.NET server controls, like buttons, work. Because the control has been created programatically it needs to be recreated once again, along with anything else created in this manner. That''s the way it works, there is no work-around. Within the PageLoad event you should used the IsPostBack property to determine whether it is handling an initial load or a postback.


谢谢.这样可以很好地保持变量状态,就像声明名称空间的全局变量为静态一样.您是否知道为什么按钮的click事件没有触发? (即使在调试模式下运行它,并在单击处理程序中的第一个命令处放置一个断点,也表明它无法按预期运行.)页面只是重新加载.
Thank you. That works well to maintain variable state, as does declaring the namespace''s global variables to be static. Do you have any idea why the button''s click event is not firing? (Even running it in Debug mode and placing a breakpoint at the very first command in the click handler shows that it''s not working as desired.) The page simply reloads.


这篇关于为什么在按钮按下并转储变量内容时触发Load_Page()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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