📅  最后修改于: 2023-12-03 14:39:42.506000             🧑  作者: Mango
In C#, we often need to dynamically add HTML elements to a web page. One way to achieve this is to use the Append()
method to add a new DIV element to the page. In this tutorial, we will discuss how to append a DIV element to a web page using C# codebehind.
To create a new DIV
element, we can use the following code:
HtmlGenericControl div = new HtmlGenericControl("div");
This code creates a new DIV
element using the HtmlGenericControl
class. We can set attributes and content for this element using the appropriate properties.
To set attributes for the DIV
element, we can use the following code:
div.Attributes["class"] = "myClass";
div.Attributes["id"] = "myDiv";
In this example, we set the class
and id
attributes for the DIV
element.
To set content for the DIV
element, we can use one of the following methods:
div.InnerHtml = "This is my div.";
In this example, we set the inner HTML content of the DIV
element.
To add the new DIV
element to the web page using C# codebehind, we can use the following code:
Page.Header.Controls.Add(div);
In this example, we add the DIV
element to the Header
section of the web page.
In this tutorial, we learned how to dynamically add a DIV
element to a web page using C# codebehind. We created a new DIV
element, set attributes and content for it, and then added it to the web page. This is a useful technique for adding dynamic content to a web page.