asp.net服务器控件没有正确保存状态

时间:2013-02-11 12:13:02

标签: asp.net ajaxcontroltoolkit custom-server-controls servercontrols

我正在尝试创建一个使用AjaxControlToolkit控件的ASP.net服务器控件。从本质上讲,控件是一个下拉列表和一个按钮。当用户单击该按钮时,将打开一个模式对话框(modalPopupExtender)。该对话框有一个复选框列表,其中包含可供选择的选项列表。当用户选择他们的选择并点击“确定”按钮时,在对话框中,对话框将关闭,所选的选项应添加到下拉列表中。然而,正在发生的事情是,当用户进行选择并点击确定时,只会添加复选框列表中的最后一项。我认为它可能与我重写的继承函数有关。谁能告诉我我可能做错了什么?以下是相关代码:

<DefaultProperty("Text"), ToolboxData("<{0}:myServerControl runat=server> 
        </{0}:myServerControl >")> _
Public Class myServerControl 
Inherits CompositeControl
Implements INamingContainer, IPostBackEventHandler

Private myDropDownListAs New DropDownList
Private addPlant As New Button
Private _updatePanel As UpdatePanel
Private _modalExtenderOne As AjaxControlToolkit.ModalPopupExtender
Private lblWarning As Label
Private cb1 As New CheckBoxList
Private btnSavePlants As New Button
Private btnCancel As New Button

  Protected Overrides Function SaveViewState() As Object
    Return New Pair(MyBase.SaveViewState(), Nothing)
End Function


Protected Overrides Sub LoadViewState(ByVal savedState As Object)
    MyBase.LoadViewState(CType(savedState, Pair).First)
    EnsureChildControls()
End Sub

Protected Overrides Sub CreateChildControls()
    createDynamicControls()
    MyBase.CreateChildControls()
End Sub

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)       
    MyBase.OnInit(e)

End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
End Sub

 Private Sub createDynamicControls()
    Controls.Clear()
    AddHandler btnSavePlants.Click, AddressOf saveItems_Click
    _updatePanel = New UpdatePanel
    Dim myTable As New Table
    myTable = buildControl()  ' a funciton that returns a built table
    Dim myPanel As New Panel
    myPanel = buildDialog()  'a function taht returns a build modal dialog
    _updatePanel.ContentTemplateContainer.Controls.Add(myTable)
    _updatePanel.ContentTemplateContainer.Controls.Add(myPanel)
    Me.Controls.Add(_updatePanel)
End Sub

  Protected Sub saveItems_Click(ByVal sender As Object, ByVal e As EventArgs)
    Me.myDropDownList.Items.Clear()
    Try
        For Each i As ListItem In Me.cb1.Items
            If (i.Selected = True) Then
                Dim newLi As New ListItem
                With newLi
                    .Selected = False
                    .Text = i.Text
                    .Value = i.Value
                End With

                Me.myDropDownList.Items.Add(newLi)
            Else                    
            End If

        Next
    Catch ex As Exception

    End Try

End Sub

   Public Sub bindSelections(ByVal myList As List(Of myObject))

    For Each p As myObjectIn myList
        Dim newLI As New ListItem
        newLI.Value = p.EquipmentPK
        newLI.Text = p.EquipmentID

        Me.cb1.Items.Add(newLI)

    Next



End Sub


Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent

End Sub

End Class

0 个答案:

没有答案