[英]Dynamically place a user control based on Code Behind (VB.net)
I have a user control which is essentially a main menu. 我有一个用户控件,它实际上是一个主菜单。
I can place it into my MasterPage hard-coded, but I don't want that, I want to be able to dynamically place it with the code behind of the MasterPage. 我可以将其放入硬编码的MasterPage中,但我不希望这样,我希望能够将其与MasterPage后面的代码一起动态放置。
<controls:mainMenu ID='MainMenu1' runat='server' />
So what I am looking to do is something like 所以我想要做的是
if **condition is true ** _
response.write('<controls:mainMenu ID='MainMenu1' runat='server' />')
Of course, I know that won't work, but how would I place the control based on a condition in code behind on the master page? 当然,我知道那是行不通的,但是我如何根据条件在母版页上放置控件呢?
I'm using VB by the way, not C# 我正在使用VB,而不是C#
You could do something like this - 你可以做这样的事情-
Dim myControl As Control = CType(Page.LoadControl("yourcontrol.ascx"), Control)
if **condition is true ** _
Panel1.Controls.Add(myControl)
You'd need to add a Panel or PlaceHolder control to your page to hold your control. 您需要在页面上添加一个Panel或PlaceHolder控件以保存该控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.