Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Writing HTML & Server controls dynamically

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WhiskeyRomeo - 05 Dec 2007 21:00 GMT
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
Andrew Morton - 06 Dec 2007 10:12 GMT
> 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
[quoted text clipped - 4 lines]
> 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
Andrew Morton - 06 Dec 2007 10:35 GMT
Also, for .NET 1.1 you may find this useful:
http://slingfive.com/pages/code/browserCaps/

Andrew
WhiskeyRomeo - 06 Dec 2007 21:06 GMT
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

> > 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
[quoted text clipped - 20 lines]
>
> Andrew
Andrew Morton - 07 Dec 2007 09:05 GMT
> 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
WhiskeyRomeo - 07 Dec 2007 16:15 GMT
Andrew,

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

Bill

> > 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
[quoted text clipped - 45 lines]
>
> Andrew
Registered User - 08 Dec 2007 00:06 GMT
>I am working in .Net 1.1 (VB.Net).  I know very little JavaScript.
>
[quoted text clipped - 3 lines]
>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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.