C# 以编程方式设置ASP:ContentPlaceHolder内容
以编程方式设置文件内容的最简单方法是什么?我想我必须调用Master.FindControl?如果你要向空白页添加控件,那么你就要执行page.controls.Add….no?如果你要向空白页添加控件,那么你就要执行page.controls.Add….no?如果你的页面继承自母版页,那么你的页面上应该有一个asp:Content控件,并带有一些id,像这样:C# 以编程方式设置ASP:ContentPlaceHolder内容,c#,asp.net,contentplaceholder,C#,Asp.net,Contentplaceholder,以编程方式设置文件内容的最简单方法是什么?我想我必须调用Master.FindControl?如果你要向空白页添加控件,那么你就要执行page.controls.Add….no?如果你要向空白页添加控件,那么你就要执行page.controls.Add….no?如果你的页面继承自母版页,那么你的页面上应该有一个asp:Content控件,并带有一些id,像这样: <asp:Content runat="server" ID="myContent" ContentPlaceHolderID=
<asp:Content runat="server" ID="myContent" ContentPlaceHolderID="masterContent">
</asp:Content>
如果您的页面继承自母版页,那么您的页面上应该有一个带有某些id的asp:Content控件,如下所示:
<asp:Content runat="server" ID="myContent" ContentPlaceHolderID="masterContent">
</asp:Content>
我使用一个自定义扩展方法,例如递归地搜索一个控件(占位符),通过Id找到您要查找的控件并返回它。然后,您可以根据需要填充返回的控件。在foreach循环中调用它,循环遍历要填充的控件列表
public static class ControlExtensions
{
/// <summary>
/// recursive control search (extension method)
/// </summary>
public static Control FindControl(this Control control, string Id, ref Control found)
{
if (control.ID == Id)
{
found = control;
}
else
{
if (control.FindControl(Id) != null)
{
found = control.FindControl(Id);
return found;
}
else
{
foreach (Control c in control.Controls)
{
if (found == null)
c.FindControl(Id, ref found);
else
break;
}
}
}
return found;
}
}
我使用一个自定义扩展方法,例如递归地搜索一个控件(占位符),通过Id找到您要查找的控件并返回它。然后,您可以根据需要填充返回的控件。在foreach循环中调用它,循环遍历要填充的控件列表
public static class ControlExtensions
{
/// <summary>
/// recursive control search (extension method)
/// </summary>
public static Control FindControl(this Control control, string Id, ref Control found)
{
if (control.ID == Id)
{
found = control;
}
else
{
if (control.FindControl(Id) != null)
{
found = control.FindControl(Id);
return found;
}
else
{
foreach (Control c in control.Controls)
{
if (found == null)
c.FindControl(Id, ref found);
else
break;
}
}
}
return found;
}
}
很抱歉,我忽略了我想要的是填充内容,而不是生成的实际控件填充任何特定内容?原始html或文本?仅文本,我在内容占位符之外有格式化html对不起,我忽略了我希望填充内容,而不是实际控件生成填充任何特定内容?原始html还是文本?纯文本,我在ContentPlaceholder之外有格式html我知道这是旧的,但从技术上讲它应该是新的HtmlGenericControldiv,HtmlContainerControl是抽象的,虽然您可以将div变量强制转换为该类型,但无法创建。@McGuireV10-谢谢……我很高兴我将webforms抛在了后面。我知道这是旧的,但从技术上讲,它应该是新的HtmlGenericControldiv,HtmlContainerControl是抽象的,无法创建,尽管您可以将div变量强制转换为该类型。@McGuireV10-谢谢……我很高兴我把webforms抛在了后面。