在 ASP.NET 中以编程方式创建用户控件实例

Create a usercontrol instance programmatically in ASP.NET(在 ASP.NET 中以编程方式创建用户控件实例)

本文介绍了在 ASP.NET 中以编程方式创建用户控件实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要动态添加的 UserControl.我试图关注这篇 MSDN 文章,但没有任何成功......http://msdn.microsoft.com/en-us/library/c0az2h86.aspx

UserControl 基本上是一个图片库,它根据 ID 加载一些图片.我的想法是将此 ID 作为属性提供.然后,当我创建控件的实例时,我可以设置此 ID 并将其添加到表单中.

我在将使用它的 .aspx 页面中添加了对控件的引用,如下所示:

<%@参考控件="~/PictureGallery.ascx" %>

在 UserControl 中,我添加了这样的 ClassName:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"Inherits="PictureGallery" ClassName="PictureGallery" %>

当我尝试像文章建议的那样在 .aspx.cs 中创建一个实例时,Dim gallery As ASP.PictureGallery,我得到一个类型 ASP.PictureGallery 未定义".

文章提到了一个命名空间,ASP,我尝试将它导入到 .aspx.cs,但没有成功.因此,我无法获得对 UserControl 的引用.

如何解决?

解决方案

听起来您混淆了使用 UserControl 的两种不同方式.

一种方法是在页面上注册控件,以便您可以在设计时将其放入页面中,例如

<asp:PictureGallery id="myGallery" runat="server" MyProperty="SomeValue"></asp:图片库></div>

第二个是在运行时以编程方式(或动态)将其添加到您的代码中的页面中.如果是这样,那么您需要使用示例中提到的 LoadControl 函数.如果您这样做,则不需要在 aspx 文件中注册控件.例如

将画廊作为 PictureGallery = LoadControl("~/PathToControl/gallery.ascx")图库.MyProperty = "SomeValue"placeHolder.controls.add(图库)

编辑
后面代码中控件的类名是什么...类似这样的:

部分公共类 MyControlsClassName继承 System.Web.UI.UserControl

这是你声明它时需要使用的类型.它可能在命名空间内吗?

I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success.... http://msdn.microsoft.com/en-us/library/c0az2h86.aspx

The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.

I added a reference to the control in the .aspx page that will use it, like this:

<%@ Reference Control="~/PictureGallery.ascx" %>

And in the UserControl I added a ClassName like this:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>

When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".

The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck. So, I'm not able to get a reference to the UserControl.

How can it be fixed?

解决方案

It sounds like you are confusing two separate ways of working with a UserControl.

One way is to register the control on your page so that you can put it into the page at Design Time e.g.

<div>
    <asp:PictureGallery id="myGallery" runat="server" MyProperty="SomeValue">  
    </asp:PictureGallery>
</div>

The second is programmatically (or dynamically) adding it into the page at runtime in your code behind. If so, then you need to use the LoadControl function which is mentioned in the sample. You do not need to register the control in the aspx file if you do this. e.g.

Dim gallery as PictureGallery = LoadControl("~/PathToControl/gallery.ascx")
gallery.MyProperty = "SomeValue"
placeHolder.controls.add(gallery)

edit
What is the class name of the control in the code behind...something like this:

Partial Public Class MyControlsClassName
    Inherits System.Web.UI.UserControl

That is the type you need to use when you declare it. Is it within a namespace perhaps?

这篇关于在 ASP.NET 中以编程方式创建用户控件实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 ASP.NET 中以编程方式创建用户控件实例

基础教程推荐