用相同的ID的时候动态地添加用户控件到UpdatePanel的服务器控件错误状态 [英] Wrong state in Server Controls with same ID's when dynamically adding UserControl to UpdatePanel

查看:122
本文介绍了用相同的ID的时候动态地添加用户控件到UpdatePanel的服务器控件错误状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面就是我要做的。我有两个链接按钮和一个UpdatePanel(两个linkbuttons触发的UpdatePanel)的页面。我有一个有相同的ID标签2用户控件。当我点击第一个链接按钮,我的第一个用户控件添加到UpdatePanel中,并设置标签值datetime.now

Here is what I am trying to do. I have a page with two link buttons and an updatepanel (the two linkbuttons trigger the updatepanel). I have two usercontrols which have labels with same ID's. When I click the first link button, I add the first usercontrol to the updatepanel and set the label value to datetime.now

当我点击第二个链接按钮,我添加第二个用户控件,但我看到,从第一个控制标签的值在第二个用户控件的标签设置。如果ID的不同是没有问题的 - 但在我的情况下,用户控件正在由不同的团队开发的,我在我提到的方式整合它们 - 所以他们可能有相同的ID

when i click the second link button i add the second usercontrol but i see that the value of the label from the first control is set in the label in second user control. if the id's are different there is no problem - but in my case the usercontrols are being developed by different teams and I am integrating them in the way i mentioned - so they may have same ids.

我已经浏览各地,并试图提出各种建议,但我不能得到这个工作,任何帮助将大大AP preciated。

i have browsed all over and tried various suggestions but i cannot get this to work, any help will be greatly appreciated.

感谢

招聘塞缪尔

推荐答案

您的问题无关的UpdatePanel的,其实。

Your problem has nothing to do with the UpdatePanel, actually.

试想一下,如果你有一个3 LinkBut​​ton的,它什么也没做,但回发会发生什么。如果你点击了第一次的LinkBut​​ton会发生什么,用户控件出现了,然后你点击第三个呢?你期望看到什么?如果你认为你将再次看到用户控件,你错了。动态创建的控件必须创建​​每个请求,它们不会自动坚持。记得ViewState的页面上的控件的状态,不是控件本身是摆在首位 - 那是什么在aspx页面的标记一样。动态创建的控件明显的arent的标记,所以他们的arent自动重新创建。

Imagine what would happen if you had a 3rd LinkButton, which did nothing but postback. What would happen if you clicked the 1st LinkButton, the UserControl appeared, and then you clicked the 3rd one? What do you expect to see? If you think you will see the UserControl again, you are wrong. Dynamically created controls must be created every request, they don't stick around automatically. ViewState remembers the state of controls on the page, NOT what the controls themselves are in the first place -- thats what the markup in the aspx page does. Dynamically created controls obviously arent in the markup, so they arent automatically recreated.

您需要考虑控制两个请求之间的'跨界'一半的生命周期。它开始中途到一个请求,结束一半进入下一个。您需要保存哪些用户控制当前显示在任何一个隐藏字段或Page.ViewState值(不是用户控件本身提个醒,正是您所需要的任何信息来弄明白),然后重新从页面的onload的控制。如果你这样做 - 序列将是这样的:

You need to think of the lifecycle of a control as 'straddling' half way between two requests. It starts half way into one request and ends half way into the next. You need to save which user control is currently displayed in either a hidden field or a Page.ViewState value (not the user control itself mind you, just whatever information you need to figure it out), then reload that control from the page's OnLoad. If you do that -- the sequence will go like this:

(1)点击LinkBut​​ton1
(2)的UserControl1动态创建
(3)单击LinkBut​​ton2
(4)Page.OnLoad重新加载的UserControl1
(5)的UserControl1加载其回传的数据和视图状态
(6)LinkBut​​ton2的点击事件触发
(7)删除现有的UserControl1和动态添加UserControl2
(8)UserControl2可以有相同的ID,因为已经的UserControl1消耗的状态。

(1) Click LinkButton1 (2) UserControl1 dynamically created (3) Click LinkButton2 (4) Page.OnLoad reloads UserControl1 (5) UserControl1 loads its postback data and viewstate (6) LinkButton2's click event fires (7) Remove existing UserControl1 and add UserControl2 dynamically (8) UserControl2 can have the same ID, since UserControl1 already 'consumed' its state.

我建议你浏览我的系列文章在ASP.NET了解动态控制:
http://weblogs.asp.net/infinitiesloop/

I suggest you browse my series of articles on Understanding Dynamic Controls in ASP.NET: http://weblogs.asp.net/infinitiesloop/

这篇关于用相同的ID的时候动态地添加用户控件到UpdatePanel的服务器控件错误状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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