Saturday, April 5, 2014

Overriding/set default button in master page in asp.net

Some time ago I have written a blog post about Default focus and default button in ASP.NET. In that post I have explained how we can set default button for form. Recently I got email related to that post asking that how we can set or override default button on a page with master page. 

In this page we will learn how we can override default button in master page. So what we are waiting for? Let’s start by creating a an asp.net empty application via File –> New Project –> ASP.NET Empty web application.

overriding-setting-default-button-with-master-page-asp-net

Once You are done with creating project it’s time to add a master page in solution via right click-> Add new Item.

adding-master-page-override-default-button

Following is HTML code ASP.NET master page.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs"
     Inherits="MasterPageDemo.Site1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Now it’s time to add new content page with master page and selecting above master page.

adding-content-page-set-default-button-dynamically-master-page

Following is a code for content page.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
     CodeBehind="Default.aspx.cs" Inherits="MasterPageDemo.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:TextBox runat="server" ID="Message"></asp:TextBox>
    <ASP:Button  runat="server" ID="DefaultButton"
        OnClick="DefaultButton_Click" Text="Default"/>
    <asp:Button runat="server" ID="AnotherButton"
        OnClick="AnotherButton_Click" Text="Another"/>
</asp:Content>
Here I have added two buttons “Default Button” and “Another button” and a textbox. Now I want to make this “Default Button” default dynamically. Below code I have written for both button’s click event. It will only print in textbox which button is clicked.

protected void DefaultButton_Click(object sender, EventArgs e)
{
    Message.Text = "Default Button Clicked";
}

protected void AnotherButton_Click(object sender, EventArgs e)
{
    Message.Text = "Another button clicked";
}

Now I want to make default button as default button of form so whenever I press enter it will fire click event of default button. So to dynamically set / override default button on master page form I have to write following code on content page page_load event.

protected void Page_Load(object sender, EventArgs e)
{
    Form.DefaultButton = DefaultButton.UniqueID;
}

Now when you run and press enter in browser output will be as expected as follows.

default-button-override-asp-net-masterpage-browser

That’s it. Hope you like it. Stay tuned for more..
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews