Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

WebControl Constructor (HtmlTextWriterTag)

Initializes a new instance of the WebControl class using the specified HTML tag.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public WebControl(
	HtmlTextWriterTag tag
)

Parameters

tag
Type: System.Web.UI.HtmlTextWriterTag

One of the HtmlTextWriterTag values.

Use this constructor to create and initialize a new instance of the WebControl class using the specified System.Web.UI.HtmlTextWriterTag value.

The following table shows the initial property value for an instance of WebControl.

Property

Initial Value

TagKey

The HtmlTextWriterTag enumeration value specified by the tag parameter.

The following example demonstrates how to use the constructor for the WebControl class to create a TextArea HTML element and display it on the Web Forms page.

NoteNote

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx file name extension. For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


    void Button1_Click(Object sender, EventArgs e) 
    {
        WebControl wc = new WebControl(HtmlTextWriterTag.Textarea);
        PlaceHolder1.Controls.Add(wc);
    }

</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>WebControl Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>WebControl Constructor Example</h3>
    <p>
        <asp:PlaceHolder id="PlaceHolder1"
            runat="Server"/>
    </p>

    <p>
        <asp:Button id="Button1" runat="Server"
            Text="Click to create a new TextArea" 
            OnClick="Button1_Click" />
    </p>

    </div>
    </form>
</body>
</html>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
Show:
© 2015 Microsoft