简体   繁体   English

从自定义控件的代码隐藏中检索aspx的html内容

[英]Retrieving html content of an aspx from the codebehind of a custom control

I have 2 files, Test.aspx and MyControl.ascx. 我有2个文件,Test.aspx和MyControl.ascx。

In Test.aspx's html: 在Test.aspx的html中:

<MyControl>
  <MyTemplate>
     <div>sample text, other controls</div>
 </MyTemplate>
</MyControl>

In MyControl.ascx.cs: 在MyControl.ascx.cs中:

[ParseChildren(ChildrenAsProperties=true)]
[PersistChildren(true)]
public class MyControl:Control
{
    [Browsable(true)]
    public ITemplate MyTemplate{ get; set; }
    protected override void OnLoad(EventArgs e)
    {
        //get the template html, but how to get???
        var templateHtml = this.MyTemplate.ToString();
    }
}

I want to get the content of the <MyTemplate> tag ( <div>sample text, other controls</div> ) from codebehind within MyControl.ascx.cs. 我想从MyControl.ascx.cs中的代码隐藏中获取<MyTemplate>标记( <div>sample text, other controls</div> )的内容。

Who can help me? 谁能帮我? Thanks. 谢谢。

You would need to make the div a server side control as so: 您需要将div为服务器端控件,如下所示:

<MyControl>
  <MyTemplate>
     <div runat="server" id="divContent">sample text, other controls</div>
 </MyTemplate>

Now, in your code behind, depending on what kind of control is this and how is that div rendered, you should be able to do something like this: 现在,在后面的代码,这取决于什么样的控制,这是怎样的是,DIV渲染,你应该能够做这样的事情:

var innerHtml = yourControl.FindControl("divContent").InnerHtml;

Again, this depends on how is the div rendered. 同样,这取决于div的呈现方式。 If it's inside a row (assuming your control is a list of some kind), then you would need to get a reference to the row and then call FindControl("divContent") on the row. 如果它在一行内(假设您的控件是某种类型的列表),那么您需要获取对该行的引用,然后在该行上调用FindControl("divContent")

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

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