控制項學習八(控制項加入JavaScript)
十.為控制項加入JavaScript
1.讓按鈕在Submit之前做些Client事情
'加上一個 onClinetClickCommand()
'如果此屬性值存在,則當按鈕 Click 的時候,不處理 postback,
'而改成執行此段 JavaScript 程式
Dim _BeforePostbackCommand As String
<Bindable(True), Category("Appearance"), DefaultValue(""), Description("按下按鈕後,Postback前,要執行的 JavaScript")> Property BeforePostbackCommand() As String
Get
Return _BeforePostbackCommand
End Get
Set(ByVal Value As String)
_BeforePostbackCommand = Value
End Set
End Property
'加入 JavaScript
If Me.BeforePostbackCommand <> "" Then
JavaScript = Me.BeforePostbackCommand & ";" & Me.Page.GetPostBackEventReference(Me, "")
output.AddAttribute("onclick", "javascript:" & JavaScript) '加上 onclick=BeforePostbackCommand
End If
3.讓按鈕支援Msgbox(Yes/No)的判斷
'加上一個 onClickConfirmMessage()
'如果此屬性值存在,則當按鈕 Click 的時候,不處理 postback,
'而改成執行此段 JavaScript 程式
Dim _onClickConfirmMessage As String
<Bindable(True), Category("Appearance"), DefaultValue(""), Description("按下按鈕時,要顯示的確認訊息")> Property onClickConfirmMessage() As String
Get
Return _onClickConfirmMessage
End Get
Set(ByVal Value As String)
_onClickConfirmMessage = Value
End Set
End Property
If Me.onClickConfirmMessage <> "" Then
'加上確認
JavaScript = "if (window.confirm('" & Me.onClickConfirmMessage & "')==true) {" & JavaScript & "};"
End If