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

  • s15199d
  • Expert
  • Expert
  • Avatar de l’utilisateur
  • Inscription: Fév 20, 2004
  • Messages: 524
  • Loc: NC, USA
  • Status: Offline

Message Novembre 8th, 2010, 1:04 pm

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:
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


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
  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


Toute aide à préserver la valeur sélectionnée à l'étape #5 à travers les publications serait grandement appréciée!
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Novembre 8th, 2010, 1:04 pm

  • s15199d
  • Expert
  • Expert
  • Avatar de l’utilisateur
  • Inscription: Fév 20, 2004
  • Messages: 524
  • Loc: NC, USA
  • Status: Offline

Message Novembre 8th, 2010, 1:31 pm

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.
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.
  • s15199d
  • Expert
  • Expert
  • Avatar de l’utilisateur
  • Inscription: Fév 20, 2004
  • Messages: 524
  • Loc: NC, USA
  • Status: Offline

Message Novembre 8th, 2010, 2:41 pm

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
  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
Image
Give a man a fish he eats for a day. Teach a man to fish he eats for a lifetime.

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
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC