If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > dynamically created controls lost during postback : anyone knows a clean workaround

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-29-03, 05:01
rodeo rodeo is offline
Registered User
 
Join Date: Jan 2003
Posts: 15
Question dynamically created controls lost during postback : anyone knows a clean workaround

For doing some testing, I added a button on a page, which' on_Click event adds a textbox to the Form containing the button.

I managed to define the position of each newly created button, only to find out all previously created ones were lost in the button_Click's postback event...
Code:
Public Class test
    Inherits System.Web.UI.Page
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then Session("txt") = 0
        'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim txtTEMP As New TextBox()
        Dim b As Button = CType(sender, Button)
        Session("txt") = Session("txt") + 1
        With txtTEMP
            .Text = "TextBox " & CStr(Session("txt"))
            .ID = "TextBox" & CStr(Session("txt"))
        End With
        b.Parent.Controls.Add(txtTEMP)
        txtTEMP = CType(b.Parent.FindControl("Textbox" & CStr(Session("txt"))), TextBox)
        With txtTEMP
            .Style.Add("POSITION", "Absolute")
            .Style.Add("LEFT", CStr(Session("txt") * 100) & "px")
            .Style.Add("TOP", "500")
        End With

    End Sub

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

    End Sub
End Class
How do I make sure these previously created boxes are rerendered during postback?
__________________
The further away I get from the things I care about,
The less I care about how much further away I get...
Reply With Quote
  #2 (permalink)  
Old 07-29-03, 05:42
rodeo rodeo is offline
Registered User
 
Join Date: Jan 2003
Posts: 15
Thumbs up

Found the answer to my own problem. I added a placeholder control to the page, and, instead of using the parent of my button, I add the controls to this placeholder dynamically.

I can then, during oninit, add the number of textboxes previously created.

Code:
Public Class test
    Inherits System.Web.UI.Page
    Protected WithEvents plhTextBoxes As System.Web.UI.WebControls.PlaceHolder
    Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then Session("txt") = 0
        'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim txtTEMP As New TextBox()
        'Dim b As Button = CType(sender, Button)
        Session("txt") = Session("txt") + 1
        With txtTEMP
            .Text = "TextBox " & CStr(Session("txt"))
            .ID = "TextBox" & CStr(Session("txt"))
        End With
        'b.Parent.Controls.Add(txtTEMP)
        PlaceHolder1.Controls.Add(txtTEMP)
        txtTEMP = CType(PlaceHolder1.FindControl("Textbox" & CStr(Session("txt"))), TextBox)
        With txtTEMP.Style
            .Add("POSITION", "Absolute")
            .Add("LEFT", CStr(Session("txt") * 100) & "px")
            .Add("TOP", "500")
            .Add("WIDTH", "90")
        End With

    End Sub

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        Dim i As Integer
        For i = 1 To Session("txt")
            Dim txtTEMP As New TextBox()
            With txtTEMP
                .Text = "TextBox " & CStr(i)
                .ID = "TextBox" & CStr(i)
            End With
            PlaceHolder1.Controls.Add(txtTEMP)
            txtTEMP = CType(Page.FindControl("Textbox" & CStr(i)), TextBox)
            With txtTEMP.Style
                .Add("POSITION", "Absolute")
                .Add("LEFT", CStr(i * 100) & "px")
                .Add("TOP", "500")
                .Add("WIDTH", "90")
            End With
        Next
    End Sub
End Class
Note that, even though I put the "textbox<x>" text in the control's text property (which is plain stupid, of course), the text entered into it appears when rendered, a beautiful demonstration of the LoadViewState event being fired after OnInit (and thus overwriting the here hardcoded textproperty with the saved one

hope this can help others, as it is plain simple :-)
__________________
The further away I get from the things I care about,
The less I care about how much further away I get...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On