2013年9月9日 星期一

C# ASP.NET form1 的 Default Button

C# ASP.NET form1 的 Default Button


Sample 1
HTML:
<body>
    <form id="form1" runat="server" defaultbutton="Button2">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" /><br />
        <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" /><br />
    </div>
    </form>
</body>

C#:
public partial class Test_tsDefaultButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, typeof(string), "Location", "alert(\"Butonn2\");", true);
    }
}

Sample 2
HTML:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" /><br />
        <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" /><br />
    </div>
    </form>
</body>

C#:
public partial class Test_tsDefaultButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region Default Button
        Button btn = (Button)this.form1.FindControl("Button2");
        this.Form.DefaultButton = btn.UniqueID;
        #endregion
        #region Default Button UserControl Sample
        //Button  btn  =  (Button)UserControl1.FindControl("Button2");
        //this.form1.DefaultButton = btn.UniqueID;
        #endregion
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, typeof(string), "Location", "alert(\"Butonn2\");", true);
    }
}

沒有留言:

張貼留言