[英]Can I dynamically add HTML within a div tag from C# on load event?
Mind you, I am using master pages, but can I locate a div within the page and throw some html in there? 请注意,我正在使用母版页,但是我可以在页面中找到一个div并在那里抛出一些HTML吗? Thanks. 谢谢。
You can add a div with runat="server" to the page: 您可以在页面中添加一个带有runat =“server”的div:
<div runat="server" id="myDiv">
</div>
and then set its InnerHtml property from the code-behind: 然后从代码隐藏中设置其InnerHtml属性:
myDiv.InnerHtml = "your html here";
If you want to modify the DIV's contents on the client side, then you can use javascript code similar to this: 如果你想在客户端修改DIV的内容,那么你可以使用类似这样的javascript代码:
<script type="text/javascript">
Sys.Application.add_load(MyLoad);
function MyLoad(sender) {
$get('<%= div.ClientID %>').innerHTML += " - text added on client";
}
</script>
Use asp:Panel
for that. 使用asp:Panel
。 It translates into a div. 它转化为div。
Remember using 记得用
myDiv.InnerHtml = "something";
will replace all HTML elements in myDIV. 将替换myDIV中的所有HTML元素。 you need to append text to avoid that.In that this may help 你需要附加文字以避免这种情况。这可能有所帮助
myDiv.InnerHtml = "something" + myDiv.InnerText;
any html control in myDiv but not ASP html controls(as they are not rendered yet). myDiv中的任何html控件,但不是ASP html控件(因为它们尚未呈现)。
You could reference controls inside the master page this way: 您可以通过以下方式引用母版页内的控件:
void Page_Load()
{
ContentPlaceHolder cph;
Literal lit;
cph = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if (cph != null) {
lit = (Literal) cph.FindControl("Literal1");
if (lit != null) {
lit.Text = "Some <b>HTML</b>";
}
}
}
In this example you have to put a Literal control in your ContentPlaceholder. 在此示例中,您必须在ContentPlaceholder中放置Literal控件。
You want to put code in the master page code behind that inserts HTML into the contents of a page that is using that master page? 您想将代码放在后面的母版页代码中,将HTML插入到使用该母版页的页面内容中吗?
I would not search for the control via FindControl as this is a fragile solution that could easily be broken if the name of the control changed. 我不会通过FindControl搜索控件,因为这是一个脆弱的解决方案,如果控件名称发生变化,很容易被破坏。
Your best bet is to declare an event in the master page that any child page could handle. 最好的办法是在母版页中声明任何子页面可以处理的事件。 The event could pass the HTML as an EventArg. 该事件可以将HTML作为EventArg传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.