Creating a Sample Form
To demonstrate how to work with the tab index property, we will create a simple form with a few text boxes that will allow us to tab around in a specified order. At this point, I have created a new ASP.NET Empty Web Site. To begin:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a textbox onto the web form.
- Add 2 break lines.
- Drag and drop a textbox under the break lines.
- Add 2 break lines.
- Drag and drop a textbox under the break lines.
I just signed up at
Server Intellect and couldn't be more pleased with my
fully scalable & redundant cloud hosting! Check it out and see for yourself.
Now that we have a simple form setup, we need to add in some simple code to set the focus to the first text box on our page. This will allow us to start tabbing through the controls on our page immediately without selecting other controls in your web browser. To do this, open Default.aspx.cs up for editing and add in the following code to the
Page_Load event method:
protected void Page_Load(object sender, EventArgs e) { //set focus to the first textbox TextBox1.Focus(); }
|
Next, we can test this out to see what actually happens on the page when we hit tab. To do this:
- Load up the web site.
- Hit tab.
- Notice that your focus was changed to the second textbox.
- Hit tab.
- Notice that your focus was change to the third textbox
Your focus changes in a specific order that corresponds to the order in which the controls are added to the page because no tab index is specified for any of our text boxes.
Modifying the Tab Index
In certain scenarios, you may want to create a form that has a specified order in which the focus will be changed when the user hits the tab key. We have complete control over this using the
TabIndex property of our controls. To change the tab order of our controls, open Default.aspx to
design mode and:
- Select TextBox1.
- Change the TabIndex property to '1'.
- Select TextBox2.
- Change the TabIndex property to '3'.
- Select TextBox3.
- Change the TabIndex property to '2'.
Need help with
cloud hosting? Try
Server Intellect. We used them for our
cloud hosting services and we are very happy with the results!
To test this out:
- Load up the web site.
- Hit tab.
- Notice that your focus was changed to the third textbox.
- Hit tab.
- Notice that your focus was changed to the second textbox.
Properly configuring the tab order of your controls can greatly help your users fill out forms quickly and easily.