Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
[prev. Lesson]  Literal Controls Rock! Blows Away response.write
     [next Lesson]  Debug

Dynamically Creating and Adding Controls

One rich capability of ASP.net relies on its Render Model. Basically, before any pages are rendered it first executes all the code, and as a result regardless of the physical code location it can insert and alter controls anywhere on the page. This is a stark contrast to a linear coding model, where any code that needed to add data to the end of the page would have to physically be placed there.

In addition to FindControls and Control Add methods, ASP.net also offers two convenient controls as well:

<asp:placeholder id="whatever">
emits/renders nothing but can be combined with FindControl.

<asp:literal id"whatever" text="whatever">
emits/renders exactly the text supplied without emitting <span><div> or other formatting a <asp:message> control emits.

   filename=\experiments\controlsadd\controlsadd.aspx

<Test Script Below>


<script language="vb" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    ' This example just injects some plain text
    dim whatever as control

    whatever=Page.FindControl("celltwo")
    whatever.controls.Add(new LiteralControl("Surprise"))
    Page.Controls.Add(new LiteralControl("Hello<br>"))
    Page.Controls.Add(new LiteralControl("Glad to Meet You<br>"))

    ' This example injects some table rows and cells
    DIM r AS new TableRow()
    DIM c1 as new TableCell()
    c1.id="emailtext"

    DIM c2 as new TableCell()
    c2.id="emailctrl"

    r.cells.add(c1)
    r.cells.add(c2)
    r.id="foobar"
    t1.rows.add(r)

    ' This example injects some Web Controls
    ' into the new rows
    ' find the cell
    DIM myemail = new TextBox
    myemail.ID = "emailadd"
    whatever=Page.FindControl("emailtext")
    whatever.controls.Add(new LiteralControl("Email Address"))

   ' find the cell
   whatever=Page.FindControl("emailctrl")
   whatever.controls.Add(myemail)
     DIM myemailvalid = new RequiredFieldValidator
   myemailvalid.ErrorMessage = "This is a required field"
   myemailvalid.ControlToValidate = "emailadd"
   whatever.Controls.Add(myemailvalid)
End Sub
</script>
<html><head>
<title>PageControlsAdd</title>
</head>
<body>
<form id="f1" runat="server">
    <asp:Table id="t1" runat="server" GridLines="both" BorderWidth="1px">
    <asp:TableRow>
        <asp:TableCell id="cellone">Cell 1</asp:TableCell>
        <asp:TableCell id="celltwo">Cell 2</asp:TableCell>
    </asp:TableRow>
    </asp:table>
    <asp:Button id="joincrowd" text="Join The Crowd" runat="server"/>
</form>
</body>
</html>
Send Us a Holiday Gift!. Charles celebrates Christmas, Jewish holidays, Kwanza, Chinese New Year, Japanese Holidays, Secretary Day, High Muslim Holy Days, Pagan & Wicca holidays, and many more! - send a gift any HOLIDAY. 2004 gift gallery & 2005 gift gallery