- // MasterPage.master
- <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:LinkButton ID="LinkButton1" runat="server" Text="Click Me" OnClick="LinkButton1_Click" />
- <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server" />
- </div>
- </form>
- </body>
- </html>
- // MasterPage.master.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class MasterPage : System.Web.UI.MasterPage
- {
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- if (ContentPlaceHolder1.FindControl("Label1") != null)
- {
- Label lbl = (Label)ContentPlaceHolder1.FindControl("Label1");
- lbl.Text = "Label updated from master page";
- }
- }
- }
- // Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master" %>
- <asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
- <asp:Label ID="Label1" runat="server" />
- </asp:Content>
ASP.Net - C# - Update Content Label Text From Master Page
The following code snippet demonstrates how to update the Text property of a Label control located in a page which uses a master page from within the master page code behind file.
No comments:
Post a Comment