How to Set DefaultButton and DefaultFocus when Using MasterPage in ASP.Net ?
From ASP.Net 2.0, some of the ASP.Net controls have 2 new properties called DefaultButton and DefaultFocus. Setting this property like below will make the Button1 as default button for the page and TextBox1 will be focussed by default.
protected void Page_Load(object sender, EventArgs e) { Page.Form.DefaultButton = Button1.ClientID; Page.Form.DefaultFocus = TextBox1.ClientID; }
But the above code will throw the below error when you have a master page associated with your page.
The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.
To reslove this error, you have to change the above code to make the DefaultButton property to UniqueID. It is because, ASP.Net will understand only the UniqueID in order to raise the control's event. Hence, the below code should do.
protected void Page_Load(object sender, EventArgs e) { Page.Form.DefaultButton = Button1.UniqueID; Page.Form.DefaultFocus = TextBox1.ClientID; }
Happy Coding!!
|