Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Generating HTML at a placeholder

Author  Topic 

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-09 : 09:32:04
I was going to use a placeholder to dynamically place controls on to my page, but then I ran into the problem where I don't know how to get the static HTML in with the controls I'm adding to the placeholder. For example the controls I add dynamically are nested within a table and I need to dynamically write out the TD and TR tags along with the controls, so that they display correctly on the page.

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-09 : 10:09:55
Bill -- you should probably find some good ASP.NET tutorials on the web to find out information on creating controls dynamically and how the asp.net object model works. You should not have to write out "TD" and "TR" tags manually; everything in ASP.NET uses actual objects and you do it as you would in any other programming language. ASP.NET takes care of the rendering.

If you have a placeholder on your page, you can easily create a table and rows and cells all using the ASP.NET object model. Within each of those cells, you can create dropdown lists and anything else you need as I demonstrated in the other thread.

For example, if "spot" is the name of your placeholder control, this will create a 10x10 table:

Dim t As New Table

Dim r As Integer, c As Integer

For r = 1 To 10
Dim tr As New TableRow
For c = 1 To 10
Dim tc As New TableCell
tc.Text = "Row " & r.ToString() & ", Column: " & c.ToString()
tr.Cells.Add(tc)
Next
t.Rows.Add(tr)
Next

spot.Controls.Add(t)


No need to manually render TR and TD tags and all that. It is a different world from regular old ASP, which I suspect you are coming from. Again, you can render HTML manually if you want, but if you want to take advantage of things like postbacks and viewstates and processing events at the server and all that, you need to use the ASP.NET object model.

Even easier than that, there are also Repeater, DataGrid and DataList controls that you can bind to data sources such as database tables and use HTML templates and so on. A DataGrid will create the HTML table for you and take care of most of the work, and Repeater gives you the most control where you can mix in HTML with databinding syntax to create your content.

Finally, if you do need to render just basic HTML, you can use the LiteralControl and just pass in the straight text you want to render. Of course, this text is just passed to the client and any tags you put in there will not have viewstate or be part of the server object model.

It is definitely a bit out of the scope of this forum for me to give you a full tutorial on asp.net, but I can assure you that it would pay off greatly to learn more about these concepts.
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-02-09 : 11:44:33
Yeah, I'm thinking the same think Bill.
A good ASP.net book (ASP.Net Unleashed is the one I have) would do you a world of good, and answer most of your questions.

From what I can tell, you are still thinking in an ASP 3.0 mode. ASP.net works very different. You have to learn how it works to be able to use all the new cool things that it can do.

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>
Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-09 : 14:17:05
You wouldn't know it based on my questions, but I have actually already ready a book and several online tutorials on ASP.Net lol. I probably still am stuck in the old classic ASP mode though.

My problem I'm worried about now when I go to create all these dropdownlists at the placeholder is that I'm going to lose their values on the postback, but I'll have to address that when I get there.
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-02-09 : 17:11:03
You need to programmatically create your ASP Drop Down list in code, populate it with options, and then do a PlaceHolder.controls.add(myDropDownList).

I think when I did this stuff, I gave it a special name. Then upon postback I had to loop through the Request.Form collection to find my dynamically created control and its selected value.

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>
Go to Top of Page
   

- Advertisement -