ASP.NET dynamic user control viewstate question

  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

Preface: my user control has 2 dropdownlists. The second dropdownlist is dependent on the first.

Here's the process in pseudo code:

1. ASPX loads normal
2. The user clicks a button
3. The button_click event adds a user control (uc) to a placeholder on the page
3a. When the uc is added I update a viewstate variable with the ID of the uc
4. The user selects a value from the first dropdownlist on the uc
4a. This triggers a postback in order to update the potential values in the second dropdownlist on my uc
4b. In page_load of the ASPX I parse the viewstate variable from 3a and re-add the uc with the same ID
5. The user selects the appropriate value in the second dropdownlist

1-5 works perfect. The problem is any postback after step 5 causes the selected value in step 5 to be lost.

Relevant ASPX code:
Code: [ Select ]
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
     getAttributes()
   End Sub

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     'READD DYNAMIC CONTROLS
     getDynCtrlAttributes()
   End Sub

Protected Sub getAttributes()
     Try
        Dim attr As String = ""
        Dim attrName As String = ""
        Dim attrValue As String = ""

        Dim ds As DataSet = getSQLData("SELECT VariantMapping FROM OD_Product_Data WHERE SKU='" & selectedSKU & "'")
        For Each dr As DataRow In ds.Tables(0).Rows()
          attr = dr(0).ToString
        Next
        ds = Nothing
        Dim attrArr As Array = attr.Split("|")
        For Each item As String In attrArr
          Dim attrDetail As Array = item.Split(":")
          attrName = attrDetail(0)
          attrValue = attrDetail(1)
          Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx")
          ctrlAttributes.AttributeName = attrName
          ctrlAttributes.AttributeValue = attrValue
          ctrlAttributes.ID = "ctrlAttribute-" & attrName
          phAttributes.Controls.Add(ctrlAttributes)
        Next
     Catch ex As Exception
        SendError(ex.Message, "Default.getAttributes")
     End Try
   End Sub

   Protected Sub getDynCtrlAttributes()
     Try
        If ViewState("dynCtrlAttributes") <> "" Then
          Dim attrArr As Array = ViewState("dynCtrlAttributes").Split("|")
          For Each item As String In attrArr
             Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx")
             ctrlAttributes.ID = item
             phAttributes.Controls.Add(ctrlAttributes)
          Next
        End If
     Catch ex As Exception
        SendError(ex.Message, "Default.getDynCtrlAttributes")
     End Try
   End Sub
  1. Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  2.      getAttributes()
  3.    End Sub
  4.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.      'READD DYNAMIC CONTROLS
  6.      getDynCtrlAttributes()
  7.    End Sub
  8. Protected Sub getAttributes()
  9.      Try
  10.         Dim attr As String = ""
  11.         Dim attrName As String = ""
  12.         Dim attrValue As String = ""
  13.         Dim ds As DataSet = getSQLData("SELECT VariantMapping FROM OD_Product_Data WHERE SKU='" & selectedSKU & "'")
  14.         For Each dr As DataRow In ds.Tables(0).Rows()
  15.           attr = dr(0).ToString
  16.         Next
  17.         ds = Nothing
  18.         Dim attrArr As Array = attr.Split("|")
  19.         For Each item As String In attrArr
  20.           Dim attrDetail As Array = item.Split(":")
  21.           attrName = attrDetail(0)
  22.           attrValue = attrDetail(1)
  23.           Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx")
  24.           ctrlAttributes.AttributeName = attrName
  25.           ctrlAttributes.AttributeValue = attrValue
  26.           ctrlAttributes.ID = "ctrlAttribute-" & attrName
  27.           phAttributes.Controls.Add(ctrlAttributes)
  28.         Next
  29.      Catch ex As Exception
  30.         SendError(ex.Message, "Default.getAttributes")
  31.      End Try
  32.    End Sub
  33.    Protected Sub getDynCtrlAttributes()
  34.      Try
  35.         If ViewState("dynCtrlAttributes") <> "" Then
  36.           Dim attrArr As Array = ViewState("dynCtrlAttributes").Split("|")
  37.           For Each item As String In attrArr
  38.              Dim ctrlAttributes As AttributeControl = LoadControl("ctrlAttribute.ascx")
  39.              ctrlAttributes.ID = item
  40.              phAttributes.Controls.Add(ctrlAttributes)
  41.           Next
  42.         End If
  43.      Catch ex As Exception
  44.         SendError(ex.Message, "Default.getDynCtrlAttributes")
  45.      End Try
  46.    End Sub


