Gloria
Gloria

Reputation: 1325

How to Find a Control on a Master page from a User Control

I am trying to change the CSS attributes of a DIV on a Master Page from a User Control but I haven't succeeded so far. The Codes are as follows:

MasterPage

 .....
 <div class="BGfixed" id="masterpageBody" runat="server"></div>
 .....

Content Page

<%@ Page Title="" Language="C#" MasterPageFile="~/main.Master" AutoEventWireup="true" CodeBehind="background.aspx.cs" Inherits="bardaba.Members.background" %>

<%@ Register TagPrefix="uc" TagName="editingPages" Src="~/UserControls/editingPages.ascx" %>
 ....
 <uc:editingPages id="editingPages1" runat="server" />

User Control - Code Behind

 HtmlGenericControl background_image = (HtmlGenericControl)Master.FindControl("masterpageBody");
            background_image.Attributes.Add("style", "background-image:url(/Members/images/BG/" + imgBG_user + ")");

Since the Master Page doesn't exist in the context of the User Control then I'm getting an error. How can I refer to the Master Page from the User Control other than Master.FindControl...?

Thanks

Upvotes: 0

Views: 2373

Answers (1)

Alireza
Alireza

Reputation: 5056

Try this.Page.Master:

HtmlGenericControl background_image = (HtmlGenericControl)Page.Master.FindControl("masterpageBody");
background_image.Attributes.Add("style", "background-image:url(/Members/images/BG/" + imgBG_user + ")");

Upvotes: 3

Related Questions