As I mention before, our new custom GridView control will support checking/unchecking the checkbox column in client-side using JavaScript functions
embedded with the control, so we need to assign the JavaScript function to the
checkbox element in the Header template of the GridView, and that occurs at the
row created subroutine as illustrated in Listing 5.
Listing 5
Protected Overloads Overrides Sub OnRowCreated(ByVal e As GridViewRowEventArgs)
MyBase.OnRowCreated(e)
If IncludeColumnCheckBox Then
If e.Row.RowType = DataControlRowType.Header Then
DirectCast(e.Row.FindControl("ChkHeader"), CheckBox).Attributes.Add("onclick", _
"ObjJs_" & Me.ID & ".CheckUnCheckRows(this.checked)")
End If
End If
End Sub
We have to tell the Page that our custom control has JavaScript
files that need to be registered when the final HTML is generated for that
page. So we need to write a bit of code in the OnInit subroutine.
Listing 6
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
Me.Page.ClientScript.RegisterClientScriptInclude(Me.GetType(), "CustomGridJS", _
Page.ClientScript.GetWebResourceUrl(Me.GetType(),
"MyCustomControl.CustomGridJS.js"))
End Sub
And we should declare a JavaScript object from the JavaScript
file which contains the client side code as in Listing 7. Using this way allows
us to drag and drop our new custom control many times in the page without
worrying about confliction between these instances.
Listing 7
Public Overrides Sub Rendercontrol(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.RenderControl(writer)
ScriptManager.RegisterStartupScript(Page, Me.GetType, Me.ID & "JSBlock", "var
ObjJs_" & Me.ID & " = new GridClass('" & Me.ClientID & "');", True)
End Sub
I recommend you download the source so that you can preview
the JavaScript file which is responsible for the client side functionality.