Writing HTML & Server controls dynamically

Discussion in 'ASP .Net' started by WhiskeyRomeo, Dec 5, 2007.

  1. WhiskeyRomeo

    WhiskeyRomeo Guest

    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
     
    WhiskeyRomeo, Dec 5, 2007
    #1
    1. Advertising

  2. WhiskeyRomeo wrote:
    > 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
     
    Andrew Morton, Dec 6, 2007
    #2
    1. Advertising

  3. Andrew Morton, Dec 6, 2007
    #3
  4. WhiskeyRomeo

    WhiskeyRomeo Guest

    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


    "Andrew Morton" wrote:

    > WhiskeyRomeo wrote:
    > > 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
    >
    >
    >
     
    WhiskeyRomeo, Dec 6, 2007
    #4
  5. WhiskeyRomeo wrote:
    > 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
     
    Andrew Morton, Dec 7, 2007
    #5
  6. WhiskeyRomeo

    WhiskeyRomeo Guest

    Andrew,

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

    Bill

    "Andrew Morton" wrote:

    > WhiskeyRomeo wrote:
    > > 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, Dec 7, 2007
    #6
  7. On Wed, 5 Dec 2007 13:00:03 -0800, WhiskeyRomeo
    <> wrote:

    >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.
     
    Registered User, Dec 8, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2lhag==?=
    Replies:
    7
    Views:
    1,543
    =?Utf-8?B?c2lhag==?=
    Mar 7, 2005
  2. Jeffrey Todd
    Replies:
    1
    Views:
    4,152
    Peter Blum
    Jun 2, 2005
  3. brian

    HTML Server Controls vs Web Server Controls

    brian, Jan 21, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    319
    Steve C. Orr [MVP, MCSD]
    Jan 24, 2005
  4. Robby Bankston
    Replies:
    7
    Views:
    243
    wasntme
    Jun 17, 2005
  5. ezmiller
    Replies:
    2
    Views:
    91
    ezmiller
    Dec 6, 2005
Loading...

Share This Page