asp,net using site.master page - c#

I am trying to change the css class attribute that has been present on the site.master page at run time and I cant really get any head way I have so far tired
mainContainer.Attributes.Add("style", "background-image('myImage.png')");
AND
mainContainer.Attributes.Add("class", "className");
BUT non of these let me change the css of the master file at run time. i am using asp.net using c#
this is the code on the master page
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>

ContentPlaceHolder is element wich won't be existed in output html code. It only defines a region. You can try to change div with class "main". Just add runat="server" and id attributes and access from the code.
<div id="MainDiv" class="main" runat="server">
and then
MainDiv.Attributes.Add...

You need to partially load the master page in the other child pages like below...
<%# MasterType VirtualPath="~/Site1.Master" %>
Then in the page load of the child page..put
protected void Page_Load(object sender, EventArgs e)
{
HyperLink contact_menu = (HyperLink)Master.FindControl("contactmenu");
contact_menu.CssClass = "current";
}
Change as per your need..
Enjoy..

You first need find the control on the master page
Image img = Page.Master.FindControl( "layoutStyleSheet" ) as Image;
then add style to it
img.Attributes.Add("class", "className");

Related

ASP.Net C# reference to element on Site.master returns: Object reference not set to an instance of an object

I've found threads solving this issue within Default.aspx and Default.aspx.cs, but applying those solutions to my problem don't fix my issue. None of them are helpful in this context:
I'm trying to access an element on my Site.master page within Default.aspx.cs. I want to hide this element if a certain condition isn't met. However, I cannot reference any elements within my Site.master page (either within Default.aspx.cs OR Site.master.cs)
Code:
Site.master:
<li><a runat="server" id="editLink" href="~/Edit">Edit</a></li>
Default.aspx.cs:
MasterPage master = this.Master;
Control linkControl = master.FindControl("editLink");
linkControl.Visible = false;
I've also tried:
Site.master:
<asp:Panel ID="Panel1" runat="server">
<li><a runat="server" id="editLink" href="~/Edit">Edit</a></li>
</asp:Panel>
Default.aspx.cs:
((Panel)Master.FindControl("Panel1")).Visible = false;
//or..
//Panel testPanel = ((Panel)Master.FindControl("Panel1"));
//Panel1.Visible = false;
I don't know where to start. Thanks!
EDIT:
I've tried to apply resolutions from:
ASP.NET - Accessing Master Page elements form the Content Page
What is a NullReferenceException, and how do I fix it?
Specifically from the first:
adding the following to Default.aspx
<%# MasterType virtualpath="~/Site.master" %>
adding the following to Default.aspx.cs
Panel testPanel = (Panel)Master.FindControl("Panel1");
testPanel.Visible = true;
The above code is the suggested fix for the first thread, but I get the same error. The second thread seemed to suggest I was referencing the element before it was created thus returning the nullreferenceexception error, but I do not believe this is the case. Ultimately, I do not understand enough to utilize those threads.
EDIT
Site.master MCVE:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<form id="wrapForm" runat="server">
<asp:ScriptManager runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
<asp:Panel ID="Panel1" runat="server">Test Text</asp:Panel>
</asp:ContentPlaceHolder>
</form>
Default.aspx.cs MCVE:
protected void Page_Load(object sender, EventArgs e)
{
Panel testPanel = (Panel)Master.FindControl("Panel1");
testPanel.Visible = true;
}
MasterType <= that should be Reference as per ASP.NET - Accessing Master Page elements form the Content Page ?
<%# Reference virtualpath="~/Site.master" %>

verify content control's ContentPlaceHolderID attribute in the content page

I am a new ASP.NET developer. I am making development using C#. I removed some default contents from that I am getting these error notification as attached in screen shot can anybody look at and give me a solution also some one inform me from where .NET pages control header and footer so I can make modification there.
Here is the complete error heading text:
"Cannot find ContentPlaceHolder 'MainContent' in the master page '/Site.Master', verify content control's ContentPlaceHolderID attribute in the content page."
Remove asp:Content ContentPlaceHolderID="MainContent" from your child pages or Add <asp:ContentPlaceHolder ID="MainContent" runat="server"> into your Master page.
There is no ContentPlaceHolder named 'MainContent' in your master page,
A content place holder should be in your Master page like this:
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
And child pages like this:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
When you have a child page that inherits from a master page ContentPlaceHolder should match between them.
So if you masterpage declare that every child should fill its Box1 Box2 and Box3containers every
child page that is linked to it (MasterPageFile="~/MasterPages/Site.master") must contain this references <asp:Content runat="server" ContentPlaceHolderID="Box1"> even if empty.
In your case you have declared a box name that Master page doesn't has
update
looking at your code I can suspect that you have linked the master page in a wrong way.
Change the attribute of your page and be sure that the path is correct
MasterPageFile="~/MasterPages/Site.master"
if it doesn't work try this
If the page is empty delete it and recreate aspx page with master page, after you select the master page the child page will be created with the right contentplaceholders.

Problem with setting visibility of controls in child master page in asp.net

