How Do I Set the DefaultFocus or DefaultButton in a Page Based on a Master Page in ASP.NET 2.0?

You may be aware of the new DefaultButton and DefaultFocus properties that a few ASP.NET 2.0 controls, including the HtmlForm have. These properties allow you to determine which button gets "clicked" when you hit enter on the page, and which form control gets the focus when the page loads. (If you're not familiar with them, check out this MSDN help file) But how do you use this feature in a page based on a Master Page?

Your first attempt may be to add the following code in the Page_Load event of the Content page:

  Page.Form.DefaultFocus = "TextBox1"
  Page.Form.DefaultButton = "Button1"

However, as soon as you run this code, you'll get the following error:

Server Error in '/MyApplication' Application.

The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.

If you comment out the second line that sets the DefaultButton, the page loads in the browser, but the focus is not set. If you look at the resulting HTML in the browser, you may start to understand the problem. At the end of the page, you'll see this:

<script type="text/javascript">
  WebForm_AutoFocus('TextBox1');// -->

TextBox1 looks good at first, but if you search for it in the code, you won't find a control with an Id of TextBox1. You do, however, find this:

<input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" 
id="ctl00_ContentPlaceHolder1_TextBox1" />

Because the page is based on a Master Page, the HTML name and id attributes have been prefixed with the names of their naming container, the ContentPlaceHolder1 in this case.

Now how do you get the correct client id inside the call to WebForm_AutoFocus? The trick here is to use the controls ClientID property that returns its long client name. If you change the code in the code behind to the following, the DefaultFocus will work:

Page.Form.DefaultFocus = TextBox1.ClientID

You can apply the same technique to set the DefaultButton but then you should use its UniqueID property:

Page.Form.DefaultButton = Button1.UniqueID

This way, you end up with a form that has the focus on the TextBox1 by default and when you press Enter, the form will post back to the server and the code in the event handler for Button1.Click will fire.

Although in normal pages (not based on a Master Page) you can set the DefaultButton and DefaultFocus properties as a string as you saw in the first code example, it's recommend not to do this. When you always use ControlName.UniqueID or ControlName.ClientID you get compile time checking of the control's name. If you rename the control in the markup of the page (which is a good idea when you call buttons Button1 ;-) ) the compiler will see the change and highlight the offending code; a benefit you don't have when you use text strings.

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .

Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.

Feedback by Other Visitors of Imar.Spaanjaars.Com

On Wednesday, November 30, 2005 6:57:51 PM glen said:
Great !

But How can I set a defaultbutton to a button in a gridview when it is in edit mode?

Thanks in advance.
On Friday, December 02, 2005 9:23:41 PM Imar Spaanjaars said:
This depends on how you want to do it.

One way to do it is to hook into the Grid's RowEditing event, get a reference to the button using FindControl and the get the Button's UniqueID, as per the example above.

Funny thing is, I seem to be getting inconsistent values for the UniqueID. I seem to get the ID for controls in an earlier column. There must be something wrong with my code, but I haven't figured out what.

Anyway, I could set the UniqueID explicitly:

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
  Dim myButton As Button
  ' Replace Rows(0) with the row being edited
  myButton = CType(GridView1.Rows(0).Controls(10).Controls(0), Button)
  ' Next line should return "GridView1$ctl02$ctl08" but doesn't
  ' Form1.DefaultButton = myButton.UniqueID
  Form1.DefaultButton = "GridView1$ctl02$ctl08"
End Sub

This example uses a wide Grid where Rows(0).Controls(10) points to the 11th column.


On Monday, December 05, 2005 1:06:04 PM Glen said:
Thanks for your answer...

I succeed in finding a button in a gridview and setting it as a defaultbutton, but I use an ImageButton and this doesn't work because it's not a IButtonControl.

So I think that the only way to asign a default Image Button when I hit the "Enter" Key is to make the desired action when the enter key is pressed...

Any other idea?
On Monday, December 05, 2005 4:53:09 PM Imar Spaanjaars said:
I haven't tested this yet, but maybe you can add a call to WebForm_AutoFocus yourself and pass it the client ID of your image.

I haven't looked at the code for this method yet, so maybe it won't work but it's at least interesting to try it out....

