[JavaScript]CheckBoxList特定項目PostBack

  • 5974
  • 0
  • 2008-12-29

[JavaScript]CheckBoxList特定項目PostBack

當專案上要降低開發門檻,往往透過AJAX的UpdatePanel,讓新進的PG可以透過server端的事件,來省掉撰寫javascript的麻煩。

雖然js不難,但是為了維護上的便利,往往還是犧牲了效率,採用無腦化的Updatepanel+Control的Autopostback屬性。

然而,面對CheckBoxList可以複選的功能,往往我們需要postback的項目並非全部,舉例來說,填寫興趣,項目可能有「釣魚」、「睡覺」、「看電影」、「其他」。

當「其他」項目被勾選時,畫面上要出現「其他說明」的TextBox。以效率來說,用js去開關display屬性當然是最好的,但如果使用updatepanel,CheckBoxList的AutoPostback設定為True,把處理的Code寫在SelectedIndexChanged的事件裡面。就會發現AJAX loading的icon轉到讓人頭痛。

 

這時候,只需要開兩個屬性:

  1. AllowCustomPostback:(bool)取得或設定核取方塊的客製化postback。
  2. PostbackItemValue:(String)取得或設定核取方塊要postback的item value並以,串接。

 

 

 

        ''' <summary>
        ''' 取得或設定核取方塊的客製化postback。是否依判斷值註冊postback事件。
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Browsable(True)> _
        Public Overridable Property AllowCustomPostback() As Boolean
            Get
                If Me.ViewState.Item("AllowCustomPostback") Is Nothing Then
                    Return False
                Else
                    Return CType(Me.ViewState.Item("AllowCustomPostback"), Boolean)
                End If
            End Get

            Set(ByVal Value As Boolean)
                Me.ViewState.Item("AllowCustomPostback") = Value
            End Set
        End Property

        ''' <summary>
        ''' 取得或設定核取方塊的postback選取字串。用來當註冊postback事件的判斷值。
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Browsable(True), Description("取得或設定checkboxlist要postback的item value並以,串接")> _
          Public Overridable Property PostbackItemValue() As String
            Get
                If Me.ViewState.Item("PostbackItemValue") Is Nothing Then
                    Return String.Empty
                Else
                    Return CType(Me.ViewState.Item("PostbackItemValue"), String)
                End If
            End Get

            Set(ByVal value As String)
                Me.ViewState.Item("PostbackItemValue") = value
            End Set
        End Property

接著在覆寫OnPreRender(),在特定項目註冊__doPostBack()即可。

 

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

            If AllowCustomPostback = True And Me.AutoPostBack = False And PostbackItemValue.Length > 0 Then
                Dim PostbackItemValueArray As String()
                PostbackItemValueArray = PostbackItemValue.Split(",")

                For j As Integer = 0 To PostbackItemValueArray.Length - 1

                    For i As Integer = 0 To Me.items.Count - 1

                        If Me.items(i).Value = PostbackItemValueArray(j) Then
                            Me.items(i).Attributes("onclick") = "__doPostBack('" + Me.ClientID + "','');"
                        End If

                    Next
                Next

            End If
        End Sub

blog 與課程更新內容,請前往新站位置:http://tdd.best/