简体   繁体   English

ASP.NET-动态控件值和回发

[英]ASP.NET - Dynamic Control Values & Postback

I'm trying to persist the contents of a textbox through a postback, and I've exhausted all of my effort but can't get it working right. 我正在尝试通过回发来保留文本框的内容,我已经花了所有力气,但无法正常工作。

What should happen is: 应该发生的是:

  1. User selects a radiobutton 用户选择一个单选按钮
  2. Depending which button was selection, a usercontrol is loaded to specify some data and a viewstate to say which enum type it's equivalent to. 根据选择哪个按钮,将加载一个用户控件以指定一些数据和一个视图状态,以说明其等效于哪种枚举类型。
  3. When they click save, if the UserControl is just a textbox input - the simplest), the contents are read and saved, then saved to the database with the format(the radiobutton choice) so they can be deserialized again later. 当他们单击“保存”时,如果UserControl只是一个文本框输入(最简单),则将读取并保存内容,然后使用格式(单选按钮选择)将其保存到数据库中,以便稍后可以对它们进行反序列化。
  4. The page posts back, and the value and format are read from the database, then the right control is loaded. 页面回发,并从数据库中读取值和格式,然后加载正确的控件。

The problem is - the first time the page posts back, it works. 问题是-页面第一次回发时,它可以工作。 Every other postback it resets to the default value of the textbox. 每隔回发一次,它将重置为文本框的默认值。

I have a very similar setup elsewhere, so I'm thinking it might be a minor thing I'd never think of. 我在其他地方也有非常相似的设置,所以我认为这可能是我从未想到的小事。 There's a lot of code, so it might be easier to talk about what to do (load the dynamic control, populate the values etc) rather than how to do it. 有很多代码,因此谈论做什么(加载动态控件,填充值等)比如何做起来容易。

There was actually a bug in my original code which meant it would never have in the way I was using the modified version. 我的原始代码中实际上存在一个错误,这意味着它永远不会像我使用修改后的版本那样。 Apparently state is restored in Page_Load, so any controls need to be initialized by to have their values restored. 显然状态已在Page_Load中恢复,因此需要初始化任何控件以恢复其值。

Creating the control in Page_PreLoad, then populating it after Page_Load solved the problem. 在Page_PreLoad中创建控件,然后在Page_Load解决问题后填充它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM