2013-05-12 25 views
1

我有一個動態創建的下拉列表控制用的SelectedValue proerty問題而無法更新:ASP .NET Dropdownlist.Selectedvalue不更新​​迭代槽動態創建的控件

我有一個包含一個組合和其他控件類型ctlProductosFormatos的一個自定義的控制。我在其他地方動態創建了這些自定義控件,然後我遍歷動態創建的控件中的所有組合,更新它們上的項目列表,從另一個虛擬下拉列表中複製我想要的值。

此代碼中的控件正確迭代,組合也填充正確。我的問題是,我想維護每個組合上的selectedvalue與項目更新之前的值相同,但是失敗。

如果我進入代碼並且只運行第一個組合的迭代,並且跳出循環外,這個組合是正確填充的並且具有正確的選定值,但是如果我運行完整循環,所有組合都是設置與最後一個組合相同的值,所以我想這是一些相關的東西,像我爲所有的迭代實例化相同的控件,但它似乎不是在我的代碼中。

有些小小的幫助嗎?

Dim ControlFormato As ctlProductosFormatos 
    ' Iterates through custom controls collection, IEnumerable(Of ctlProductosFormatos) 
    For Each ControlFormato In Controles 
     ' Get the dropdownlist inside the current custom control 
     Dim ControlComboFind As Control = ControlFormato.FindControl("cmbFotoFormato") 
     Dim ControlCombo As DropDownList = CType(ControlComboFind, DropDownList) 
     ' Get the currently selected value in the dropdownlist 
     Dim ValorSeleccionado As String = ControlCombo.SelectedValue 

     ' Clear the items in the current combo,and fills with the ones in a dummy combo 
     ControlCombo.Items.Clear() 
     ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray()) 

     ' Sets the current combo selected item with the previously saved one 
     ControlCombo.SelectedValue = ValorSeleccionado 
    Next 

回答

0

正如我在彼得的評論中寫道的,問題在於組合項目的重複,就像「共享項目」一樣。我用這個最醜的和更長的代碼替換重複行,並且問題解決了。希望能幫助到你。

' Clear the items in the current combo,and fills with the ones in a dummy combo 
    ControlCombo.Items.Clear() 
    ' THIS LINE HAS BEEN COMMENTED AND REPLACED FOR THE FOLLOWING CODE 
    ' ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray()) 

     Dim LSource As ListItem 
     Dim LDestination As ListItem 
     For i = 0 To ComboPatron.Items.Count - 1 
      LSource = ComboPatron.Items(i) 
      LDestination = New ListItem(LSource.Text, LSource.Value) 
      ControlCombo.Items.Add(LDestination) 
     Next 
1

問題是ViewState。如果您動態修改DropDownList()內的項目,則ViewState將不會更新,因此當您回發值時,框架將監視控件的ViewState中發送的值,但值將會變爲mising,因此SelectedValue屬性不會被設置。如果你想使用發佈的值,然後從Request.Form [ddlName.ClientID](我不確定ClientID是否是正確的索引,但主要想法是這樣)。

+0

感謝您的提示,但問題與此無關,因爲如果我打破處理只有一個元素的循環,項目處理工作正常。我終於明白,問題出在組合複製方法中。看來這些項目是使用此方法在組合之間共享的,並且在其中一箇中更改selectedvalue會影響其他項目。我發佈瞭解決我的問題的更改。任何方式,感謝您的想法;) – tomasofen 2013-05-12 07:41:45