Set default focus and default button in ASP.NET forms

Sometimes it may require to set default focus to an element on a web page. For example if you have a registration form in your web page, ...


Sometimes it may require to set default focus to an element on a web page. For example if you have a registration form in your web page, with lot of text boxes to be filled in, it will be easy for the user to start typing instantly when the page had loaded instead of moving the cursor towards the first text box to get focus. Also if there is a "submit" button which an user should click on to fire the underlying click event, it would be more desirable for the user to do that by pressing "ENTER" button instead of moving the cursor on to the button and then click on it.

It is very simple and easy to implement this in ASP.NET forms. Consider you have three text boxes and a 'submit' button in your page. When the page loads you want focus to be on the first text box and pressing "ENTER" on your keyboard should trigger the submit button's click event.

<form id="form1" runat="server">
Name :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Age :
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
Gender:
<asp:TextBox ID="TextBox3" runat="server" Height="23px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" />
</form>




To set focus to the first text box and to set submit button as default button of the page simply edit the form tag and add two attributes - defaultbutton, defaultfocus. See the code below.

<form id="form1" runat="server" defaultbutton="Button1" defaultfocus="TextBox1">

Now when the page loads the cursor will be on the first text box enabling the users to type instantly and pressing enter will submit the page.


I hope this post helped you to understand how to set default focus to elements in your asp.net web application. Please leave your comments and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading this!!



Subscribe to GET LATEST ARTICLES!


Related

Dot Net 5640594804835631714

Post a Comment

  1. I am aware that people like you, are naturally intuitive, resourceful and already have the ability to heal yourself and transform into the person you want to be, you just need assistance to tap into your own resources.
    토토사이트
    온라인경마
    경마사이트

    ReplyDelete
  2. We can help with research, write a chapter best ewriter or the whole work from scratch, revise and edit your text. These are the features your paper acquires with us.

    ReplyDelete
  3. One of the reasons why logo designs in London stand out is the emphasis on customization. Designers work closely with clients to understand their brand vision and create logos that resonate with their target audience. This personalized approach ensures that each logo is unique and memorable. 🎯

    ReplyDelete

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

SUBSCRIBE


Hot in weekRecentComments

Recent

Spring Security 4 for Spring MVC using Spring Data JPA and Spring Boot

I have been writing a series of tutorials on using Spring Security 4 in Spring MVC application starting from the basic in-memory authentication. In this post, I am writing a step by ste...

Spring Security JDBC Authentication with Password Encryption

I published a basic level tutorial on how to implement JDBC Authentication and Authorization using Spring Security last week. There are few best practices to be followed while implementing secur...

Spring Security 4 - Simple JDBC Authentication and Authorization

In one of my articles, I explained with a simple example on how to secure a Spring MVC application using Spring Security and with Spring Boot for setup. I am going to extend the same example to ...

Java String Split with Pipe Character Not Working - Solution

If you are working on Java, you might have run into this issue when you try to split a string based on a pipe character ("|"). It simply won't work. Split method in Java takes regex as an argumen...

Comments

We Care India:

These features are super useful, and the design is so Keyword!

DigitalSaga:

Another great post! I'm always excited to see a new article from you in my feed. PhD The...

Kajal95:

It's work for me, thanks a lot.
href=https://iimskills.com/medical-coding-courses-in-delhi/>Medical Coding Courses in Delhi

new york roofing company:

Great article! Really helpful insights—just like quality matters in content, it’s also key in choosing trusted New York Roofing services

Ninja:

You always deliver high-quality content—this post is no exception.
seo agency in gwalior

item