I have a parent master page (Master.Master) and child master page (Child.Master). The Child.Master inherits Master.Master master page file. Now in the Child.Master i want to set the visibility of Div (whose ID is Div1) to false, for which i'm using the following code:
protected void Page_Load(object sender, EventArgs e)
{
this.FindControl("Div1").Visible = false;
}
Here is the code in the Child Master Page file:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Webstore.Master.cs" Inherits="WebStore.WebStoreMaster" MasterPageFile="~/Login.Master" %>
<asp:Content ID="UserMaster" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div id="Div1" runat="server">
<div id="Sidebar" runat="server" style="float: left; margin-top: 100px; margin-right: 20px;">
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
The compiler is giving me the following error:
Object reference not set to an instance of an object.
Can someone explain why is it happening so ??
Thanks in anticipation
EDIT:
In this case, if the div is a top level element, and you are in the page_load of the child master page in which the div resides, you should just be able to do
Div1.Visible = false;
Why not use a Panel control?
you should say
this.Master.FindControl("Div1").Visible = false;
Try setting #MasterType in Content Pages and in Child Master Pages. Below are some reference links
http://msdn.microsoft.com/en-us/library/ms228274.aspx
http://dotnet.dzone.com/news/back-basics-%E2%80%93-using-mastertype?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+zones%2Fdotnet+%28.NET+Zone%29
http://dotnetslackers.com/community/blogs/haissam/archive/2008/02/11/mastertype-directive-in-content-page.aspx
Thanks
Master page is loaded is after page_load. Therefore when you attempt to address the master page during page_load it's properties and methods are not yet available. Move this down in the page life cycle. ASP.NET Page Life cycle, Another SO answer on Masterpage/page life cycle. the child master is loaded during the page_load, and parent master is loaded during the child master page_load.

From UserControl to parent master page

I have a weird scenario but this was how it was designed before me. Basically I have userControl, and there is a child.masterpage
in the userControl in the ascx file it contains the following
<div><%=_template%></div>
the child.masterpage inherits from a parent.masterpage, in the child.masterpage there is a call to the userControl
<asp:Content><ucc:UserControl></ucc>
the parent.masterpage has other fields in it and it has a .cs file with a c# function
public void passVal(string s)
Now what I want to do is to pass a value from the user control directly to the parent.masterpage function so that I can put it in the parent.masterpage literal I have created.
How can I achieve this (again, this is existing design and I cant turn things around) I am just adding a functionality.
<%# Master Language="C#" AutoEventWireup="true" MasterPageFile="../common/main.master" %>
<%# Register Src="UserControl.ascx" TagName="Ord" TagPrefix="uc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="in"><uc:OrderReceipt ID="myord" runat="server" Visible="true"/>
<div style="margin-bottom:30px;">
Back to Home Page
</div>
</asp:Content>
You can use the Page.Master property to get a hold of the master page instance.
protected someEvent(object sender, EventArgs e)
{
(Page.Master as ChildMaster).passVal("some string");
}
More of an Answer:
I was just reviewing your OP and realized something. The code that kept puzzling me was the user control.
in the userControl in the ascx file it
contains the following
<div><%=_template%></div>
I haven't seen the code behind but my guess is that the user control is simply used to output dynamic HTML. I bet if you looked at the code behind (.cs file) of the user control, you would find a variable called _template. It is a string variable that is pumped with html at run time.
Now, that doesn't answer your question but, if you didn't already know that ... it is good to know =P
Now, the next mystery is the one concerning your missing code behind file for the child master page.
My theory is that whoever made it did it with some error that would cause it not to automatically generate a code behind file. Or, they made it from scratch and just simply added it to the project but neglected to make a code behind as well.
I made a master page, then made another one called child. I am able to subclass it and here is what the markup and code behind look like.
<%# Master Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="Child.master.cs" Inherits="Child" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
</asp:Content>
public partial class Child : Master
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
In comparing the markup to yours, the key difference here is that mine explicitly mentions a CodeFile attribute.
Create a new cs file and following the naming convention. Then add the CodeFile and Inherits attributes to your child master page. This should wire everything up correctly and allow you to start adding methods and such to the child master page.
Let me know where you are at and we'll take it from there. GL

How to load menu only once time in Master Page?

I created Master page which has got mainNavigator panel on top of page that is a web user control(BuildMenu.ascx). I am filling UC Menu in master page's loading :
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="IntermMaster.master.cs" Inherits="MyProject.IntermMaster" EnableViewState="true" %>
<%# Register src="Utils/BuildMenu.ascx" tagname="BuildMenu" tagprefix="uc1" %>
>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<uc1:BuildMenu ID="BuildMenu2" runat="server" />
</div>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
This is loading in postback event:
BuildMenu.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Fill Menu from DataBase (Compare SiteMap...)
}
Every post back BuildManu.ascx is loading every time tihs is really bored me. How can i solve it. I want to do only one time load BuildMenu.ascx (in master page)
Unless you want to use frames (and you probably do not), the control has to be reloaded each time so it can be rendered. The best you can do is to use server-side output caching so that it takes less processing time to load the control.
To do output caching, put this in your page:
<%# OutputCache Duration="[Number of Seconds]" VaryByParam="None" %>
The load method WILL be called every time a postback occures (except for AJAX pages, but let's not go there). Take a look at the ASP.NET page lifecycle.
What you can do is just return from the controls Load event if the value of IsPostBack is true.
However, if the control in question is static (or almost static) in content you could try using output cashing on the server, that way the control will be loaded once in a while, and the rest of the times, the server will just use it's cashed copy.
i have a better idea why dont you sue a session it will help you
make like this ::
protected void Page_Load(object sender, EventArgs e)
{
if session(ispostback") <> "menuloaded"
{
// Fill Menu from DataBase (Compare SiteMap...)
Session("ispostback")="menuloaded"
}
this will work for sure

Categories

Resources