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

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

问题描述

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

我有 ID =contentData内容和几个控件的div


  • one.ascx

  • two.ascx

  • three.ascx

现在我已经创建了一个页面 Default.aspx的,这可能会在查询字符串参数,通过以下方式之一:

的Default.aspx?VAL =一个
Default.aspx的?VAL =两个
Default.aspx的?VAL =三个

我从

取值

 的Request.QueryString [VAL]

现在我怎么加载在这个特定的控制?

 < D​​IV ID =controlData>< / DIV>


解决方案

在你的aspx

 < D​​IV ID =DIV1=服务器>< / DIV>

在Page_Load中

 用户控件UC =(用户控件)Page.LoadControl(test.ascx);
    div1.Controls.Add(UC);

所有你需要做的是让你的分区服务器加入=服务器的约束,并在codebehind使用Page.LoadControl走出去,获取你的用户控件,然后使用div的它添加到您的div Controls.Add被

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

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

  • one.ascx
  • two.ascx
  • three.ascx

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

I am taking the value from

Request.QueryString["val"]

Now how do I load specific control in this?

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

解决方案

In your aspx

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

</div>

In Page_Load

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

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天全站免登陆