如何删除自定义控件,然后使用相同的ID重新添加 [英] How do I remove a custom control and add it again with the same id

查看:140
本文介绍了如何删除自定义控件,然后使用相同的ID重新添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你们中的一个能在这里帮助我.

我正在使用vb.net

我有一个网页,上面有一个用户控件.相当多的功能/子项等通过其ID进行引用(正如您所期望的那样)

一旦用户控件完成了所有需要的操作,对于我来说,删除该对象并重新加载它会容易得多,但是新控件将需要使用相同的ID.

所以我已经尝试过了:

Hi there, I''m hoping one of you guys can help me out here.

I''m using vb.net

I have a web page that holds a user control on it. Quite a few functions/subs etc. reference it by it''s ID (as you would expect)

Once the user control has done all it needs to, It would be much, much easier for me to remove the object and reload it, but the new control would need to use the same ID.

So I''ve tried this :

Dim Old_User_ctrl As Control
Old_User_ctrl= Me.FindControl("MyControl1")
divPlaceholder.Controls.Remove(Old_User_ctrl)

Dim NEW_User_ctrl As New Control
NEW_User_ctrl = LoadControl("~\Controls\MyControl.ascx")
NEW_User_ctrl.ID = "MyControl1"
divPlaceholder.Controls.Add(NEW_User_ctrl)



然而;一旦我继续运行我的代码,我发现我得到了一个错误:



However; once I continue running through my code, I''ve found that I get an error :

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Multiple controls with the same ID ''MyControl1'' were found. FindControl requires that controls have unique IDs.



谁能告诉我如何正确处理用户控制对象?

谢谢大家,
Wotney



Can anyone tell me how to properly dispose of the user control object ?

Thanks folks,
Wotney

推荐答案

Wontbey,

试试这个,

设置Old_User_ctrl =没什么
Hi Wontbey,

Try this,

Set Old_User_ctrl = nothing


不幸的是没有.
一旦我运行
Unfortunately not.
Once I run
Dim Old_User_ctrl As Control
Old_User_ctrl= Me.FindControl("MyControl1")
divPlaceholder.Controls.Remove(Old_User_ctrl)

Old_User_ctrl = Nothing



我破坏了代码,并使用即时窗口仍然可以访问控件,例如



I break the code and using the immediate window I can still get to the control eg.

?MyControl1.ID
"MyControl1"



后来在代码中,我得到与以前相同的错误...



and later in the code I get the same error as before...

Multiple controls with the same ID ''MyControl1'' were found.



但是-我现在使用另一种技术解决了我的问题.我基本上已经重置了所有现有控件的变量,子控件等.因此我可以像使用新控件一样使用它.

因此,我已经解决了我眼前的问题,但是如果任何人都可以回答我的原始问题,那将是以后的参考!!

谢谢:)



But - I have worked around my problem now using a different technique. I have basically reset all of the existing control''s variables, child controls etc. So I can use it like it was a fresh control.

So, I''ve solved my immediate problem, but if anyone can answer my original question - that''d be great for future reference !

Thanks :)


这篇关于如何删除自定义控件,然后使用相同的ID重新添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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