Writing HTML & Server controls dynamically

W

WhiskeyRomeo

I am working in .Net 1.1 (VB.Net). I know very little JavaScript.

I need to display information which is not known until runtime. This is a
perfect situation for a hierarchical grid except web grids cannot have
multiple child bands at the same level (unless someone knows of a company
that makes one -- Infragistics doesn't). This would be easily solved in a
windows environment.

I have all the information I need to display in a dataset but because of
normalization it is not user friendly to see it that way. There are 3
pieces of information that need displaying (this is an ordering system where
packages contain different sizes of photographs taken of the customer). A
package can be a simple 5x7 or 2 5x7’s and 1 8x10, etc. A customer can
choose to add retouching, lamination and/or pearlized paper as options. If
the Package contains a plaque, the customer must supply 1 or 2 or 3 lines of
engraving.

Package (there can be multiple package)
Each Package can have 0 to many Options
Each Package can have 0 to 3 lines of Engraving.

For a particular order, I do know how many Packages have been ordered, what
options have been chosen, and whether or not engraving is present. That is
in the dataset. But for each order these value can vary a lot.

Since I can not use hierarchical data-grid, it seems my only option is to
display an HTML table with asp.net labels. Since I don't think I can insert
HTML code and sever controls at specific location of my choosing on page, it
seems I must dynamically generate the whole HTML.

I would have to iterate through my dataset and generate the html and
apsx.net label controls and set their value in the page_init. Is this
possible in code behind? Are they any good code examples?

WR
 
A

Andrew Morton

WhiskeyRomeo said:
Since I can not use hierarchical data-grid, it seems my only option
is to display an HTML table with asp.net labels. Since I don't think
I can insert HTML code and sever controls at specific location of my
choosing on page, it seems I must dynamically generate the whole HTML.

I would have to iterate through my dataset and generate the html and
apsx.net label controls and set their value in the page_init. Is this
possible in code behind? Are they any good code examples?

What I've done is to have an <asp:placeholder> on the aspx page then add
controls to that, e.g. use a Literal for putting pieces of HTML in, then add
a DropDownList, ImageButton, etc.

Remember the Literal doesn't have to have a complete HTML element in it: for
example, you can have it ending with a <td>, then add some other control to
the PlaceHolder, then have another Literal starting with </td>.

Google for
asp.net add control at runtime

to find articles like
http://www.devx.com/codemag/Article/20144/0/page/2

Andrew
 
W

WhiskeyRomeo

Andrew,

Thanks for responding. This information was very helpful. I have included
sample code for the page load event below which works very well. In the real
application, I would be iterating through the dataset instead of for next
loop.

Just for grins, I added a button. In this paricular scenario, the button
does not make sense but assume I was adding labels and text boxes and allowed
a user to submit information via the text boxes.

How would you specify the on_click envent handler in this example? How would
we make sure the labels, textboxes and buttond survive a post back?

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As Int16 = 0
For i = 1 To 4
Select Case i
Case 1
Dim lblPackage1 As New System.Web.UI.WebControls.Label
Dim lblPackagePrice1 As New
System.Web.UI.WebControls.Label
Dim btnSubmit1 As New System.Web.UI.WebControls.Button
btnSubmit1.Text = "Submit"
phPkg.Controls.Add(New LiteralControl("<table>"))
phPkg.Controls.Add(New LiteralControl("<tr>"))
phPkg.Controls.Add(New LiteralControl("<td>"))
phPkg.Controls.Add(lblPackage1)
phPkg.Controls.Add(New LiteralControl("</td>"))
phPkg.Controls.Add(New LiteralControl("<td>"))
phPkg.Controls.Add(lblPackagePrice1)
phPkg.Controls.Add(New LiteralControl("</td>"))
phPkg.Controls.Add(New LiteralControl("<td>"))
phPkg.Controls.Add(btnSubmit1)
phPkg.Controls.Add(New LiteralControl("</td>"))
phPkg.Controls.Add(New LiteralControl("</tr>"))
phPkg.Controls.Add(New LiteralControl("</table>"))
lblPackage1.Text = "Package A"
lblPackagePrice1.Text = Format(25.0, "C")
Case 2

Case 3

Case 4
End Select


Next

End Sub
 
A

Andrew Morton

WhiskeyRomeo said:
Just for grins, I added a button. In this paricular scenario, the
button does not make sense but assume I was adding labels and text
boxes and allowed a user to submit information via the text boxes.

How would you specify the on_click envent handler in this example?

Code snippets to help you along:

B = New ImageButton
With B
.AlternateText = "Delete"
.ImageUrl = "images/delete.gif"
' n is the index of this item in the basket
.ID = "B_" & n.ToString
AddHandler .Click, AddressOf DeleteEntry
End With
PL.Controls.Add(B)

Private Sub DeleteEntry(ByVal sender As Object, ByVal e As
System.web.UI.ImageClickEventArgs)
' retrieve the ID number to index into the basket
Dim n As Integer = CInt(Mid(CType(sender, ImageButton).ID, 3))
' remove corresponding entry
DirectCast(Session("basky"),
basketCentral).currentBasketItems.RemoveAt(n)
' throw away the controls as they have been created before this Sub is
called
' but with the deleted entry included
PL.Controls.Clear()
showBasketContent()
End Sub

(This is with AutoEventWireup="false" in the <%@ Page %> directive, so
remember to use "Handles", e.g.
Private Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles
MyBase.Load).
How would we make sure the labels, textboxes and buttond survive a
post back?

I just create it all again each time. I have a suspicion I'm doing something
out of order with my code (where I throw the controls away) because I create
the items in Page_Load but it was noisy in the office when I wrote it and it
works anyway, <mumble>, <mumble>, <cough> "if not page.ispostback" or
something needed.

HTH

Andrew
 
W

WhiskeyRomeo

Andrew,

Again thanks for the repsonse and the education. You have my vote for MVP.

Bill
 
R

Registered User

I am working in .Net 1.1 (VB.Net). I know very little JavaScript.

I need to display information which is not known until runtime. This is a
perfect situation for a hierarchical grid except web grids cannot have
multiple child bands at the same level (unless someone knows of a company
that makes one -- Infragistics doesn't). This would be easily solved in a
windows environment.
- snip details -

I would have to iterate through my dataset and generate the html and
apsx.net label controls and set their value in the page_init. Is this
possible in code behind? Are they any good code examples?

You can write a custom server control to do this. A good starting
point is
http://msdn2.microsoft.com/en-us/library/zt27tfhy.aspx

regards
A.G.
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top