A master page that creates a GUID on the first request (C#) : Master page « User Control and Master Page « ASP.Net






A master page that creates a GUID on the first request (C#)

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Untitled Page" %>

<script runat="server">

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        Label1.Text = (Master.FindControl("Label1") as Label).Text;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label2.Text = "<b>Hello " + TextBox1.Text + "!</b>";
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="server">
    <b>Your GUID number from the master page is:<br />
    <asp:Label ID="Label1" Runat="server" /></b><p>
    <b>Enter your name:</b><br />
    <asp:Textbox ID="TextBox1" Runat="server" />
    <asp:Button ID="Button1" Runat="server" Text="Submit" 
     OnClick="Button1_Click" /><br />
    <br />
    <asp:Label ID="Label2" Runat="server" />
</asp:content>

<asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder2" Runat="server">
        <asp:Image ID="Image1" Runat="server" ImageUrl="http://www.java2s.com/style/logo.png" />
</asp:Content>

File: Default.master

<%@ Master Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Label1.Text = System.Guid.NewGuid().ToString();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>My Company Master Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h1>My Company Home Page</h1>
                    <b>User's GUID:&nbsp;&nbsp;
                        <asp:Label ID="Label1" Runat="server" /></b>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" 
                     Runat="server">
                    </asp:ContentPlaceHolder>
                </td>
                <td>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" 
                     Runat="server">
                    </asp:ContentPlaceHolder>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    Copyright 2006 - My Company
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

 








Related examples in the same category

1.Nested master page (C#)
2.Use master page (C#)
3.Master page with trace (C#)
4.Two Master Pages (C#)
5.Link content page with master page
6.Master page with Default Content
7.Master in page derivative
8.Two pieces of contentplaceholder
9.master for print
10.Control title in master page with content page code
11.Nested master page
12.Use image to fill the filler in master page
13.Code behind and master page
14.Specifying master page in Web.config causes every content page to inherit from the master page
15.Specifying the master page for a specific folder in the Web.config file: Use
16.A master page that creates a GUID on the first request (VB)
17.A master page that exposes a custom property (C#)
18.A master page that exposes a custom property (VB)
19.Exposing a server control from a master page as a public property (C#)
20.Exposing a server control from a master page as a public property (VB)
21.Overriding some default content in the content page (C#)
22.Overriding some default content in the content page (VB)
23.Using Page_PreInit to assign the master page programmatically (C#)
24.Using Page_PreInit to assign the master page programmatically (VB)
25.A content page that can work with more than one master page
26.Update the page properties in master page
27.Nested master
28.Put site navigation in the master page
29.Master pages exposing an object model for content pages to programmatically modify elements on the master.