Unnikrishnan.S
Unnikrishnan.S

Reputation: 185

How to access content page control from master page in asp.net c#?

I have a literal Control in my default.aspx, and i want to access this from my masterpage.When i try to do this,i got null exception error(object reference missing).

Upvotes: 0

Views: 864

Answers (1)

Daniel Forslund
Daniel Forslund

Reputation: 241

You can use FindControl in your master page.

Content page:

<asp:Literal runat="server" ID="myLiteral"></asp:Literal>

And in your master page load (or other place):

protected void Page_Load(object sender, EventArgs e) {
    var myLiteral = ContentPlaceHolder1.FindControl("myLiteral");
    if (myLiteral != null) {
        ((Literal)myLiteral).Text = "Hello World!";
    }
}

Where ContentPlaceHolder1 is the ID of your placeholder in the master page.

Upvotes: 1

Related Questions