Master Page DefaultButton/DefaultFocus

G

Guest

We can set the default button and focus field in ASP.Net 2.0 on the Form tag.
My form tag is in the Master page, but the controls are in containers.

I read through a few articles which did not work.

I added a "runat=server" to make the values visible to the devel. env.

I added properties to the masters codebehind:

Private strDefaultFocus As String
Private strDefaultButton As String

Public WriteOnly Property FormFocusControl() As String
Set(ByVal value As String)
strDefaultFocus = value
End Set
End Property

Public WriteOnly Property FormButtonControl() As String
Set(ByVal value As String)
strDefaultButton = value
End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Me.frmMaster.DefaultButton = strDefaultButton
Me.frmMaster.DefaultFocus = strDefaultFocus
End Sub


In the Container, I set the default to a container control name:

CType(Master, Generic).FormButtonControl = "ibtnNext"

The button is of type asp:ImageButton.

When I run the page, I get "The DefaultButton of 'frmMaster' must be the ID
of a control of type IButtonControl."

I hope the resolution is as easy as placing the Generic assignment to a
different area. I have watched the Generic.Page_Load execute AFTER the
Container.Page_Load, so I assumed the controls were available.

Thank you
 
B

Bruno Alexandre

DefaultButton and DefaultFocus works in any Panel as well...

<asp:panel id="p" runat="server" defaultbutton="btnSave">
<asp:button id="btnSave" runat="server" text="Save" />
</asp:panel>
 
S

Steven Cheng[MSFT]

Hello John,

From your description, you have some TextBox and Button controls in a
concrete page(apply a certain master page) and you're wondering how to set
the entire page form's "DefaultFocus" and "DefaultButton" as the certain
Textbox and Button in content page, correct?

Based on my research, if you want to set "DefaultButton" and "DefaultFocus"
of Htmlform in master page case, you need to use code to do it
programmatically. Also, the value you specified for the
HtmlForm.DefaultButton and HtmlForm.DefaultFocus are also different from
normal scenario. here is a blog article mentioned this(in the comments)

#Default Focus, Buttons and Validation Errors with ASP.NET 2.0
http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx

I have tested on my local environment, and here is what you need to do in
content page:

** use control.UniqueID for HtmlForm.DefaultButton
** use control.ClientID for HtmlForm.DefaultFocus

here is the complete code of the test content page:

========content aspx==========
<%@ Page Language="C#" MasterPageFile="~/masters/site.master"
AutoEventWireup="true" CodeFile="concretepage1.aspx.cs"
Inherits="concrete_concretepage1" "%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" /><br />
<hr />
<asp:panel ID="Panel1" runat="server" Height="50px" Width="125px"
BorderStyle="solid" BorderColor="black">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click"
Text="Button" />
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="http://static.asp.net/asp.net/images/MicrosoftASPNET.gif"
OnClick="ImageButton1_Click" /></asp:panel>
</asp:Content>

=========content codebehind========
public partial class concrete_concretepage1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

Page.Form.DefaultButton = ImageButton1.UniqueID;
Page.Form.DefaultFocus = TextBox2.ClientID;

}


protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Button1_Click..........");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("Button2_Click..........");
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Write("ImageButton1_Click..........");
}
}
===========================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top