Javascript integreted with GridView rowdatabound event

Javascript code is as below. The second hightlighten part also shows how to set a value to the label, if you just use property “value”, it won’t work.

function CheckGridList(maxnumber, currentcheckboxid){
  var count = 0;
  var leftitems = 0;
  for (i=0; i < document.forms[0].elements.length; i++){           
     if (document.forms[0].elements[i].type == 'checkbox') 
        if (document.forms[0].elements[i].checked == true){                                   
            if (count > maxnumber){
        //document.forms[0].elements[i].checked = false;                                               
            document.getElementById(currentcheckboxid).checked = false;                       
  if (count > maxnumber)
      alert("Only 5 items can be selected at one time.");
      return false;
   else {
         //leftitems = maxnumber - count;
         document.getElementById("lbl_alert").innerHTML = (maxnumber - count).toString() + " items remaining for selecting.";
         return true;

The first highlight get parameter from the function argument currentcheckboxid, but how to pass the current check box client id to the javascript?

Private Sub gridview_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview.RowDataBound
        Dim ck As CheckBox
            If e.Row.RowType = DataControlRowType.DataRow Then
                ck = DirectCast(e.Row.FindControl("ckselected"), CheckBox)
                ck.Attributes.Add("OnClick", "CheckGridList(" & SurveyController.getMaxItemNumber & ", '" & ck.ClientID & "')")
            End If
        Catch ex As Exception

        End Try
    End Sub

Be cautious the single quote and double quote using, if you just give the id value without single quote, the javascript will pass an object as the parameter instead of the id value.


One thought on “Javascript integreted with GridView rowdatabound event

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s