Friday, October 14, 2011

Get selected checkbox value of ASP.NET GRID VIEW using JavaScript

<script language="javascript" type="text/javascript">

function GetSelected(objchk, HId) {
var hdnIds = document.getElementById("<%= hdnIds.ClientId %>");

if (hdnIds.value.length == 0) {
hdnIds.value = ",";
}

if (objchk.checked == true) {
hdnIds.value = hdnIds.value + HId + ",";

}
else {

hdnIds.value = hdnIds.value.replace("," + HId + ",", ",")
}
}
</script>






    'Bind this function like     On GridView's RowDataBound Event.
Private Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
Dim chkSelect As CheckBox = CType(e.Row.FindControl("chkSelect"), CheckBox)
If Not chkSelect Is Nothing Then
chkSelect.Attributes.Add("onclick", "GetSelected(this,'" & DataBinder.Eval(e.Row.DataItem, "HId").ToString() & "');")
End If
End Sub
'Now you have comma seperated values of HIds in hdnIds hidden field which you can use accordingly

No comments: