ASP.NET question dynamique viewstate contrôle de l'utilisateur
- s15199d
- Expert


- Inscription: Fév 20, 2004
- Messages: 524
- Loc: NC, USA
- Status: Offline
Préface: mon contrôle utilisateur dispose de 2 listes déroulantes. Le second est dropdownlist dépendant du premier.
Heres le processus dans le code pseudo:
1. ASPX charges normales
2. L'utilisateur clique sur un bouton
3. L'événement Button_Click ajoute un contrôle utilisateur (uc) à un espace réservé sur la page
3a. Lorsque l'uc est ajouté à jour une variable viewstate avec l'ID de l'uc
4. L'utilisateur sélectionne une valeur dans la dropdownlist d'abord sur le uc
4a. Cela déclenche une publication afin de mettre à jour les valeurs de potentiel dans le dropdownlist seconde sur mon uc
4b. En page_load de l'ASPX je analyser la variable d'état d'affichage 3a et rajouter le uc avec le même ID
5. L'utilisateur sélectionne la valeur appropriée dans la deuxième dropdownlist
5.1 fonctionne parfaitement. Le problème, c'est toute publication après l'étape 5 causes de la valeur sélectionnée à l'étape 5 à perdre.
Pertinents ASPX code:
Pertinents ASCX code:
Toute aide à préserver la valeur sélectionnée à l'étape #5 à travers les publications serait grandement appréciée!
Heres le processus dans le code pseudo:
1. ASPX charges normales
2. L'utilisateur clique sur un bouton
3. L'événement Button_Click ajoute un contrôle utilisateur (uc) à un espace réservé sur la page
3a. Lorsque l'uc est ajouté à jour une variable viewstate avec l'ID de l'uc
4. L'utilisateur sélectionne une valeur dans la dropdownlist d'abord sur le uc
4a. Cela déclenche une publication afin de mettre à jour les valeurs de potentiel dans le dropdownlist seconde sur mon uc
4b. En page_load de l'ASPX je analyser la variable d'état d'affichage 3a et rajouter le uc avec le même ID
5. L'utilisateur sélectionne la valeur appropriée dans la deuxième dropdownlist
5.1 fonctionne parfaitement. Le problème, c'est toute publication après l'étape 5 causes de la valeur sélectionnée à l'étape 5 à perdre.
Pertinents 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
Pertinents 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
Toute aide à préserver la valeur sélectionnée à l'étape #5 à travers les publications serait grandement appréciée!

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


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Novembre 8th, 2010, 1:04 pm
- s15199d
- Expert


- Inscription: Fév 20, 2004
- Messages: 524
- Loc: NC, USA
- Status: Offline
Ive identifié la source du problème. Je ne sais pas de la solution si.
Lorsque le postsback page après l'étape #5, il déclenche l'événement SelectedIndexChanged sur le ASCX, chaque fois que le postsback page, si les changements selectedIndex ou non.
Lorsque le postsback page après l'étape #5, il déclenche l'événement SelectedIndexChanged sur le ASCX, chaque fois que le postsback page, si les changements selectedIndex ou non.

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


- Inscription: Fév 20, 2004
- Messages: 524
- Loc: NC, USA
- Status: Offline
Changé ma SelectedIndexChanged pour ceci et cela fonctionne comme un champion en ce moment:
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 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 3 messages
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 163 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
