ASP.NET dynamic user control viewstate question
- s15199d
- Expert


- Joined: Feb 20, 2004
- Posts: 524
- Loc: NC, USA
- Status: Offline
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:
Relevant ASCX code:
Any help preserving the selected value in step #5 across postbacks would be greatly appreciated!
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
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
- 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
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
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
- 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
Any help preserving the selected value in step #5 across postbacks would be greatly appreciated!

Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
November 8th, 2010, 1:04 pm
- s15199d
- Expert


- Joined: Feb 20, 2004
- Posts: 524
- Loc: NC, USA
- Status: Offline
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.
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.

Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
- s15199d
- Expert


- Joined: Feb 20, 2004
- Posts: 524
- Loc: NC, USA
- Status: Offline
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
If ddlAttributeName.SelectedValue <> "" Then
bindAttributeValues()
ddlAttributeValue.Enabled = True
End If
- 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

Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 3 posts
- Users browsing this forum: Kurthead+1 and 136 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
