ASP.Net – Custom Validator for a Checkbox

There seems to be a lot of people having issues with client-side validation of a checkbox – so one of my colleagues wrote a new custom validator script that works well.

<head runat="server">
<script type="text/javascript">
function valCheckBox(source, args) {

var chk = document.getElementById("chkTest")
if (chk.checked) {
args.IsValid = true;
return;
}
else {
args.IsValid = false;
return;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="chkTest" runat="server" />
<asp:CustomValidator ID="valTest" runat="server" ClientValidationFunction="valCheckBox" Text="*" />
<asp:Button ID="cmdTest" runat="server" Text="Validate" />
</form>
</body>

ASP.NET – Programmatically adding a hyperlink field – “Value of type String cannot be converted to ‘1 dimensional array of String’ (vb) – the solution

We came across an interesting problem when trying to programmatically add a column to a asp:gridview using page_load (vb). It took quite a while to figure out the relatively simple solution, so I thought that I would share it

Original code:

Dim hf As New HyperLinkField
hf.Text = "Delete"
hf.DataNavigateUrlFormatString = "delete.aspx?JobID={0}"
hf.DataNavigateUrlFields = "JobID" ‘It is this JobID as the field name to use for the ID that is causing the issues
Me.gridTable.Columns.Add(hf)

Me.gridTable.DataBind()

And the solution:

Dim hf As New HyperLinkField
hf.Text = "Delete"
hf.DataNavigateUrlFormatString = "delete.aspx?JobID={0}"
Dim flds() As String = {"JobID"} ‘Create a new 1-dimensional string array with one item – “Job ID”
hf.DataNavigateUrlFields = flds
Me.gridTable.Columns.Add(hf)

Me.gridTable.DataBind()

If this helps you out, or you have any queries, please do leave me a comment and let me know!