如何在母版页 cs 文件中实例化用户控件
我已在母版页的 contentplace 持有者中添加了 uc,但是如何在母版页 .cs 文件中实例化用户控件以使其在母版页加载时可见
I have added uc in contentplace holder of master page but how do you instantiate the user control in master page .cs file to make it visible when the master page loads
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我并不肯定,但我认为您需要使用 Page.LoadControl() 在运行时将用户控件正式加载到页面。
I'm not positive but I think you need to use the Page.LoadControl() to officially load a user control to a page at run time.
如果我没看错的话,您在母版页中有一个控件,并且需要从子页面引用它来更改它的可见性? 或者我把事情搞错了?
无论如何,这是我在 VB .Net 中通常的做法,移植应该不会太难;-)
在 aspx 页面中:
在隐藏代码中:
If I read this correctly, you have a control in a Master page and need to reference it from the child pages to change it's visibility? Or have I got things around the wrong way?
Anyway, here's how I commonly do it in VB .Net, it shouldn't be too hard to port ;-)
In the aspx page:
In the code-behind:
如果您将其添加到 ContentPlaceHolder,并且子页面将内容添加到该 ContentPlaceHolder - 您的用户控件将不再出现。 IOW,您添加的控件只是默认。
如果您希望它始终可见,只需将其添加到 ContentPlaceHolder 之外即可。
If you add it to a ContentPlaceHolder, and a child page adds content to that ContentPlaceHolder - you're user control will no longer appear. IOW, the controls you add are only the default.
Just add it outside of the ContentPlaceHolder if you want it always visible.