This tutorial will demonstrate how to give a control focus when the page is loaded using ASP.NET 4.0 and C#.
Setting a Control's Focus
To demonstrate how to give a specific control focus when a page is loaded, we will create a simple web site with a textbox on it. 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 some code that will set the focus to the textbox we added. To do this, open up Default.aspx.cs for editing and add in the following code to the Page_Load event method:
Now when your page is loaded, the textbox will have focus so you will be able to start typing in it immediately without having to click on it.
Focusable Controls
We have demonstrated how easy it is to a give a control focus using ASP.NET and C#. However, the catch is that only certain controls can actually have focus. These controls are as follows:
Giving focus to a control can be an easy way to indicate to the user where to begin when filling out forms on your web site.
To demonstrate how to give a specific control focus when a page is loaded, we will create a simple web site with a textbox on it. 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.
Now that we have a simple form setup, we need to add some code that will set the focus to the textbox we added. To do this, open up Default.aspx.cs for editing and add in the following code to the Page_Load event method:
protected void Page_Load(object sender, EventArgs e) |
Now when your page is loaded, the textbox will have focus so you will be able to start typing in it immediately without having to click on it.
Focusable Controls
We have demonstrated how easy it is to a give a control focus using ASP.NET and C#. However, the catch is that only certain controls can actually have focus. These controls are as follows:
- Button
- LinkButton
- ImageButton
- CheckBox
- DropDownList
- FileUpload
- HyperLink
- ListBox
- RadioButton
- TextBox
Giving focus to a control can be an easy way to indicate to the user where to begin when filling out forms on your web site.
No comments:
Post a Comment