Now you could easily specify default button/focus on control (ASP.NET 2.0)

In ASP.NET 1.X, if you wanted to make any button as a default or if you wanted to keep a specified text box control as a default focus after page load, it’s not that easy as we have to write JavaScript on page load to achieve this!! But in 2.0, it’s going to be vary simple!

 

Let’s say if you want to keep BtnSubmit as a default and have default focus on TextBox1 control, then all you have to do is, you have to add defaultbutton, defaultfocus attributes in server side form tag!! That’s it, you are done!!

 

 Example:

  <form id="Form1"

    defaultbutton="BtnSubmit"

    defaultfocus="TextBox1"

    runat="server">

 

 After page load the default focus will be in TextBox1 and BtnSumbit will act as a default button! Haa. how simple it is… life is soooo easy now with 2.0 J

Published Monday, November 01, 2004 6:31 AM by SreedharK

Comments

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Monday, November 01, 2004 7:20 AM by Rachit
Yeah, this is awesome. I was trying to different ways to implement this in 1.x but I guess I won't have to work hard on this. Great finding!!

# re:Now you could easily specify default button/focus on control (ASP.NET 2.0)

Sunday, April 10, 2005 6:56 AM by TrackBack
^_^,Pretty Good!

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Monday, October 08, 2007 12:09 PM by anon

lame.  what use is a default button when you only have one form tag?

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Saturday, January 05, 2008 10:00 AM by M.Manmadha Rao

Many people have been giving answers to get focus to a web control in ASP.NET web application, but nobody could clarify my problem for getting focus to a mobile web control in a mobile web form in ASP.NET. I shall be happy to have the code in VB.Net language.

M.Manmadha Rao.

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Monday, July 07, 2008 2:18 AM by kumar Abhinandan

We can not apply this in a master page concept site because there are asp cpntent pages instead of forms .even if we give forms in content pages it gives errors ...is there any way out to get focus on content pages?

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Tuesday, April 07, 2009 1:54 AM by rahul

Setting a default focus is so easy as you specified, but how to deal with the situation where we need to define focus at runtime?

Let say we have a page where we are adding some details to the grid this time the "add" button is visible. so the default focus should be on add button.Now after adding the data in the grid we want to edit it. this time the add button will be invisible and edit button is visible. Now the default focus should be on "Edit" button. How to handel this situation coz i was able to set the default focus only to add button, but at the time of editing the details when i changed the focus of the page, it dint worked for me.

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Saturday, August 28, 2010 1:27 PM by keerthikanth

this was awesome.no need to write several lines to complete this task.thats great learning

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Saturday, August 20, 2011 6:56 PM by araceli

Esto — es insoportable.  

http://eru1.myftp.biz/  

aubrey

# re: Now you could easily specify default button/focus on control (ASP.NET 2.0)

Tuesday, August 23, 2011 3:09 AM by Deepak

But suppose i do not want to set the default focus of the textbox. In that case when i hit the enter key it fires the default submit button event within the page. I am looking  the event is fire only specific condition like when cursor is in a textbox.

I want to bind the enter key event some condition say with some textbox.

any help will be appriciated.

Leave a Comment

(required) 
(required) 
(optional)
(required)