verify content control's ContentPlaceHolderID attribute in the content page - c#

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.

Related

Asp.Net Page.Master.FindControl always returns null inside static method [duplicate]

I have to find a Control in an aspx page bound to a master page.
The master page contains:
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
The content page contains:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
</asp:Content>
I added a Table with ID formtable as a child of Content2.
I tried to use the following code to access the Table, but the code returns null:
protected void Ok_Click(object sender, EventArgs e)
{
Table tblForm = this.FindControl("MainContent").FindControl("formtable") as Table;
}
How can I access the Table?
Try this
Table tblForm = this.Master.FindControl("MainContent").FindControl("formtable") as Table;
Checkout this Control ID Naming in Content Pages for more details
Working with findControl() cause complications sometimes.
It is easier to define a public property for that control in master page and then access control through the property.
you should add this line in child page:
<%# MasterType VirtualPath="~/MasterPage.master" %>
What context are you in when you are trying to do this? Are you in the codebehind of the individual page?
If you are it should be Content1.FindControl("formtable") as Table and that would be it.

What's the difference between <head> and <asp:Content ID="HeaderContent" ...>?

This may be a newbie question, but i'm pretty new to asp.net & C# etc.
I'm working with an ASP.net website, and I'm curious about the structure of it (after automatically creating a web project), specifically the following:
I see that in Default.aspx , I have a tag like this:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>**strong text**
But in Site.master, I have this:
<head runat="server">
*etc*
</head>
So where would I put code if I wanted to include JavaScript code to run, on page load?
I believe you can put your code in any of them. The first one is for adding code or script used by all content pages(that using this master page file) while the second one is if you want to to add script or code from content pages(that should be used only for this specific page)
//in the Master page, the content here is used by all content pages
<head runat="server">
*etc*
</head>
and
//this is specific to the content page that use it. This section needs to be supplied in content pages
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
That section needs to be supplied in each content page and it will be exclusive to that page - no other page can use the script in that section
asp:Content ID="HeaderContent" is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated.
head is a standard html markup, indicating the page head elements. Typically, the HeadContent placeholder is inside the head tag on the master page.
The head element, container for all the head elements, must use a title for the document. Some other elements it can include: style, base, link, meta, script, noscript.
The asp: Content ID = "HeaderContent" is a content element of the master page.
Have a look at the Plugging in Content part of the following link for detailed information on this: http://odetocode.com/articles/419.aspx
I think you asked when you want to use JavaScript where you put JS in your code.You can put anywhere you wish in asp side between script block such as:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</asp:Content>
or
<head runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</head>
Also you can put JS outside this tag. You only should use tag.

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

Set background picture on webpage when using a masterpage

Is there away to set a background image on a webpage when the webpage is based on a masterpage. Notice its not the background of the masterpage i want to change, but the background of the page which use the masterpage.
You can just add a style rule for body{} or whatever on the actual ASPX page. It will override any style set at the master page level.
edit: example:
<%# Page Title="Example" Language="C#" MasterPageFile="~/MasterPages/Main.master"
AutoEventWireup="true" CodeBehind="TroubleShootScanning.aspx.cs"
Inherits="SpectrumTechnologies.TroubleShootingScanning" %>
<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" runat="server">
<style type='text/css'>
body { background-image: url(images/bgimage.jpg); }
</style>
<!-- the rest of the page here -->
</asp:Content>
This will override any values set in a stylesheet.
You could put a content placeholder on the master page that is within the header.
Then in your content page, put a content control where you could include the second modified CSS stylesheet or STYLE block directly.
I suggest adding an <asp:ContentPlaceHolder ID="ExtraStyles" runat="server" /> tag to the header of the Masterpage. That way you can add the following to your page:
<asp:Content ID="ExtraStylesContent" ContentPLaceHolderId="ExtraStyles" runat="server">
<style type="text/css">
body {background-image:url('someotherimage.jpg');
</style>
</asp:Content>
By adding an extra ContentPlaceHolder you won't get the scattered style tags, they will end up in the head tag of the rendered html.
There's an additional trick to add to this...(Second line below)
Also helps the background show while testing locally.

Loading a control From a master Page

The "Header control" we use holds a jquery reference. When I attempt to leverage any jquery functionality from the Master page it fails because the jquery reference has not been added yet.
Is there a way to force the Header Control that is embedded into the Master Page to load it's resources before the Master Page attempts to reference them?
I'd include the jquery reference in the head of the master page itself.
Or if you don't want the jquery on every page then you can do this in your master page:
<head runat="server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
And then do this on the aspx page that needs jquery:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</asp:Content>
Either way you do it accomplishes the same thing. It includes the jquery file in the "head" of your HTML. And that's the easiest way to ensure jquery works properly.
You can use the ScriptManager in the template master to register your javascript inclue file for JQuery.
You can use
ScriptManager.RegisterStartupScript or
ScriptManager.RegisterClientScriptInclude
Another way is in the template master, in the oninit method use this code
Page.Header.Controls.Add(new LiteralControl(" <script src="jquery" type="text/javascript"></script> "));
You could simply move the JQuery script reference into the aspx of the master page itself (at the top, of course). Then, any controls would be able to access JQuery.

Categories

Resources