简体   繁体   English

我可以在加载事件的C#中的div标签中动态添加HTML吗?

[英]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.

相关问题 如何在c#中将CSS添加到动态创建的div中? - How can i add css to a dynamically created div in c#? C#,Html Agility,选择div标签中的每个段落 - C#, Html Agility, Selecting every paragraph within a div tag 如何在C#中动态创建DIV,向其添加子元素,然后隐藏DIV(及其上的所有内容)? - How can I dynamically create a DIV in C#, add child elements to it, and then hide the DIV (and everything on it)? Html,C#,ASP.NET如何重复包含标签的div标签中的内容来显示数据库中的所有记录? - Html, C#, ASP.NET How can i repeat the contents in div tag containing labels to display all records from database? 我如何处理循环内动态创建的控件的click事件 - c# - how can I handle the click event of controls created dynamically within a loop 从中获取文字 <Div> 标记从HTML页面到C# - Get text from a <Div> tag from a html page to c# 如何在中添加选项 <select>标签使用C#动态加载页面? - how to add options in <select> tag at page load dynamically using c#? 如何在C#中向div动态添加数据表? - How to Add Datatable Dynamically to div in C#? C# 动态添加事件处理程序 - C# dynamically add event handler 如何从背后的 C# 代码动态创建 html 元素? - How can i dynamically create html elements from C# code behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM