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

6 Comments

  • 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!!

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

  • 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.

  • 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?

  • 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.

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

Comments have been disabled for this content.