Master页面:
在Master 页面 设置 Label1.Text = System.Guid.NewGuid().ToString();
引用页面:
用 Master.FindControl() 的方法找出控件
1 <%@ Master Language="C#" %>
2
3 <script runat="server">
4 protected void Page_Load(object sender, EventArgs e)
5 {
6 if (!Page.IsPostBack)
7 {
8 Label1.Text = System.Guid.NewGuid().ToString();
9 }
10 }
11 </script>
12
13 <html xmlns="http://www.w3.org/1999/xhtml" >
14 <head id="Head1" runat="server">
15 <title>My Company Master Page</title>
16 <asp:ContentPlaceHolder id="head" runat="server">
17 </asp:ContentPlaceHolder>
18 </head>
19 <body>
20 <form id="form1" runat="server">
21 <table cellpadding="3" border="1">
22 <tr bgcolor="silver">
23 <td colspan="2">
24 <h1>My Company Home Page</h1>
25 <b>User's GUID:
26 <asp:Label ID="Label1" Runat="server" /></b>
27 </td>
28 </tr>
29 <tr>
30 <td>
31 <asp:ContentPlaceHolder ID="ContentPlaceHolder1"
32 Runat="server">
33 </asp:ContentPlaceHolder>
34 </td>
35 <td>
36 <asp:ContentPlaceHolder ID="ContentPlaceHolder2"
37 Runat="server">
38 </asp:ContentPlaceHolder>
39 </td>
40 </tr>
41 <tr>
42 <td colspan="2">
43 Copyright 2008 - My Company
44 </td>
45 </tr>
46 </table>
47 </form>
48 </body>
49 </html>
引用页面:
1 <%@ Page Language="C#" MasterPageFile="~/Listing 05-08.master" Title="Untitled Page" %>
2
3 <script runat="server">
4
5 protected void Page_LoadComplete(object sender, EventArgs e)
6 {
7 Label1.Text = (Master.FindControl("Label1") as Label).Text;
8 Master.FindControl("");
9 }
10
11 protected void Button1_Click(object sender, EventArgs e)
12 {
13 Label2.Text = "<b>Hello " + TextBox1.Text + "!</b>";
14 }
15 </script>
16
17 <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
18 </asp:Content>
19
20 <asp:Content ID="Content2" ContentPlaceHolderId="ContentPlaceHolder1"
21 runat="server">
22 <b>Your GUID number from the master page is:<br />
23 <asp:Label ID="Label1" runat="server" /></b><p>
24 <b>Enter your name:</b><br />
25 <asp:Textbox ID="TextBox1" runat="server" />
26 <br />
27 <br />
28 <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /><br />
29 <br />
30 <asp:Label ID="Label2" runat="server" Font-Bold="True" />
31 </asp:Content>
32
33 <asp:Content ID="Content3" ContentPlaceHolderId="ContentPlaceHolder2" runat="server">
34 <asp:Image ID="Image1" runat="server" ImageUrl="Wrox.gif" />
35 </asp:Content>