如何访问ASP.NET控件的内容?

时间:2021-04-05 20:54:32

I was wondering if it is possible to do something like this:

我想知道是否有可能做这样的事情:

    <uc1:TestControl ID="TestControl1" runat="server">
        <div>More random HTML, etc...</div>
    </uc1:TestControl>

I got an error of "Type 'System.Web.UI.UserControl' does not have a public property named 'div'.". Doing a little research, I found I could add the following property to the server control:

我收到一个错误“类型'System.Web.UI.UserControl'没有名为'div'的公共属性。”做了一点研究,我发现我可以将以下属性添加到服务器控件:

[ParseChildren(false)]
public partial class TestControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Is it possible to read the content from an ASP.NET control?

是否可以从ASP.NET控件中读取内容?

Edit: Changed wording to reflect that I am curious if you can do this with either a server or user control

编辑:更改了措辞以反映我很好奇,如果您可以使用服务器或用户控件执行此操作

2 个解决方案

#1


4  

In a server control, you would create a property that implements ITemplate to contain that content. I'm not positive whether that is possible in a user control, but it may be.

在服务器控件中,您将创建一个实现ITemplate以包含该内容的属性。我不确定在用户控件中是否可行,但它可能是。

#2


0  

yeah it is possible

是的,这是可能的

check this MSDN Article about creating templated user controls, plus you can add [ParseChildren(false)] to the user control class so you can see them from the page holding them. http://msdn.microsoft.com/en-us/library/36574bf6.aspx

查看这篇关于创建模板化用户控件的MSDN文章,另外你可以将[ParseChildren(false)]添加到用户控件类中,这样你就可以从持有它们的页面中看到它们。 http://msdn.microsoft.com/en-us/library/36574bf6.aspx

hope this helps.

希望这可以帮助。

#1


4  

In a server control, you would create a property that implements ITemplate to contain that content. I'm not positive whether that is possible in a user control, but it may be.

在服务器控件中,您将创建一个实现ITemplate以包含该内容的属性。我不确定在用户控件中是否可行,但它可能是。

#2


0  

yeah it is possible

是的,这是可能的

check this MSDN Article about creating templated user controls, plus you can add [ParseChildren(false)] to the user control class so you can see them from the page holding them. http://msdn.microsoft.com/en-us/library/36574bf6.aspx

查看这篇关于创建模板化用户控件的MSDN文章,另外你可以将[ParseChildren(false)]添加到用户控件类中,这样你就可以从持有它们的页面中看到它们。 http://msdn.microsoft.com/en-us/library/36574bf6.aspx

hope this helps.

希望这可以帮助。