Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have 1000 of page in asp.net application with many master page. I want to show Happy new year on every page. So how it can be achieved without apply code on every page or master page?


What I have tried:

I don't know how to achieve it without apply anything on master pages.
Posted
Updated 15-Sep-16 1:32am

In life when I have a problem, my intentions are always to keep it simple. In this case, you'll need to do a little work, but it will be worth it. My recommendation is to create a Web User Control.

A Web User Control is a Web Form, but it can be placed on multiple Web Forms and can even be put on Master Pages. If it were me, I would create 1 Web User Control and put it on each Master Page. If you have 10 Master Pages for the 1000 Web Forms, you'll still be saving a lot of time and it will be easier managed in the future.

The first release you do, you would need to compile and publish. Let's say a month from now in the future, if there is a special holiday in your culture and you don't want to do a full release, then you can only update the text to your 1 Web User Control and it will be updated through out the site. Below is sample of a Web User Control used in a Master Page.

ASP.NET
//Here is the Web User Control named Head.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Head.ascx.cs" Inherits="MyWebsite.UI.Web.WUC.Head" %>
<% @ OutputCache Duration="3600" VaryByParam="None" VaryByCustom="browser" %>
<!-- **** above I am setting the Web User Control to cache for 1 hour **** -->
	<div id="title">This is Text I want to display in EVERY PAGE!
	</div>





//Here is my Master Page named Main.Master
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Main.master.cs" Inherits="MyWebsite.UI.Web.Templates.Main" %>
<%@ Register Src="../WUC/Head.ascx" TagName="Header" TagPrefix="Head" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div id="struct">
<Head:Header ID="head2" runat="server" />
<!-- **** the tag above is the Web User Control being called in the Master Page **** -->
    <div id="content">
        <div id="main">
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        
    </asp:ContentPlaceHolder>
        </div>
    </div>
</div>
</form>
</body>
</html>
 
Share this answer
 
May be you can include common page fragment to all other pages to display your message.
 
Share this answer
 
So you want to show something on every page without changing any of the code? Your options are fairly limited then, your best bet is probably a response filter. That will let you inject html into the page after .net has rendered it but before it is sent to the client. This article shows you how you remove spaces from the html, you'd need something that injected some html after the body tag or something

Removing White Chars from ASP.NET Output using Response.Filter property[^]

If you google HttpResponse.Filter you can probably find examples closer to what you are looking to do.
 
Share this answer
 
Quote:
I have 1000 of page in asp.net application with many master page. I want to show Happy new year on every page.
You can easily add the content that you want to on the master pages of those templates web pages. I assume you are using ASP.NET Web Forms in your project, you can however use Response.Write() (which is the best way to ruin your web application!) or you can try to dynamically build up a web page and then move onward to master pages for addition of the pages etc. One of the methods is to use JavaScript and add the content once the page has been loaded,
JavaScript
var paragraph = // content to be added.
document.body.insertBefore(paragraph, document.body.firstChild); // Add to top.

If this works, you can use this method to include the content using JavaScript and not modifying any master page. This would work across the web application, include the script.
Quote:
So how it can be achieved without apply code on every page or master page?
This is the part which is not possible. Every change requires a change in the code that gets executed.

For more read here,
Dynamically inject HTML code from a String into an asp.net page - Stack Overflow[^]
Code injection - Wikipedia, the free encyclopedia[^]
javascript - How to insertBefore() element in body tag? - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900