Results 1 to 2 of 2

Thread: Getting Dynamic controls to persist

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77

    Getting Dynamic controls to persist

    Workin a DB search page where the enduser can build a custom search so I must add controls to the page as the search items are selected. Well I got that part done by looping thru a Session object I used to store the searchfield names. Well now I go and add some value to the page and then when I try to add another search item to the page I have to refresh it to do so. Well now any text I have put into a textbox is now gone because I had to reload all the controls when something is added. So I need to know is there a way to retain that text/selection when added or add controls without refreshing the page.

    Here is my current code for adding search items
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
            If Not Page.IsPostBack Then
                GetField()
                reslist()
            End If
    
    
        End Sub
    
        
        
            
        
    
    
        Function GetField()
            
            Session("SearchItems") = "By Town,Status,List Price Range,Bedrooms,Full Baths,Total SqFt (gla),Year Built"
            Session("sys_name") = "town,status,list_price,bedrooms,baths,glaag,year_built"
            Session("condition") = "=,=,bt,bt,bt,bt,bt"
            Session("val1") = "0,0,0,1,1,0,1800"
            Session("val2") = "0,0,99900020,20,99999,2003"
            Session("vi_type") = "TOWN,STATUS,RANGE,RANGE,RANGE,RANGE,RANGE,"
            Session("vi_translation") = "MADAREA,STATUS, , , , , "
            Session("ttlitems") = 7
    
            FillPanel()
        End Function
    
        Function getcondition(ByVal vtype As String, ByVal actual As String, ByVal name As String, ByVal condition As String, ByVal translation As String)
            If condition = "=" And (translation = " " Or translation = "") Then
    
                Dim s As New TextBox
                s.EnableViewState = True
                s.ID = name
                Panel1.Controls.Add(s)
            End If
            If condition = "=" And translation = "MADAREA" Then
                Dim f As New ListBox
                f.Rows = 5
                f.EnableViewState = True
                f.SelectionMode = ListSelectionMode.Multiple
                f.ID = name
                f.DataTextField = "name"
                Dim conn As String
                Dim appsetting As NameValueCollection = CType(HttpContext.Current.GetConfig("appSettings"), NameValueCollection)
                conn = CStr(appsetting("dsnNH"))
                Dim myConnection As SqlConnection = New SqlConnection(conn)
                Dim dsLogin As SqlCommand
                If name = "county" Then
                    dsLogin = New SqlCommand("select name from major_area", myConnection)
                Else
                    dsLogin = New SqlCommand("select name from mls_area", myConnection)
                End If
                Dim dr As SqlDataReader
                myConnection.Open()
                dr = dsLogin.ExecuteReader()
                f.EnableViewState = True
                f.DataSource = dr
                f.DataBind()
                Panel1.Controls.Add(f)
    
                dr.Close()
    
            End If
            If condition = "=" And translation = "STATUS" Then
                Dim f As New DropDownList
                f.ID = name
                f.EnableViewState = True
                f.Items.Add("Current")
                f.Items.Add("Pending")
                f.Items.Add("Sold")
                f.Items.Add("Expired")
                f.Items.Add("Withdrawn")
                Panel1.Controls.Add(f)
            End If
            If condition = "bt" And (translation = " " Or translation = "") Then
    
                Dim s As New TextBox
                s.ID = name & "min"
                s.EnableViewState = True
                Panel1.Controls.Add(s)
                Panel1.Controls.Add(New LiteralControl(" and "))
                Dim t As New TextBox
                t.ID = name & "max"
                t.EnableViewState = True
                Panel1.Controls.Add(t)
    
            End If
            If condition = "cn" And translation = "" And Not name = "features" Then
                Dim d As New DropDownList
                d.ID = name & "cn"
                d.EnableViewState = True
                d.Items.Add("Contains")
                Panel1.Controls.Add(d)
            End If
            If condition = ">=" Or condition = "<=" Or condition = ">" Or condition = "<" Then
                Dim d As New DropDownList
                d.ID = name & "cn"
                d.EnableViewState = True
                d.Items.Add(condition)
                Panel1.Controls.Add(d)
            End If
                    If condition = "=" And translation = "YNU" Then
                Dim d As New DropDownList
                d.ID = name
                d.EnableViewState = True
                d.Items.Add("Yes")
                d.Items.Add("No")
                d.Items.Add("Unknown")
                Panel1.Controls.Add(d)
            End If
            If condition = "cn" And translation = "UPPERSPACE" Then
                Dim t As New TextBox
                t.ID = name
                t.EnableViewState = True
                Panel1.Controls.Add(t)
            End If
            If condition = "=" And translation = "UPPERSPACE" Then
                Dim t As New TextBox
                t.ID = name
                t.EnableViewState = True
                Panel1.Controls.Add(t)
            End If
        End Function
        
        Private Sub ddsearch2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddsearch2.SelectedIndexChanged
    
            If InStr(Session("SearchItems"), Trim(ddsearch2.SelectedValue)) And Not Trim(ddsearch2.SelectedValue) = "Baths" Then
    
                lblExists.Visible = True
                FillPanel()
    
                Exit Sub
            Else
                lblExists.Visible = False
            End If
            Dim stype As String
            Dim conn As String
            Dim appsetting As NameValueCollection = CType(HttpContext.Current.GetConfig("appSettings"), NameValueCollection)
            conn = CStr(appsetting("dsnNH"))
            Dim myConnection As SqlConnection = New SqlConnection(conn)
            Dim dsLogin As SqlCommand = New SqlCommand("select type from search_tree where class_filter like '1%' and name='" & Trim(ddsearch2.SelectedValue) & "'", myConnection)
            Dim ds As SqlDataReader
            myConnection.Open()
            ds = dsLogin.ExecuteReader()
            Do While ds.Read()
                stype = Trim(ds("type").ToString())
            Loop
    
            ds.Close()
            myConnection.Close()
    
            If stype = "H" Then
                
        Private Sub ddSearch3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddSearch3.SelectedIndexChanged
            Dim conn As String
            Dim appsetting As NameValueCollection = CType(HttpContext.Current.GetConfig("appSettings"), NameValueCollection)
            conn = CStr(appsetting("dsnNH"))
            Dim myConnection As SqlConnection = New SqlConnection(conn)
            Dim dsLogin3 As SqlCommand = New SqlCommand("select name,sys_name,condition,vi_translation,vi_type from search_tree where name='" & Trim(ddsearch2.SelectedValue) & "' and class_filter like '1%'", myConnection)
            Dim ds3 As SqlDataReader
            myConnection.Open()
            ds3 = dsLogin3.ExecuteReader()
            
            Do While ds3.Read()
    
                Session("SearchItems") = Session("SearchItems") & "," & ds3("name").ToString()
                Session("sys_name") = Session("sys_name") & "," & ds3("sys_name").ToString()
                Session("condition") = Session("condition") & "," & ds3("condition").ToString()
                'Session("val1") = Session("val1") & "," & ds3("min").ToString()
                'Session("val2") = Session("val2") & "," & ds3("max").ToString()
                Session("vi_type") = Session("vi_type") & "," & ds3("vi_type").ToString()
                Session("vi_translation") = Session("vi_translation") & "," & ds3("vi_translation").ToString()
                Session("ttlitems") = Session("ttlitems") + 1
            Loop
            FillPanel()
            ds3.Close()
            myConnection.Close()
        End Sub
        Function FillPanel()
            Dim name(20) As String
            Dim s_name(20) As String
            Dim cond(20) As String
            Dim v1(20) As String
            Dim v2(20) As String
            Dim vityp(20) As String
            Dim vitrans(20) As String
            Dim i As Integer
            name = Split(Session("SearchItems"), ",")
            s_name = Split(Session("sys_name"), ",")
            cond = Split(Session("condition"), ",")
            v1 = Split(Session("val1"), ",")
            v2 = Split(Session("val2"), ",")
            vityp = Split(Session("vi_type"), ",")
            vitrans = Split(Session("vi_translation"), ",")
            For i = 0 To (Session("ttlitems") - 1)
                Panel1.Controls.Add(New LiteralControl("<table>"))
                Dim l As New Label
                l.ID = s_name(i) & "lbl" & i
                l.Text = name(i)
                'label1.EnableViewState = True
                'l.Font.Name = "Ms sans Serif;10pt"
                Panel1.Controls.Add(New LiteralControl("<tr><td align=left width=150>"))
                Panel1.Controls.Add(l)
                Panel1.Controls.Add(New LiteralControl("</td><td>"))
                getcondition(vityp(i), name(i), s_name(i), cond(i), vitrans(i))
                'getsearch(ds("sys_name").ToString() & "cn", ds("condition").ToString(), ds("vi_translation").ToString())
                Panel1.Controls.Add(New LiteralControl("</td></tr>"))
                Panel1.Controls.Add(New LiteralControl("</table>"))
            Next
        End Function
    Last edited by Cin0s3; Apr 10th, 2003 at 01:48 PM.
    "Find all you need in your mind if you take the time" -DT

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    You need to make sure that any controls that are added dynamically are recreated before the postback change events fire. This will require you to maintain a list of controls you added so you can recreate them on postback.

    I've used ViewState for this. You can create an ArrayList containing the names of the ID's of the controls you've added. Persist this to ViewState. Then on every postback check if that ArrayList is in ViewState, if it is then spin through each item and recreate the control and add it to your panel or whatever BEFORE the postback change events fire(i.e. no later than the Page Load event), that way the values in your dynamically added controls will persist properly.

    This approach is easy if you're always adding the same type of control, it gets complicated if you're adding a bunch of different control types, then you'll need to persist the ID of the control as well as the type of control. A cool way to store both items is to use a Triplet object, then save the Triplet object to your arraylist and again persist the arraylist to viewstate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width