Shoving dynamic code in the middle of a page.

U

UJ

I'm a newbie to this so please excuse the level of question. My question is
how can I dynamically create a table in the middle of a page without having
to build the entire page dynamically? I have code to build the string that
will generate the table. (This is all to solve the problem of when a person
clicks a checkbox in a datagrid, there's no way of knowing what record it
belongs to - see my previous post)

Here's what my code would look like ideally:

<html>
<body>
<form>
Isn't this a pretty page?

(Some kind of code that would build all the controls I need. Ideally
this code would be a function that is in the codebehind page)

Done creating my stuff.
</form>
</body>
<html>

Thanks again in advance.

Jeffrey.
 
K

Ken Cox [Microsoft MVP]

Hi Jeffrey,

You can put a Placeholder control there and in your codebehind, add controls
you create to the placeholder's Controls collection.

Ken
 
K

Karl Seguin

Jeffrey:
Typically you stick a placeholder control and then dynamically build your
controls, placing them inthe placeholder..


....
<form>
<asp:placeholder id="content" runat="server" />
</form>


codebehind:

protected content as PlaceHolder

page_load
dim table as new HtmlTable
dim row as new HtmlTableRow

dim cell as new HtmlRableCell
cell.InnerText = "Hello"
row.Cells.Add(cell)


cell = new HtmlTableCell
cell.InnerText = "World"
row.Cells.Add(cell)

table.Rows.Add(row)

content.Controls.Ad(table)
end sub


or, if you are looping through records, use a repeater/datagrid/datalist.

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
J

jeffpriz

also couldn't you define the table control in the HTML and simply
dynamically build it out (adding the rows & columns) in the
code-behind..?

What does the placeholder give you?

It seems to me that the placeholder is good when you know that
sometimes the control at that "place" is a textbox and sometimes the
control in the "place" is something else..
Seems that if you know that you are building a table, then you may as
well go ahead and define the table...? What am i missing?
 
U

UJ

Karl,
Thanks for the help.

Jeffrey

Karl Seguin said:
Jeffrey:
Typically you stick a placeholder control and then dynamically build your
controls, placing them inthe placeholder..


...
<form>
<asp:placeholder id="content" runat="server" />
</form>


codebehind:

protected content as PlaceHolder

page_load
dim table as new HtmlTable
dim row as new HtmlTableRow

dim cell as new HtmlRableCell
cell.InnerText = "Hello"
row.Cells.Add(cell)


cell = new HtmlTableCell
cell.InnerText = "World"
row.Cells.Add(cell)

table.Rows.Add(row)

content.Controls.Ad(table)
end sub


or, if you are looping through records, use a repeater/datagrid/datalist.

Karl


--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top