Relevant ASCX code:
Code: [ Select ]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     Try
        If Not IsPostBack Then
          bindAttributeName()
          ddlAttributeName.Items.Insert(0, "Select an Attribute")
          ddlAttributeValue.Items.Insert(0, "Select a Value")
          If AttributeName <> "" Then
             ddlAttributeName.SelectedValue = AttributeName.Trim()
             bindAttributeValues()
             If AttributeValue <> "" Then
               ddlAttributeValue.SelectedValue = AttributeValue.Trim()
             End If
          End If
        Else
          If AttributeName = "" Then
             bindAttributeName()
             ddlAttributeName.Items.Insert(0, "Select an Attribute")
             ddlAttributeValue.Items.Insert(0, "Select a Value")
             ddlAttributeValue.Enabled = False
          End If
        End If

     Catch ex As Exception
        Common.SendError(ex.Message, "AttributeControl.Page_Load")
     End Try
   End Sub

   Protected Sub bindAttributeName()
     Try
        ddlAttributeName.DataSource = Common.getSQLData("SELECT DISTINCT AttributeName FROM OD_Attribute_Values ORDER BY AttributeName")
        ddlAttributeName.DataTextField = "AttributeName"
        ddlAttributeName.DataValueField = "AttributeName"
        ddlAttributeName.DataBind()
     Catch ex As Exception
        Common.SendError(ex.Message, "AttributeControl.bindAttributeName")
     End Try
   End Sub

   Protected Sub ddlAttributeName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAttributeName.SelectedIndexChanged
     bindAttributeValues()
     ddlAttributeValue.Enabled = True
   End Sub

   Protected Sub bindAttributeValues()
     Try
        ddlAttributeValue.DataSource = Common.getSQLData("SELECT DISTINCT AttributeValue as attrValue FROM OD_Attribute_Values WHERE AttributeName = '" & ddlAttributeName.SelectedValue & "' ORDER BY AttributeValue")
        ddlAttributeValue.DataTextField = "attrValue"
        ddlAttributeValue.DataValueField = "attrValue"
        ddlAttributeValue.DataBind()
     Catch ex As Exception
        Common.SendError(ex.Message, "AttributeControl.bindAttributeValues")
     End Try
   End Sub
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.      Try
  3.         If Not IsPostBack Then
  4.           bindAttributeName()
  5.           ddlAttributeName.Items.Insert(0, "Select an Attribute")
  6.           ddlAttributeValue.Items.Insert(0, "Select a Value")
  7.           If AttributeName <> "" Then
  8.              ddlAttributeName.SelectedValue = AttributeName.Trim()
  9.              bindAttributeValues()
  10.              If AttributeValue <> "" Then
  11.                ddlAttributeValue.SelectedValue = AttributeValue.Trim()
  12.              End If
  13.           End If
  14.         Else
  15.           If AttributeName = "" Then
  16.              bindAttributeName()
  17.              ddlAttributeName.Items.Insert(0, "Select an Attribute")
  18.              ddlAttributeValue.Items.Insert(0, "Select a Value")
  19.              ddlAttributeValue.Enabled = False
  20.           End If
  21.         End If
  22.      Catch ex As Exception
  23.         Common.SendError(ex.Message, "AttributeControl.Page_Load")
  24.      End Try
  25.    End Sub
  26.    Protected Sub bindAttributeName()
  27.      Try
  28.         ddlAttributeName.DataSource = Common.getSQLData("SELECT DISTINCT AttributeName FROM OD_Attribute_Values ORDER BY AttributeName")
  29.         ddlAttributeName.DataTextField = "AttributeName"
  30.         ddlAttributeName.DataValueField = "AttributeName"
  31.         ddlAttributeName.DataBind()
  32.      Catch ex As Exception
  33.         Common.SendError(ex.Message, "AttributeControl.bindAttributeName")
  34.      End Try
  35.    End Sub
  36.    Protected Sub ddlAttributeName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAttributeName.SelectedIndexChanged
  37.      bindAttributeValues()
  38.      ddlAttributeValue.Enabled = True
  39.    End Sub
  40.    Protected Sub bindAttributeValues()
  41.      Try
  42.         ddlAttributeValue.DataSource = Common.getSQLData("SELECT DISTINCT AttributeValue as attrValue FROM OD_Attribute_Values WHERE AttributeName = '" & ddlAttributeName.SelectedValue & "' ORDER BY AttributeValue")
  43.         ddlAttributeValue.DataTextField = "attrValue"
  44.         ddlAttributeValue.DataValueField = "attrValue"
  45.         ddlAttributeValue.DataBind()
  46.      Catch ex As Exception
  47.         Common.SendError(ex.Message, "AttributeControl.bindAttributeValues")
  48.      End Try
  49.    End Sub


Any help preserving the selected value in step #5 across postbacks would be greatly appreciated!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

I've identified the source of the problem. Unsure of the solution though.

When the page postsback after step #5 it triggers the selectedindexchanged event on the ASCX, every time the page postsback, whether the selectedindex changes or not.
  • s15199d
  • Expert
  • Expert
  • User avatar
  • Posts: 524
  • Loc: NC, USA

Post 3+ Months Ago

Changed my selectedIndexChanged to this and it works like a champ now:

Code: [ Select ]
   Protected Sub ddlAttributeName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAttributeName.SelectedIndexChanged
     If ddlAttributeName.SelectedValue <> "" Then
        bindAttributeValues()
        ddlAttributeValue.Enabled = True
     End If
  1.    Protected Sub ddlAttributeName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAttributeName.SelectedIndexChanged
  2.      If ddlAttributeName.SelectedValue <> "" Then
  3.         bindAttributeValues()
  4.         ddlAttributeValue.Enabled = True
  5.      End If

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 79 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.