Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am using the following code example I dug up to learn template based ASP.Net web apps but I don't see how I can add dynamic controls (iow creating a Table control and putting it into the page).

 

Here's the code for the header (testheader.ascx):

<div align="center">
<font Size="5"><b>Test Header</b></font>
</div> 

 

Here's the code for the footer (testfooter.ascx):

 

<div align="center"><hr> 
<font Size="2"><i>Test Footer</i><br> Copyright 2002, All Rights Reserved</font> 
</div> 

Now - here's the main page, where each user control is inserted:

 

<%@ Register TagPrefix="xprs" tagname="footer" src="Testfooter.ascx" %> 
<%@ Register TagPrefix="xprs" tagname="header" src="testheader.ascx" %> 
<html> 
<head> 
<meta name="GENERATOR" Content="ASP Express 2.0"> 
<title>Heading of Page Goes here</title> 
</head> 
<body> 
<xprs:header runat="server"/> 
<i><font Color="#0000FF">As you can see - the Header is on the top of this page. 
</font></i> <p> 

Again - two lines accomplish this, also.</font></i>
<xprs:footer runat="server"/> 
</body> 
</html>

 

Now when I try to do the following in my Page_Load event, no table appears in my page view in runtime, why not?


		Table menu = new Table();
		TableRow row = new TableRow();
		TableCell cell = new TableCell();
		cell.Text = "TestCell";
		row.Cells.Add(cell);
		menu.Rows.Add(row);
		
		Page.Controls.Add(menu);

		TextBox txtBox = new TextBox();
		Page.Controls.Add(txtBox);

Experience is something you don't get until just after the moment you needed it
  • Moderators
Posted

Your code is not really Templating it's a UserControl.

 

To add a userControl at run-time do the following...

 

'At the top of your page inside your class....

Private myHeader As Control = LoadControl("UserControl/myHeader.ascx")

 

'Then in your init event ....

Me.Controls.Add(myHeader)

'Or you can add it to a Panel or something

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...