This tutorial will demonstrate how to use the tab index of ASP.NET 4.0 controls to make your web site more use friendly.
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:
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:
Next, we can test this out to see what actually happens on the page when we hit tab. To do this:
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:
To test this out:
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.
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) |
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
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'.
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.
No comments:
Post a Comment