Loading a control From a master Page - c#

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.

Related

Why doesn't Visual Studio Default.aspx file let me add HTML elements?

<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XEx02Quotation._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<html>
<head runat="server">
<title></title>
</head>
<body>
</body>
</html>
</asp:Content>
I am confuse about why Visual Studio 2017 doesn't let me add HTML elements into the Default.aspx, but what I do know is that under the Solution Explorer, I see master page and apparently they are bind to the content page (which is the Default.aspx). Also, I put a copy of my screenshot problems.
So my question is do I start putting content (HTML elements) inside of that master page first!
Aside from what I have mentioned above, I also notice that when I try to put basic HTML elements into the #page directive (Default.aspx file), it says "“Content is not supported outside of 'script' or 'asp:Content' Regions” ".
The only reason I know of these terms is because I research this problem. Here are the article that I have researched. "Content Not Supposed to Be Outside 'Script' or 'asp:Content' Regions" AND https://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx
Here are the screenshot problems:
You can put HTML elements like this:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <div>Test</div> </asp:Content>.
If you want to edit the and others and things like that, you can make that inside the Site.Master.
But if you want to add other HTML pages you need to make new HTML page and add that inside the page you want(Maybe Default.aspx) , like this Stack answer.

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.

Integrating CKEditor in an ASP.Net Webforms Application

I'm trying to use CKEditor in my Webforms application and I have tried many other methods without success. Can someone help? The site uses a Site.Master file which all the aspx files provide content for using content place-holders. So I place the JavaScript call in the ASPX rather than in the master file.
I'm also trying to get it to edit in a content editable div element. It works fine in a small test site but not in my application. Any ideas?
My Site.master has two content placeholders I want to use:
<asp:ContentPlaceHolder ID="Scripts" runat="server" />
and
<asp:ContentPlaceHolder ID="MainContent" runat="server">
An then my aspx files have:
<asp:Content ID="Content3" ContentPlaceHolderID="Scripts" runat="server">
<script src="ckeditor/ckeditor.js"></script>
</asp:Content>`
and
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="renderTarget" renderwidth="1100">
</asp:Content>
</asp:Content>
Somewhere in my JavaScript, I set the content editable of my renderTarget div to true.
$(".contentbox[dynamic='true']").attr("contenteditable", "true");
And at this point, I expect CKedtor to add its controls to the div. But It doesn't. i use the same process with TinyMCE and it works fine.
If you have CKEditor in your project correctly, it is as simple as:
<textarea cols="80" class="ckeditor" id="editor1" name="editor1" rows="10"></textarea>
or even
<div contenteditable="true">...</div>
Also, you may want to read up on jQuery Selectors.
An example more like what you posted:
<div id="ckMe">...</div>
<script>
$(document).ready(function () {
$("#ckMe").attr("contenteditable", "true");
});
</script>
all you need is to install the version for asp.net in your project and then just use it as simple as adding a usercontrol:
<ckeditor:ckeditorcontrol ID="CkEditor1" runat="server"></ckeditor:ckeditorcontrol>
Documentation from ckEditor

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.

Using nested Master Pages

I'm very new to ASP.NET, help me please understand MasterPages conception more.
I have Site.master with common header data (css, meta, etc), center form (blank) and footer (copyright info, contact us link, etc).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="_SiteMaster" %>
<!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 id="tagHead" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form id="frmMaster" runat="server">
<div>
<asp:ContentPlaceHolder ID="holderForm" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="holderFooter" runat="server">Some footer here</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and I want to use second master page for a project into sub directory, which would contains SQL query on Page_Load for logging (it isn't necessary for whole site).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<asp:ContentPlaceHolder ID="holderForm" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooter" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
But I have a problem: footer isn't displayed.
Where is my mistake? Am I right to use second master page as super class for logging?
Project page looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="Server">
Some footer content
</asp:Content>
I've been working with nested master pages and have run in to something similar. From what I see where you have "Some footer here" in the Site.Master is where the problem lies and I've had similar problems with having content with-in a contentplaceholder tag. if you try this instead
<asp:ContentPlaceHolder ID="holderFooter" runat="server"/>Some footer here
Then you should be able to see the footer content.
I'm not sure I'd use master pages for this. If it's really just going to do logging, I'd implement IHttpModule, register it in web.config, and then check whether or not to log based on the path of the request. I think of master pages as being about content rather than other processing such as logging.
See the IHttpModule walkthrough on MSDN for an example - in your BeginRequest handler, you'd probably check the request path and log appropriately if it matched.
Apologies if I misunderstood what you're trying to do though.
You should leave your ContentPlaceHolder empty, for it gets substituted by the content of the Content in your actual Page...
When you move the "Some footer here" text to your Content, you will see your lines of text :)
HTH
This link gives a simple explanation on Master pages,
http://waxtadpole.wordpress.com/2009/01/16/master-page-content-not-visible-visual-studio-2008/
The question are you right to use child Master pages in this instance - I would say master pages should be helping you solve issues around building a consistent layout, not for whether or not logging should occur.
The problem is, when the text elements placed inside Default.aspx are put in their relative Content Placeholders, they are written on the placeholders of your Site.master page and not those of Project.master (which have the same names).
You should resolve the naming conflict, by assigning different ContentPlaceHolderIDs to the the placeholders in Project.master (this means you'll also have to change the references in Default.aspx).
This would be your Project.master file:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<!-- whatever... -->
<asp:ContentPlaceHolder ID="holderFormInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
<!-- ... -->
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooterInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
And thus, your .aspx pages that use the Project master page instead of the global Page.master must be changed to:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderFormInternal" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooterInternal" runat="Server">
</asp:Content>
If the only reason is to implement loggin why would you mess around with masterpages?
If the logging isent supposed to display any text!?
You either do as Skeet proposed with an IHTTP handler.. Or lazier one would be do have a class that derives from webpage and implement logging in that class and make your pages that need logging dervice from that..
ex:
public class LoggingPage : : System.Web.UI.Page
{
public override void OnLoad()
{
// Do logging
}
}
partial class OneOfTheWebPages : LoggingPage
{
public void onLoad()
{
base.onLoad();
}
}
I may be misunderstanding your problem - but from the code you've posted, there isn't anything in the footer.
In your Project page, the <asp:Content> tag for the holderFooter content place holder doesn't have anything in it.
I have next inheritance tree:
Site.master <-- Page1.aspx
<-- Project.master <-- Page2.aspx
And I don't know why Page2 display only content of itself and it's master page - Project. But doesn't display a content of Site (as Page1 does) Why? What have I to write for doing that?

Categories

Resources