如何在 ASP.NET 中添加用户对运行时的控制? [英] How to add user control on run time in ASP.NET?

查看:13
本文介绍了如何在 ASP.NET 中添加用户对运行时的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ASP.NET 中动态添加页面加载用户控件?

How do I add a user control on page load dynamically in ASP.NET?

我有一个带有 ID="contentData" 和几个控件的 div

I have a div with ID="contentData" and few controls

  • one.ascx
  • 两个.ascx
  • 三个.ascx

现在我创建了一个页面default.aspx,它可以通过以下方式之一获取查询字符串中的参数:

Now I have created a page default.aspx, which may get parameters in query string, in one of the following ways:

default.aspx?val=one  
default.aspx?val=two  
default.aspx?val=three

我从

Request.QueryString["val"]

现在如何在其中加载特定控件?

Now how do I load specific control in this?

<div ID="controlData"></div>

推荐答案

在你的aspx中

<div id="div1" runat="server">

</div>

在 Page_Load 中

In Page_Load

    UserControl uc = (UserControl)Page.LoadControl("test.ascx");
    div1.Controls.Add(uc);

您需要做的就是通过添加 runat="server" 来绑定您的 div 服务器,并在代码隐藏中使用 Page.LoadControl 出去并获取您的用户控件,然后使用 div 的 Controls.Add 将其添加到您的 div

All you need to do is make your div server bound by adding runat="server", and in the codebehind use Page.LoadControl to go out and fetch your usercontrol and then add it to your div using the div's Controls.Add

这篇关于如何在 ASP.NET 中添加用户对运行时的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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