|
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
<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>
 |  |  |
 |
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too.
a $5 tip buys him lunch at McDonalds,
a $20 tip buys his kid Hitoshi a new computer game,
a $39 tip buys his daughter Michiko a few nice outfits.
See our donor list.
|  |
 |  |  |
|
|
|
|