On Sunday, March 05, 2006 1:22:20 AM Marcio Esteves said:
Man, you are good. Thanks for the clear and pratical approach to the problem.
On Thursday, May 18, 2006 9:39:05 AM Richard said:
Thanks for the that much appreciated.  

Seems OK in IE but it doesn't seem to set the defaultfocus in firefox, is anyone else experiencing the same problem?
On Sunday, May 21, 2006 8:59:33 AM Imar Spaanjaars said:
Hi Richard,

Instead of using the control's UniqueID, you can use its ClientID like this:

Page.Form.DefaultFocus = TextBox1.ClientID

The UniqueID maps to the control's name, while the ClientID maps to its client side ID property. It looks like its better to always use ClientID, so I'll do a bit of testing and then modify the original article to include this.


On Friday, June 09, 2006 8:52:41 AM Daniel said:

When I use the method in the article I still get the error: "The DefaultButton of 'pnGrid' must be the ID of a control of type IButtonControl"
This happens even when I use:
"Me.pnGrid.DefaultButton = ctl00$MainContent$fvFactuurnummers$btnUpdate"

This occurs when I use masterpages in which I have content which contains a panel which in turn contains a Formview with an update button.

Why doesn't it work for me? It does work when I place a button outside the Formview, but inside the panel en refer to that button as being the default.
On Saturday, June 10, 2006 2:44:43 PM Imar Spaanjaars said:
Hi Daniel,

Can you post your code and an explanation of the problem at If you send me the link, I'll take a look.

On Monday, June 12, 2006 7:37:19 AM Daniel said:
Here is the topic. Hopefully that's a bit more clear:
On Tuesday, June 13, 2006 10:03:49 PM Imar Spaanjaars said:
Hi Daniel,

Not sure if you've seen it yet, but I replied to that post....


On Tuesday, June 20, 2006 7:29:41 PM Richard Tang said:
Anybody experienced this error?
    1. Add a DropDownList control, a Textbox control, and a RequiredFieldValidator to a page.
    2. Set the DefaultFocus to a DropDownList control in the code(Page_Load).
        Page.Form.DefaultFocus = DropDownList1.ClientID;
    3. Set the ControlToValidate property of RequiredFieldValidator to theTextbox.
    4. Run the page and then close the browser.

I got the following message:
    iexplore.exe - Application Error
    The instruction at "0x10003e8d" referenced memory at "0x10003e8d". The memory could not be "read".

Is there a fix to this problem?


On Tuesday, June 27, 2006 6:07:56 PM Rick Boardman said:
Great Articles!

DefaultButton and DefaultFocus are working great for me Except...

I have a page derived from a MasterPage.  The MasterPage has buttons and textboxes on it.  The Page has buttons and textboxes on it.

I set DefaultButton to one of the buttons in the Content area.

When the page loads, it's great.  The button in the Content area is in fact the default for the enter key AND visually has focus.

When I manually click on a text box in the Content area, the default for the enter key is still the desired button BUT the first button in the master page visually has focus.

If I use defaultfocus to make the first textbox in the Content area have focus, it acts like when a user clicks it.  The defaultbutton is the default for the enter key but the visible focus is on the first button in the master page.

Anyone else experiencing this or know a way to fix it?
On Tuesday, September 05, 2006 10:47:41 AM Chiko said:
Thanks! Very helpful
On Tuesday, October 10, 2006 1:44:24 PM The Emperor said:
Maaaaaaaan You ruuuuuuuuuuuuuule !!!!!!!!!

I tried to solve the problem in ASP.NET with the Enter Key when I use Master Pages and your solution worked !!!!

After hours and hours of searching, reading and trying I was able to solve the problem thanks to you!!!
On Saturday, November 18, 2006 4:16:11 PM ameya said:
Cool .... But i m having a problem... i have a Login Control of 2.0 on the Page and a link called LogOut on the master Page. when i type my username and the password on the login control and press enter the focus goes on to LogOut Of the master Page and i m logged out even though i have correct username and password
On Sunday, November 19, 2006 5:02:13 AM Garfield Lindo said:
Is there a way to change the DefaultButton in JavaScript.  For example I have 2 buttons on a page.  The form's default button is property is set to button 1, however if the user inputs data into a textbox I'd like to change the defaultbutton to button 2 in Javascript.  So on the onblur event of the textbox I would like to set the defaultbutton property for the form.  I hope this is clearn.
On Sunday, November 19, 2006 11:23:37 AM Imar Spaanjaars said:
Hi Garfield ,

