为继承masterpage的页面设置defaultbutton和defaultfocus

一个普通的aspx页面,我们可以很容易地为其设置设置defaultbutton和defaultfocus,具体如下:

.aspx:

 <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" Text="Button1" runat="server" OnClick="Button1_Click"></asp:Button>
            <asp:Button ID="SubmitButton" Text="Submit" runat="server" OnClick="SubmitButton_Click">
            </asp:Button>
        </div>
    </form>

.cs:

 protected void Page_Load(object sender, EventArgs e)
    {
        this.Form.DefaultButton = SubmitButton.ID ;
        this.Form.DefaultFocus = TextBox1.ID;
    }

但是如果我们的页面是继承于一个masterpage的,形如:

.aspx:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" Text="Button1" runat="server" OnClick="Button1_Click"></asp:Button>
    <asp:Button ID="SubmitButton" Text="Submit" runat="server" OnClick="SubmitButton_Click">
    </asp:Button>
</asp:Content>

.cs: 

protected void Page_Load(object sender, EventArgs e)
    {
        this.Form.DefaultButton = SubmitButton.ID;
        this.Form.DefaultFocus = TextBox1.ClientID;
    }
在执行时就会提示出现“The DefaultButton of 'form1' must be the ID of a control of type IButtonControl. ”的异常;解决的办法是:在这个页面,用一个panel控件把textbox, button包起来,比如:

.aspx:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:panel id="panel" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" Text="Button1" runat="server" OnClick="Button1_Click"></asp:Button>
        <asp:Button ID="SubmitButton" Text="Submit" runat="server" OnClick="SubmitButton_Click">
        </asp:Button>
    </asp:panel>
</asp:Content>

.cs:

protected void Page_Load(object sender, EventArgs e)
    {
        panel.DefaultButton  = SubmitButton.ID;
        this.Form.DefaultFocus = TextBox1.ClientID;
    }

 

posted on 2008-10-23 10:05  Good life  阅读(618)  评论(2编辑  收藏  举报