Validating ASP.NET CheckBox Controls

Validation controls in ASP.NET make it very easy to check data in forms if any information has been filled or if it meets certain criteria. It saves a great deal of time in hand coding JavaScript validation. Unfortunately, validation controls don’t work with the CheckBox or CheckBoxList controls. Your form may need to determine if a checkbox has been checked, or if a choice was made among a group of related checkboxes.

I found many solutions from searching on the Internet, but most of them didn’t work very well, if at all, and very few were designed for use with an asp:CheckBox. Many of the solutions used JavaScript or the JQuery framework. If I was using standard HTML controls (i.e. PHP development) I would use JavaScript to validate the controls.

After much trial and mostly error, I decided to find my own solution for validating if at least one checkbox was selected in a group of controls. I used C# code to determine if any one control was checked, followed by code to redirect the page to an anchor (i.e. <a name=”checkbox_controls”>Required</a>) on the current form. This code is placed in the form submission method (i.e. submitButton_Click) and placed before page validation (Page.IsValid), as in the following example

bool bIsValidChecked = CheckBox_Pickup_Option1.Checked || CheckBox_Pickup_Option2.Checked;

if (!bIsValidChecked )
Response.Write (“<script type=\”text/ecmascript\”>window.location.href = ‘#checkbox_controls’;</script>”);

// Set order time and session variables from the order form.

if (Page.IsValid & bIsValidChecked )
{

// form processing code

}

This isn’t the best solution, but its one that works quickly. I need to develop a custom control for validating checkboxes.

How to Connect to a Windows Share with a Different User

When you browse to a folder shared on a Windows machine on a network, you may need permissions to access it. A dialog box appears and request a username and password. If you enter one that doesn’t work, you won’t get the chance to change it the next time you try opening that folder. There are no options in the Windows GUI to change the login information for a network share.

In order to change a user that is logged into a network folder, you need to use the net use command. The first thing that you do is disconnect from the share, which is done with the following command:

     net use \\share /d

This deletes the share being accessed by your computer. Now you can connect to the share with a different user with following command:

     net use \\share\folder password /user:account

where the password is the actual password used by account to log into the shared folder. The account name is a valid Windows user on the machine that your trying to browse to.

With these commands, you can connect to a network share with credentials other than what you logged into your Windows computer as.

Setting a Favicon for Joomla

In an earlier article, I discussed how to create a favicon for your web site (How to Create a Favicon for Your Website). The instructions in this article applied to an HTML site, but it could be used in any CMS by modifying its template.

To create a favicon in Joomla, you need copy the favicon file to the template folder and edit index.php. This folder can be found in the /templates folder. After the favicon has been copied, open index.php and find the <head></head> section of the page. Insert the following code at any point between these two HTML tags:

        <link rel="shortcut icon" href="templates/<?php echo $this->template ?>/favicon.ico" />

The $this->template variable gives the full path to favicon.ico. Open your web site and you should see the favicon in the browser tab and address bar.

Setting Accordion for Closed Panels on Startup

The Accordion in the ASP.NET AJAX Control Toolkit is a very useful tool which creates collapsible panels that open like an accordion when you click on the header. This is a good way to put content on a web page without taking up a lot of space on the page.

The problem with this control is that the first panel is always open. This doesn’t matter if you have multiple panels in the control, but if you just have one panel it doesn’t work well. With a single panel, it should be initially closed until you click on its header.

You can get the Accordion to default as completely closed by setting the SelectedIndex property to –1. The default setting is 0 which opens the first panel when the page is loaded. When you set it to –1, all of the panels will be closed until you click on its header. This setting makes it ideal for single panel use of this web control.

How to Use the “No Follow” HTML Attribute

The major search engines (Google, MSN, and Yahoo!) use a common HTML attribute for adjusting SEO rankings. When these search engines see the No Follow attribute in a hyperlink, they will exclude this link from its indexing and statistics gathering. Contrary to popular opinion, the No Follow attribute will not prevent robots from following the link, its mainly used to prevent search engine from adjusting a web site’s rank with a link that the owner does not want referenced.

You can make any link No Follow by inserting the attribute in the anchor tag:

<a href="www.website.com" rel="nofollow">website</a>

If you want to make all links No Follow on a page, then use the attribute in a meta tag in the <head></head> section of a web site:

<meta name="robots" content="noindex, nofollow">