Glad you found the other article about DefaultButtons on my site. In case anyone else is interested, here's a link to it:


On Sunday, November 19, 2006 11:24:28 AM Imar Spaanjaars said:
Hi ameya,

Did you put the focus on the Login button with the techniques mentioned in this article?

On Tuesday, December 12, 2006 12:16:03 AM RayBand said:
Great article.  I am having a problem though with setting a panel's defaultbutton property.  The panel is on a page that derives from a masterpage and the panel contains a ASP:Login control.  Because the login control contains its own button it is not easily accessable in prerender to get the client or unique id.  To accomplish this I do a findcontrol:
     Button btn = (Button)lgnMain.FindControl("btnLogin");
     pnlLogin.DefaultButton = btn.ClientID;

I have tried Unique, Client, and the regular ID property but all of them seem to give the same error "The DefaultButton of 'pnlLogin' must be the ID of a control of type IButtonControl."  

I know that the findcontrol is returning the login button but the IDs do not seem to be valid for a defaultbutton parameter.  Any help is much appreciated.  Thanks!
On Tuesday, December 12, 2006 6:42:32 PM Imar Spaanjaars said:
Are you sure that FindControl("btnLogin") returns a valid button? I guess it does otherwise the code would crash.

Haven't run into this myself yet. Looks like the ASP.NET framework has trouble finding the very button that you can find.

Sorry, can't suggest much at this point....

On Monday, February 19, 2007 12:51:23 PM tim mackey said:
hi, i thought you might like to know that your site crashes firefox every time i visit it.  it appears to be a javascript problem because if i turn off javascript then it works ok.  i'm running Vista Ultimate x86.  
On Monday, February 19, 2007 2:01:05 PM Imar Spaanjaars said:
Hi tim,

Thanks for that. Are you running any special plugins?

I am running FireFox here as well, and it never crashed on me....

On Monday, February 19, 2007 2:38:33 PM tim mackey said:
hi imar,
ah yes you're absolutely right.  it is a problem with the firebug extension.  apologies for implying that your site had a problem!  
good luck
On Friday, April 13, 2007 10:23:38 PM mary said:
Is there a way to use the unique id to act on a selected value for a dropdownlist?  My logic for selectedindexchanged doesn't work because of the naming put in place by the contentplaceholder.
onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$DropDownList1\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_DropDownList1"
On Saturday, April 14, 2007 7:41:35 AM Imar Spaanjaars said:
Hi mary,

I don't understand what you're asking. What do you mean with "act on a selected value for a dropdownlist"? What are you trying to accomplish?

On Monday, April 16, 2007 5:21:42 PM mary said:
thanks, for following up, I figured out my problem
On Tuesday, July 10, 2007 3:52:58 PM Pradeep Kini said:
Hello Imar,

First of all let me thank you for such a detail explanation of the issue.
I have tried your solution for setfocus to textbox etc using the Page.From.DefaultFocus = TextBox.ClientID and it seems to be doing nothing, no error but the focus doe snot happen.

Let me explain my scenario and the things that I have already tried:
I am on .Net 2.0 with latest verion of AJAX.
The page is based off of a master page. The textbox is inside an user control which is inside a TabPanel. The User Control is encapsulated with a AJAX UpdatePanel. If I try the above code to setfocus to adefault textbox, the application does nothing. No error, nothing. If you see the view source I do see the code as given below but it does not do the job of setting the focus. Is there anything else you can suggest. You can see below that the clientid of the textbox is correct w.r.t. its control hierarchy.

<snip> Cut by Imar </snip>

The other things that I have tried and have not been successful:
document.getElementById(" & Chr(34) & Me.TBLastName.ClientID() & Chr(34) & ").focus
This one gives a javascript error indicating that the application is trying to setfocus to a control that is not enabled, but that is not true as the textbox is enabled and I can manually setfocus and type in it.
ScriptManager.RegisterClientScriptBlock(Page, sender.GetType(), "FocusScr", "WebForm_AutoFocus('" + TBLastName.ClientID + "');", True)
This gives an error in indicating object required.

Any help or direction to tackle this issue will be appreciated.

Thanks in advance

On Tuesday, July 10, 2007 7:06:57 PM Imar Spaanjaars said:
Hi Pradeep,

Hard to tell why it isn't working without seeing your code.

Regarding your issues:

1. This doesn't look right. You're mixing JavaScript with a server side ClientID. Try using something like <%=TBLastName.ClientID%> instead or use an <asp:Literal> control.

2. Again, hard to tell without seeing your code and without knowing which variable caused the null reference.

May I suggest you post this on a forum like

Additionally, take a look here: Maybe you're using Validators?


On Wednesday, July 11, 2007 1:42:23 PM Pradeep said:
Thanks Imar for your response.

The first item I had indicated is actually a server side "RegisterStartupScript" block that registers the javascript :document.getElementById(" & Chr(34) & Me.TBLastName.ClientID() & Chr(34) & ").focus hence the server side clientid.

Your Talk  Back comment section would not allow me to put all the javascript tags, hence I have stripped it out.

Yes I do have validators on this screen, so it could be the cause.

From what you have given here, I will need to further validate and see if validators are indeed the issue.

I will post back with my finding.

On Wednesday, July 11, 2007 8:35:16 PM Pradeep said:
I found another suggestion on the AJAX message board which did the job, I thought I should share it with you guys here.

I basically registered a client side function "SetFocusFunction" that is invoked thru the SetFocusDelay() function (which is registered in .JS file that is include with all pages).
In the SetFocusDelay() function I call the SetFocusFunction after a timeout [setTimeout("SetFocusFunction()", 1000);], what this does is, it gives the browser the time to render the control before the setfocus code is fired.

Because the rendering had not yet happened (I think because of UpdatePanel and Tab Panels coming into Play), my previous implementation tried to put the focus before the control had rendered, which was causing the javascript error. This situation was avoided by the delay logic. This resolved the issue.

I wanted to put the complete code here, but because of HTML and server tags the post is not allowing me to send it with those details.
But for a full solution you can look at this post on the ASP.Net (AJAX) Message board.


On Thursday, July 12, 2007 10:47:51 AM Imar Spaanjaars said:
Hi Pradeep,

Thanks for posting a follow up; very useful.


On Thursday, July 12, 2007 11:19:17 AM Anksunamon said:
I have an easier solution using AJAX :

Use the scriptmanager to set the focus to the textbox like this :


Where i_sm is the ID of the ScriptManager in the MasterPage,
i_fv the ID of a FormView or any other control containing the textbox,
i_tbNom the ID of the Textbox To focus.

Anyone has the solution for the dynamically Setting the DefaultButton of a panel? (The only way to generate the DefaultButton if Using AJAX ?)

In this thread :

Thanks in advance !
On Saturday, July 14, 2007 12:34:49 PM Beach200 said:
Pradeep: don't forget the trailing brackets : focus()
On Thursday, October 04, 2007 6:00:13 PM Patrick Hauck said:
Great tip. How do I set the default focus to a login control that is part of the Master Page?
On Thursday, October 04, 2007 8:06:54 PM Imar Spaanjaars said:
Hi Patrick,

Pretty much identical to what you saw here, except that you need to use FindControl on the Login control to find the child control you want to set the focus to.

On Thursday, October 04, 2007 10:53:47 PM Patrick Hauck said:
Thanks Imar.  That's what I tried, but no luck.  I was putting the code in the page derived from the Master, not the Master page code. Correct?
On Friday, October 05, 2007 6:04:19 AM Imar Spaanjaars said:
Hi Patrick,

Well it depends; if you always want the code to fire, then the master page is a good location too.

On Wednesday, October 31, 2007 8:01:58 PM Aftab said:
Thanks for the nice article. I know that the new panel feature in 2.0 is having a property of default button but I was wondering that if this feature is there in Asp.Net 1.0
It would definitely be a great help if that was possible in older versions too ^_^

http://seotips 50webs com
On Wednesday, October 31, 2007 8:11:54 PM Imar Spaanjaars said:
Hi Aftab,

Follow the link to the "MSDN help file" at the top of this article. The MSDN docs always state in what version a feaure was introduced.


On Monday, November 19, 2007 8:01:49 PM Adam Kahtava said:
Thanks Imar, you save me some valuable time yet again.

-Adam Kahtava []
On Sunday, January 06, 2008 2:39:26 AM Chirag said:
Awesome article. I spend hours trying to figure out.
On Thursday, January 17, 2008 5:18:08 PM Fahim said:
thank you so much! a lot of people have some long winded solutions but yours is the most simple and elegant. well written article.
On Tuesday, February 19, 2008 8:54:05 PM jun Penalosa said:
Thanks.... this resolves my problem...

God Bless!
On Wednesday, March 05, 2008 9:18:07 AM Rose said:
Thanks a lot. Your article solved my issue of default focus at last. Thank you once again.
On Thursday, March 20, 2008 6:12:35 PM Chris Messineo said:
Great article.  Very helpful.
On Monday, April 14, 2008 11:16:16 AM Naveen Nair said:
Hello frnz, I am trying to set a confirm box in my .cs file.

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Success", "if(confirm('" + message + "')){ " + btn_download.ClientID + ".click(); }", true);

i even tried to replace btn_download.ClientID with document.getElementByID('<%=" + btn_download.ClientID + "%>').click()

but its not working. the reason i m doing it is because

if the user clicks ok then i want to fire a button click event which may have the following code :

Response.AddHeader("Content-disposition", "attachment; filename=" + hdn_filename.Value);
            Response.ContentType = "application/XML";

i.e. which asks users for a open/save download box.
On Monday, April 14, 2008 11:23:35 AM Imar Spaanjaars said:
How is this related to DefaultFocus or DefaultButton????

On Wednesday, May 07, 2008 7:10:50 AM eswar said:
if u are using Master page,


On Tuesday, June 10, 2008 7:05:50 PM johnlagan said:
Hi Imar

Many thanks for info on default focus.

However I'm having trouble getting both the UserName and Answer textboxes set to focus.

I can set one or another to focus. But I can't simply get the Username textbox to focus on load in stage one followed by the Answer textbox (in focus) in stage two.

Driving me nuts! (I'll probably add some javascript, but should be possible in ASP).

My PasswordRecovery control is in a content page.

Hope you can find time to provide solution.

PS. I reckon I can pretty much set focus on textbox in any other control, it's the PasswordRecovery control that bugs me.
On Tuesday, June 10, 2008 9:06:46 PM Imar Spaanjaars said:
Hi johnlagan,

You probably need FindControl on the PasswordRecovery control to find a reference to the relevant TextBox so you can set the focus on it.

Hope this helps,

On Wednesday, June 11, 2008 3:42:29 PM johnlagan said:
Hi Imar (again)

Thanks for your prompt reply.

I wasted a few hours yesterday with setting default focus for the PasswordRecovery control - my system had the gremlins - problem was resolved by reloading VWDE 2008 - Thanks for your help, of course your suggestion for content pages works.

It seems to me that Microsoft should have a DefaultFocus property for all input controls, such that the user could simply add DefaultFocus="true" in the control declaration.

For the PasswordRecovery control I've had to add the following code in code behind page:

      protected void Page_Load(object sender, EventArgs e)
          if (!IsPostBack)
              Page.Form.DefaultFocus = PasswordRecovery1.FindControl("UserNameContainerID$UserName").ClientID;

      protected void PasswordRecovery1_VerifyingUser(object sender, LoginCancelEventArgs e)

          MembershipUser user = Membership.GetUser(PasswordRecovery1.UserName, false);
          if (user != null)
              Page.Form.DefaultFocus = PasswordRecovery1.FindControl("QuestionContainerID$Answer").ClientID;

              Page.Form.DefaultFocus = PasswordRecovery1.FindControl("UserNameContainerID$UserName").ClientID;


What a pain focus method is still, even in ASP3.5.

All the best, John
On Wednesday, August 27, 2008 7:06:20 AM sreenu vadluri said:
thanks its working fine.
On Friday, September 05, 2008 11:30:05 PM Martin said:
I am using a Master page with ASP.NET 3.5 and none of the solutions work.  It appears the Default Button works, but not the focus. Nothing is focused on the page when the login loads.

I have tried swapping back and forth between ClientID and UniqueID.  Here is the code at the bottom of the page:


It seems a bit odd the // is on the same line.

Here is my control code for the Login Text Box:

[input name="ctl00$ContentPlaceHolder1$Login1$UserName" type="text" id="ctl00_ContentPlaceHolder1_Login1_UserName" /]
On Saturday, September 06, 2008 7:39:22 AM Imar Spaanjaars said:
Hi Martin,

Are you calling this code in Debug mode in VS 2008? I am finding that it sometimes takes the focus away when you debug so things don't seem to work while they actually do work properly. With the following code in a Content Page:

Page.Form.DefaultFocus = Login1.FindControl("UserName").ClientID ;
// Or use this. Works the same, less flexible
// Page.Form.DefaultFocus = "ctl00_ContentPlaceHolder1_Login1_UserName";

I can get the focus every time I right-click the file in the Solution Explorer and choose View in Browser.


On Monday, September 08, 2008 2:10:52 PM Martin said:
Yes, I was calling in debug mode. I tried View in browser and it works now.  Thanks!
On Thursday, October 16, 2008 6:24:51 AM Mithun Narayanan said:
Thank u.......
my problem is solved when i tried this one.....

  Button btn1 = (Button)form1.FindControl("btnSearch2");
                    form1.DefaultButton = btn.ClientID;

On Monday, October 27, 2008 7:52:20 PM Moe said:
You are the man, Thankx
On Wednesday, October 29, 2008 2:22:27 PM WebGuru said:
Very useful information, and helped me on the pages.
On Tuesday, January 06, 2009 1:19:21 PM Gopi said:
I have a page derived from a MasterPage.  The MasterPage has Image click event that redirects to home page.  The Page doesnt have any controls like  buttons, textboxes on it.

when I click the enter key in that page it is automatically redirects to the home page ,It is redirecting to masterpage click event.

In page dont have buttons to set the default button,then how to restrict the execution of the master page events

On Tuesday, January 06, 2009 7:48:22 PM Imar Spaanjaars said:
Hi Gopi,

Difficult to say without seeing your code or a real-world example page.

Can you post this on a forum like instead? If you do, please provide relevant code and an example page or people are unlikely to help you.


On Thursday, June 04, 2009 2:59:00 AM Jason said:
You really know your stuff, thanks Imar
On Tuesday, July 07, 2009 11:18:47 AM jaimin said:
thanks its working.......
On Friday, February 26, 2010 4:31:57 PM Leah Naiditch said:
I have a page that is based on a Master Page.  The page has AJAX contolled tabs and on one tab I have 2 panels, each with 1 button.  Only 1 panel is visible at a time.  I am trying to get the button on the visible panel to be the DefaultButton so that when Enter is pressed the click event is fired off.  The issue that I am having is that I get this error no matter what I try:  The DefaultButton of 'form1' must be the ID of a control of type IButtonControl. I have read through everything above and I have tried everything that was suggested with no success.  Any other ideas?
On Friday, February 26, 2010 4:35:25 PM Imar Spaanjaars said:
Hi Leah Naiditch,

What client ID are you assigning? And how does the client ID of the control look in the HTML? Are the two the same?

On Friday, February 26, 2010 4:47:52 PM Leah said:
Hi Imar,

I am working on just getting one of the buttons working right now.  I figure once I figure that out the second one will be pretty easy (I hope).

I put this line in my page load.  
   Page.Form.DefaultButton = btnAdvancedSearch.UniqueID

When I run the application this is what the value of btnAdvancedSearch.UniqueID is equal to

It appears as though it is not stripping off the Master Page information.

BUT if I do this in the Page Load:
Page.Form.DefaultButton = "btnAdvancedSearch"

It sets the DefualtButton to the proper string, but I still get the exact same error as above.
On Friday, February 26, 2010 5:37:07 PM Imar Spaanjaars said:
Hi Leah,

But what is the client ID of the button in the final HTML?

On Friday, February 26, 2010 5:49:07 PM Leah said:
Hi Imar,

I am sorry I am not quite sure what you mean.  Do you mean in the Source?  I get an error page, so the source has the same error as above and no value for btnAdvancedSearch.ClientId exists in the source.

On Friday, February 26, 2010 5:52:55 PM Imar Spaanjaars said:
Hi Leah,

Sure, but have you considered the idea to run the page without the offending code so you can see how the button ends up? Its client ID is key in this scenaio...

On Friday, February 26, 2010 6:07:19 PM Leah said:
Hi Imar,

The client id for this button appears to me to be:


On Saturday, February 27, 2010 10:03:40 AM Imar Spaanjaars said:
Hi Leah,

And? Did you try assigning that ID and see if that worked and then checked the final HTML to see how it ended up? This is going to take years if we do this with these small steps and you're not providing a whole lot of other information.

If this doesn't work, can you please post here:
It's a lot easier to have a discussion like this there as others can join the discussion and it's easier to share code.  If you do post there, be sure to provide enough relevant information and source code.


On Tuesday, March 02, 2010 7:39:23 PM Leah said:
I did try to assign the id value to the defaultbutton property in the panel, but this still gave the same error as before.  I have posted on the other site as you asked me to ...

On Tuesday, March 02, 2010 7:48:07 PM Imar Spaanjaars said:
Hi Leah,

Great. Let's hope someone knows an answer, although I doubt that choosing a VB forum rather than an ASP.NET forum will do you any good. Limited code and final HTML and lack of detailed explanations doesn't make it any better. But, you never know.... ;-)

On Thursday, March 04, 2010 9:35:36 PM Ric Lloyd said:
As always, nothing is as easy as it seems.

If you are coding straight ASP.NET, this solution probably works. If you use the AJAX extenders it doesn't.

I have 3 user controls added at the bottom of the ascx file that are AJAX Popups with DevExpress Library Spell Checking. the last one gets the focus set on its' first input field, even though it isn't displayed when the page renders.


Got any more ideas? If I find one, I'll post it here.
On Friday, March 05, 2010 9:32:13 AM Imar Spaanjaars said:
Hi Ric,

Very difficult to answer without seeing your setup and code. You may want to follow the same advise I gave to Leah.


On Friday, March 05, 2010 4:42:44 PM Ric Lloyd said:
I'll be glad to share code. How? I have a Master Page, main ascx page, and included ascx pages.
On Friday, March 05, 2010 5:10:25 PM Imar Spaanjaars said:
Hi Ric,

Did you *read* the reply to Leah and see the link to ?

If you do post there, be sure to provide a self-contained example that can be run as-is (e.g. with no dependencies on stuff like databases.



BTW: your notification mail bounced so you won't get replies for this post.
On Monday, March 08, 2010 7:18:44 PM Leah said:

I found that with a master page it caused issues as well, but I found a solution!  You can use the onkeypress javascript method to solve this issue.
On Tuesday, July 26, 2011 10:05:52 AM Daniel Ruzo said:
Hi, Imar.

What about using Page.SetFocus() rather than Form.DefaultFocus()? Doing so you just use the control object as the SetFocus argument, and it doesn't matter if you're deriving from a master page or not. Is there any difference in the behavior of this two methods?

This is an example:
On Tuesday, July 26, 2011 12:32:50 PM Imar Spaanjaars said:
Hi Daniel,

SetFocus just sets the focus on a control once, while DefaultButton sets the button that gets "clicked" when you press enter. If you set focus on the button, the user may click in a form control (and thus move the focus away fro the button), leaving you without "enter" behavior.

Hope this helps,

On Thursday, October 20, 2011 10:45:55 AM Gail Bowen said:
Thank you - this solved my problem and saved me lots of time
On Monday, February 20, 2012 7:22:34 AM Manjari said:
How is this way of setting default focus different from AjaxControlToolkit.Utility.SetFocusOnLoad(TxtID) which is used in AJAX?
Here TxtID is the unique id of a given textbox

Thanks in advance
On Wednesday, February 22, 2012 5:46:18 PM Imar Spaanjaars said:
Hi Manjari,

Not much different, except that it doesn't require the AJAX toolkit.

On Wednesday, March 21, 2012 9:17:34 PM Juan Ramirez said:
Thanks for this article. Very nice solution.
On Thursday, March 29, 2012 9:43:19 AM Hadri said:
Its work, but can I use LinkButton as default button instead of Button ? I really need this help. Thanks in advance
On Thursday, March 29, 2012 3:32:08 PM Imar Spaanjaars said:
Hi there,

Have you tried it?

On Friday, July 20, 2012 12:53:51 PM Dipak said:
Default Buttons stops working if you off the browsers javascript.
On Friday, July 20, 2012 2:18:51 PM Imar said:
Yep, that's correct, as they rely on JavaScript....



Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.