基于代码隐藏 (VB.net) 动态放置用户控件

发布于 2024-12-17 21:55:11 字数 413 浏览 2 评论 0原文

我有一个用户控件,它本质上是一个主菜单。

我可以将其硬编码到我的 MasterPage 中,但我不希望这样,我希望能够将其动态地放置在 MasterPage 后面的代码中。

<controls:mainMenu ID='MainMenu1' runat='server' />

所以我想做的是类似的事情

if **condition is true ** _
  response.write('<controls:mainMenu ID='MainMenu1' runat='server' />')

当然,我知道这是行不通的,但是我如何根据母版页上代码后面的条件放置控件?

顺便说一句,我使用的是 VB,而不是 C#

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.

<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#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2024-12-24 21:55:11

您可以执行类似的操作 -

Dim myControl As Control = CType(Page.LoadControl("yourcontrol.ascx"), Control)
if **condition is true ** _ 
   Panel1.Controls.Add(myControl) 

您需要向页面添加一个 Panel 或 PlaceHolder 控件来保存您的控件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文