<%@Control Language="VB" Debug="False" %> <script runat="server"> <%------------------------------------------------------------%> ' enumeration of dialog types Public Enum DialogType Alert = 0 Confirm = 1 Prompt = 2 End Enum <%------------------------------------------------------------%> Sub AttachDialog(DlgType As DialogType, _ ControlID As String, _ EventName As String, _ MessageText As String, _ Optional CancelEvent As Boolean = False, _ Optional SubmitForm As Boolean = False) ' create hidden field in page for any return value Dim sHidFieldName As String = "AHHClientDlg$" & ControlID Page.RegisterHiddenField(sHidFieldName, "") ' create function name to attach to control Dim sFunctionName, sParams As String sParams = "('" & sHidFieldName & "'," _ & MessageText.Replace("'", "\'") & "', " _ & (Not CancelEvent).ToString().toLower() & "," _ & SubmitForm.ToString().toLower() & ");" Select Case DlgType Case 1: sFunctionName = "return AlertDlgEvent" & sParams Case 2: sFunctionName = "return ConfirmDlgEvent" & sParams Case 3: sFunctionName = "return PromptDlgEvent" & sParams End Select ' attach client-side event handler to element ' need to determine base control type and cast to the ' appropriate type that has an Attributes collection Dim oCtrl As Control = Parent.FindControl(ControlID) If TypeOf oCtrl Is HtmlControl Then CType(oCtrl, HtmlControl).Attributes.Add(EventName, sFunctionName) ElseIf TypeOf oCtrl Is WebControl Then CType(oCtrl, WebControl).Attributes.Add(EventName, sFunctionName) Else Throw New Exception("Control Type Not Supported") End If ' create client-side script if not already registered If Not Page.IsClientScriptBlockRegistered("AHHClientDlg") Then Dim sScript As String = vbCrlf _ & "<script language='javascript'>" & vbCrlf _ & "<!--" & vbCrlf _ & "function AlertDlgEvent(sField, sMsg, bCancel, bSubmit) {" & vbCrlf _ & " var hidfield = document.forms[0].elements[sField];" & vbCrlf _ & " hidfield.value = sMsg;" & vbCrlf _ & " alert(sMsg);" & vbCrlf _ & " if (bSubmit) document.forms[0].submit();" & vbCrlf _ & " return bCancel;" & vbCrlf _ & "}" & vbCrlf _ & "function ConfirmDlgEvent(sField, sMsg, bCancel, bSubmit) {" & vbCrlf _ & " var hidfield = document.forms[0].elements[sField];" & vbCrlf _ & " if (confirm(sMsg) == true)" & vbCrlf _ & " hidfield.value = 'True'" & vbCrlf _ & " else " & vbCrlf _ & " hidfield.value = 'False';" & vbCrlf _ & " if (bSubmit) document.forms[0].submit();" & vbCrlf _ & " return bCancel;" & vbCrlf _ & "}" & vbCrlf _ & "function PromptDlgEvent(sField, sMsg, bCancel, bSubmit) {" & vbCrlf _ & " var hidfield = document.forms[0].elements[sField];" & vbCrlf _ & " hidfield.value = prompt(sMsg, '');" & vbCrlf _ & " if (bSubmit) document.forms[0].submit();" & vbCrlf _ & " return bCancel;" & vbCrlf _ & "}" & vbCrlf _ & "//-->" & vbCrlf _ & "<" & "/script>" & vbCrlf Page.RegisterClientScriptBlock("AHHClientDlg", sScript) End If End Sub <%------------------------------------------------------------%> Function GetDialogResult(ControlID As String) As String ' build hidden field name Dim sHidFieldName As String = "AHHClientDlg$" & ControlID ' get posted value from Request collection Return Request.Form(sHidFieldName) End Function <%------------------------------------------------------------%> </script>