简体   繁体   English

如何在 ASP.NET 中在运行时添加用户控件?

[英]How to add user control on run time in ASP.NET?

How do I add a user control on page load dynamically in ASP.NET?如何在 ASP.NET 中动态添加页面加载上的用户控件?

I have a div with ID="contentData" and few controls我有一个ID="contentData"和几个控件的 div

  • one.ascx一个.ascx
  • two.ascx二.ascx
  • three.ascx三.ascx

Now I have created a page default.aspx , which may get parameters in query string, in one of the following ways:现在我创建了一个页面default.aspx ,它可以通过以下方式之一获取查询字符串中的参数:

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在你的aspx

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

</div>

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

// Load the User Control

Control uc = LoadControl("~/MyUserControl.ascx");

// Add the User Control to the Controls collection

Page.Controls.Add(uc);

for more details go thru this link - An Extensive Examination of User Controls http://msdn.microsoft.com/en-us/library/ms972975.aspx有关更多详细信息,请访问此链接 - 用户控制的广泛检查http://msdn.microsoft.com/en-us/library/ms972975.aspx

Also do read the use of ~ tidle in .net http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.aspx也请阅读 .net 中 ~ tidle 的使用http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.aspx

Use the Page's LoadControl method to do this programmatically:使用页面的 LoadControl 方法以编程方式执行此操作:

http://msdn.microsoft.com/en-us/library/t9ecy7tf.aspx http://msdn.microsoft.com/en-us/library/t9ecy7tf.aspx

Also, if your intention is to add it to the div, make sure you make that div a server control by adding runat="server" in the markup此外,如果您打算将其添加到 div,请确保通过在标记中添加 runat="server" 使该 div 成为服务器控件

I'm a little unclear what you're asking, so forgive me if this doesn't answer the question.我有点不清楚你在问什么,所以如果这不能回答问题,请原谅我。

Assuming the possible values are known in advance and that the number is modest -- like the three in your example -- the easiest thing to do is to include all the controls on the form with "visible=false", then in your Load method set visible to true on the one you want.假设可能的值是预先知道的,并且数量是适中的——就像你的例子中的三个——最简单的方法是用“visible=false”包含表单上的所有控件,然后在你的 Load 方法中在你想要的那个上设置可见为真。

If the number of possibilities is very large, or if it is dynamic so that you don't even know what the possibilities are at coding time, then you could put in a wrapper object, say a div, and add the items to its "controls" property.如果可能性的数量非常大,或者如果它是动态的,以至于您在编码时甚至不知道可能性是什么,那么您可以放入一个包装对象,比如一个 div,然后将项目添加到它的“控制”属性。 But I'd avoid this if at all possible because it would be very hard to maintain: future programmers won't even know what controls could possibly be on the screen.但我会尽可能避免这种情况,因为它很难维护:未来的程序员甚至不知道屏幕上可能有哪些控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM