0

我有 2 个文件,Test.aspx 和 MyControl.ascx。

在 Test.aspx 的 html 中:

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

在 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();
    }
}

我想从 MyControl.ascx.cs 中的代码隐藏中获取<MyTemplate>标记 ( ) 的内容。<div>sample text, other controls</div>

谁能帮我?谢谢。

4

1 回答 1

2

You would need to make the div a server side control as so:

<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:

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

Again, this depends on how is the div rendered. 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.

于 2013-05-23T02:36:26.033 回答