以编程方式将用户控件添加到页面,同时保留已存在的控件
我正在尝试在运行时将用户控件添加到 div 中。我可以毫无问题地添加控件,但它会覆盖以前添加的控件。
基本上,我正在尝试将乘客添加到旅行系统中 - 乘客详细信息位于用户控件中,我事先不知道会有多少人。我有一个添加新乘客按钮,该按钮应将新用户控件附加到 div 中,而不会覆盖以前的乘客。
代码是 c#/.net 4。
我尝试将控制数据保存到视图状态中,然后将其重新添加到新的视图状态中,但这也不起作用。这是我正在使用的代码片段
foreach (Control uc in p_passengers.Controls) {
Passenger p = uc as Passenger;
if (p != null) {
p.SaveValues();
}
}
,但是,p.SaveAs()(仅将控制值写入 ViewState)永远不会被命中。
我确定这只是一些愚蠢的事情,但有什么想法吗?
干杯,伙计们。
I am trying to add a user control into a div at runtime. I can add the control no probelem but it overwrites the previous control added.
Basically, I am trying to add passengers to a travel system - the passenger details are in the user control and I don't know in advance how many there will be. I have an add new passenger button which should append the new user control into the div without overwriting the previous passenger.
The code is c#/.net 4.
I have tried to save the control data into viewstate and re add it with the new one but that also doesn't work. Here is a snippet of the code I'm using
foreach (Control uc in p_passengers.Controls) {
Passenger p = uc as Passenger;
if (p != null) {
p.SaveValues();
}
}
however, p.SaveAs() (just writes the control values into ViewState) is never hit.
Im sure its just something stupid but any ideas??
Cheers guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否为每次回发重新创建所有动态控件?
请记住,每个回发都是 Page 类的一个新实例,并且您之前创建的任何控件都需要显式地重新创建。
更新
如果您在视图状态中有一个添加项目的列表,类似这样..
然后在您的点击处理程序中,您可以简单地添加到此列表:
然后覆盖 CreateChildControls
Are you re-creating all of your dynamic controls for every postback?
Remember each postback is a new instance of the Page class and any controls you previously created will need to be explicitly re-created.
Update
If you had a list of added items in viewstate, something like this..
Then in your click handler you could simply add to this list :
Then override CreateChildControls