JSMITH242B
Programmer
Hi Group
I have the following sub which I would like to share because the code will be used by a number of my webforms.
Public Sub MonitorChanges(ByVal wc As WebControl)
If wc Is Nothing Then Exit Sub
'check boxes and radio boxes are treated
'differently in asp.NET need to
'client id is the Server Control ID generated by ASP.NET
If TypeOf wc Is CheckBoxList OrElse TypeOf wc Is RadioButtonList Then
'Add an array element for each item
For i As Integer = 0 To CType(wc, ListControl).Items.Count - 1
Page.RegisterArrayDeclaration("monitorChangesIDs", """" & _
String.Concat(wc.ClientID, "_", i) & """")
Page.RegisterArrayDeclaration("monitorChangesValues", "null")
Next
Else
Page.RegisterArrayDeclaration("monitorChangesIDs", _
"""" & wc.ClientID & """")
Page.RegisterArrayDeclaration("monitorChangesValues", "null")
AssignChangeValuesOnPageLoad()
End If
End Sub
As soon as I put shared in, I get a squiggly line under the Page directive. Why is this so?
Here is the AssignChangeValuesOnPageLoad() sub routine too which is affected too.
Private Sub AssignChangeValuesOnPageLoad()
If Not Page.IsStartupScriptRegistered("monitorChangesAssignment") Then
Page.RegisterStartupScript("monitorChangesAssignment", _
"<script language = ""Javascript"">" & vbCrLf & _
"assignInitialValuesForMonitorChanges();" & vbCrLf & _
"</script>")
Page.RegisterClientScriptBlock("monitorChangesAssignmentFunction", _
"<script language = ""Javascript"">" & vbCrLf & _
"function assignInitialValuesForMonitorChanges() {" & vbCrLf & _
"for (var i = 0; i < monitorChangesIDs.length; i++) {" & vbCrLf & _
"var elem = document.getElementById(monitorChangesIDs);" & vbCrLf & _
"if (elem) if (elem.type =='checkbox' ||elem.type == 'radio') " & _
"monitorChangesValues = elem.checked; " & _
"else monitorChangesValues = elem.value;" & vbCrLf & _
" }" & vbCrLf & _
" }" & vbCrLf & vbCrLf & vbCrLf & _
" var needtoConfirm= true;" & vbCrLf & _
" window.onbeforeunload = confirmClose;" & vbCrLf & _
" function confirmClose() {" & vbCrLf & _
" if(!needtoConfirm) return;" & vbCrLf & _
" for (var i = 0; i < monitorChangesValues.length; i++) {" & vbCrLf & _
" var elem = document.getElementById(monitorChangesIDs);" & vbCrLf & _
" if (elem) if (((elem.type == 'checkbox' || elem.type == 'radio') && elem.checked != monitorChangesValues) || (elem.type != 'checkbox' && elem.type != 'radio'&& elem.value !=monitorChangesValues)) {needtoConfirm = false; setTimeout('resetFlag()', 750); return ""You have made changes to the data but have not saved it. If you leave this page, any changes will be lost. To save changes, click on the cancel to return to the page and click on the Save button to save the data."";}" & vbCrLf & _
" } " & vbCrLf & _
" }" & vbCrLf & vbCrLf & _
" function resetFlag(){needtoConfirm = true;} " & vbCrLf & _
" </script>")
End If
End Sub
If you copy the above code as is and put into a vb class and change them to shared routines, you will see what I mean.
Kind Regards
I have the following sub which I would like to share because the code will be used by a number of my webforms.
Public Sub MonitorChanges(ByVal wc As WebControl)
If wc Is Nothing Then Exit Sub
'check boxes and radio boxes are treated
'differently in asp.NET need to
'client id is the Server Control ID generated by ASP.NET
If TypeOf wc Is CheckBoxList OrElse TypeOf wc Is RadioButtonList Then
'Add an array element for each item
For i As Integer = 0 To CType(wc, ListControl).Items.Count - 1
Page.RegisterArrayDeclaration("monitorChangesIDs", """" & _
String.Concat(wc.ClientID, "_", i) & """")
Page.RegisterArrayDeclaration("monitorChangesValues", "null")
Next
Else
Page.RegisterArrayDeclaration("monitorChangesIDs", _
"""" & wc.ClientID & """")
Page.RegisterArrayDeclaration("monitorChangesValues", "null")
AssignChangeValuesOnPageLoad()
End If
End Sub
As soon as I put shared in, I get a squiggly line under the Page directive. Why is this so?
Here is the AssignChangeValuesOnPageLoad() sub routine too which is affected too.
Private Sub AssignChangeValuesOnPageLoad()
If Not Page.IsStartupScriptRegistered("monitorChangesAssignment") Then
Page.RegisterStartupScript("monitorChangesAssignment", _
"<script language = ""Javascript"">" & vbCrLf & _
"assignInitialValuesForMonitorChanges();" & vbCrLf & _
"</script>")
Page.RegisterClientScriptBlock("monitorChangesAssignmentFunction", _
"<script language = ""Javascript"">" & vbCrLf & _
"function assignInitialValuesForMonitorChanges() {" & vbCrLf & _
"for (var i = 0; i < monitorChangesIDs.length; i++) {" & vbCrLf & _
"var elem = document.getElementById(monitorChangesIDs);" & vbCrLf & _
"if (elem) if (elem.type =='checkbox' ||elem.type == 'radio') " & _
"monitorChangesValues = elem.checked; " & _
"else monitorChangesValues = elem.value;" & vbCrLf & _
" }" & vbCrLf & _
" }" & vbCrLf & vbCrLf & vbCrLf & _
" var needtoConfirm= true;" & vbCrLf & _
" window.onbeforeunload = confirmClose;" & vbCrLf & _
" function confirmClose() {" & vbCrLf & _
" if(!needtoConfirm) return;" & vbCrLf & _
" for (var i = 0; i < monitorChangesValues.length; i++) {" & vbCrLf & _
" var elem = document.getElementById(monitorChangesIDs);" & vbCrLf & _
" if (elem) if (((elem.type == 'checkbox' || elem.type == 'radio') && elem.checked != monitorChangesValues) || (elem.type != 'checkbox' && elem.type != 'radio'&& elem.value !=monitorChangesValues)) {needtoConfirm = false; setTimeout('resetFlag()', 750); return ""You have made changes to the data but have not saved it. If you leave this page, any changes will be lost. To save changes, click on the cancel to return to the page and click on the Save button to save the data."";}" & vbCrLf & _
" } " & vbCrLf & _
" }" & vbCrLf & vbCrLf & _
" function resetFlag(){needtoConfirm = true;} " & vbCrLf & _
" </script>")
End If
End Sub
If you copy the above code as is and put into a vb class and change them to shared routines, you will see what I mean.
Kind Regards