I searched a way to include a file in a web application (like a menu, so I won't have to edit it on all pages when applying changes), but haven't found something as simple as
<?php include "Menu.html"; ?>
Can you please help?
Have you looked into Master Pages? They would certainly help you add the same layout across several pages.
Or perhaps you want a reusable User Control (that you write yourself)?
We don't use "include page" in asp.net, even though it is possible (with a different syntax of course). Instead, have a look at Master page concept.
MasterPages allow you to maintain a parent/child relationship between a master page which contains content that wraps around any number of child content pages.
Similarly, UserControls allow you to re-use whatever content you want on whatever page you want, whether it's a MasterPage or ContentPage:
<%# Page Language="C#" %>
<%# Register TagPrefix="uc" TagName="Spinner"
Src="~/Controls/Spinner.ascx" %>
<html>
<body>
<form runat="server">
<uc:Spinner id="Spinner1"
runat="server"
MinValue="1"
MaxValue="10" />
</form>
</body>
Methods (C#)
Executable code:
Page include
<!--#include file="a.aspx"-->
Execute independently inside a page
<% Server.Execute("a.aspx"); %>
Non-executable code:
<% Response.WriteFile("a.inc"); %>
I believe this is what you are looking for.
<!--#include file="wisdom.aspx"-->
I use C#.net
<% Response.WriteFile("YourPage.aspx"); %>
and this works real well for me!!
I also use your line,
<!--#include file="wisdom.aspx"-->
when I am in HTML mode.
Related
I have a question about the ****.Master Site a in ASP.NET with C# (WebForms).
I've created a Site.Masters File with a lot of differente Placeholders inside. So the question is - is it possible to fill the Placeholders from different .aspx Files?
.Master Page:
<asp:ContentPlaceHolder ID="MainHeader" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="MainFooter" runat="server">
</asp:ContentPlaceHolder>
</div>
Now i would do something like this:
Adding some Content to the Main Header - the Content is represented in a file called mainHeader.aspx
Adding some Content to the Main Content - the Content is represented in a file calle mainContent.apsx
Is this possible or is there a solution for my plan? I would like to create a modular and flat structure of my .NET Project.
Thanks for your help - best regards
No, that's not how Master Pages work.
When the user/client requests an individual content Page, that Page is merged with the Master Page. The Master Page can not be called by the user/client.
I believe User Controls are what you are looking for.
You can do what you want by using jquery.load(), here is the documentation http://api.jquery.com/load/, however i think what you are doing is not the way to procede, in order to do what you want i suggest yo to do this:
Create User Controls, one for each master ContentPlaceHolder, then in your master, add each one of this and initiate them.
I have two master pages in my C# MVC application. What I would like to be able to do is use one, or the other depending on the users 'role'. Something similar to this (obviously with a little more validation etc):
<% if(User.IsInRole("One")) { %>
<%# Page Language="C#" MasterPageFile="~/Views/Shared/One.Master"
Inherits="System.Web.Mvc.ViewPage<MyApp.Data.ProductData>" %>
<% } else if { %>
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Other.Master"
Inherits="System.Web.Mvc.ViewPage<MyApp.Data.ProductData>" %>
<% } %>
I've seen answers where this can be done to elements of a page, for example a menu, an image, etc. Is it possible to do it for the entire master page? In my situation, depending on the role, different css, images, colours will be used so it is necessary to use a different master page.
If anyone could help I'd be very grateful, or if anyone has any alternative (and probably better) solutions I'd also be grateful.
Thanks.
As you are using ASPX View in ASP.net MVC Application.
ASP.net MVC ASPX ( Webform) view still derive from Page class so you can use following code in
your aspx view.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script language="C#" runat="server">
protected void Page_PreInit(object sender, EventArgs e)
{
if (User.IsInRole("Admin"))
{
this.MasterPageFile = "~/Views/Shared/Site2.Master";
}
else
{
this.MasterPageFile = "~/Views/Shared/Site.Master";
}
}
</script>
You can change it dynamically via ViewMasterPage.MasterPageFile.
I would suggest making the selection in your Masterpage file rather than selecting which masterpage file to use.
I have a webpage with server accessible controls, see 'FileIconLink' below:
<body>
<p class="FirstTitle style5">Downloads:</p>
<div id="BreadcrumbDiv">
<p style="padding-left:5px; ">Page Loading...</p>
</div><!--/BreadcrumbDiv-->
<div id="DirLinksDiv">
<p><span class="SecondTitle">Files:</span></p>
<a runat="server" href="#" id="FileIconLink">File</a>
<% WriteFileLinks(); %>
<p><span class="SecondTitle">Folders:</span></p>
<a runat="server" href="#" id="FolderIconLink">Folder</a>
</div><!--/DirLinksDiv-->
</body>
<%RemoveHTMLTemplates(); %>
Both 'FileIconLink' and 'FolderIconLink' are templates of web controls which are copied by my code - such as <% WriteFileLinks(); %> above. How could these templates be permanently removed from the web page at run-time on the server without causing the error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Thanks in advance!
This is because you have <% %> inside the control you're trying to change. Instead of using <% %> in the aspx page, I would modify the code behind to add a literal control or something to the div, like:
DirLinks.Controls.Add(new LiteralControl(WriteFile()));
You should then be able to modify your control form the code behind.
Your inline code is executed during render.
But you probably want to get rid of the templates during Load.
Which means that the two techniques conflict.
Ultimately I realised my approach was wrong, as Cade Roux was alluding to, I needed to make up my mind where the templates were going to be used.
My solution was as follows:
Make controls for containing the results of my (previously inline) code.
Use templates in Page_Load to fill the controls described above.
Delete templates in Page_Load.
Do nothing inline.
The Page object has another function apart from Page_Load function called Page_PreRender, this function gets executed before Page_Load. So please try remove logic in this Page_PreRender function.
Please refer this link http://msdn.microsoft.com/en-us/library/system.web.ui.control.prerender.aspx
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
I recently put some code <% %> code blocks in my Master Page. Note I've read of the "fix" for either moving things out of <head> or using <%# %> but neither of them work well for my application.
Now the weird thing is that I only get this error on one page of mine. All the other pages seem to work fine, so what actually causes this error? There is nothing I can think of that is unique about this page. It uses the script manager as does other working pages and there is just nothing extraordinary about this page. It does have quite a few custom controls on it, so hunting down what is different in this page is more difficult than usual.
So what actually causes the Controls collection cannot be modified because the control contains code blocks exception?
Things can go wrong when some code tries to add controls to the tag containing the <% ... %> or <%= ... %> code block (in this case your <head> tag).
For instance, when you're using themes, the Page class will automatically add <link> tags to the <head> for every CSS file in your theme's directory. But it could also be triggered by setting the Page.Title.
But there are many more ways that can cause modifications to the <head> tag, so without further information (such as a stacktrace) it's hard to give a definitive answer.
I've come up with something I find a lot easier and more straightforward -- while leaving the tag in the header where it belongs.
First, start the code block with <%# instead of <%= :
<head id="head1" runat="server">
<title>My Page</title>
<link href="css/common.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%# ResolveUrl("~/javascript/leesUtils.js") %>"></script>
</head>
This changes the code block from a Response.Write code block to a databinding expression. Since <%# ... %> databinding expressions aren't code blocks, the CLR won't complain. Then in the code for the master page, you'd add the following:
protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}
The DataBind method evaluates all the databinding expression(s) in your header at load time.
If you have a page or control with <% %> and ever dynamically update the control collection (add a control to the page that isn't defined in the .aspx/.ascx) this error will trigger. To get around this I have used an <ASP:Literal/> to inject data instead of <% %>
If you have themes enabled it could cause it to do that.