Ways to keep the same header on every page of the website - asp.net

I am trying to make a member based website in which I will need to keep the HEADER and certain other elements fixed on every page of the site.
I am familiar with the concept of MasterPages in Asp.net, and SHTML however I find it a bit confusing and tedious to use them for a website.
I want to know that are their some other ways to achieve the same feat in a way other than SHTML or MasterPages, one that is more refined and easy to implement?

Create User Control In your Application and Register Your User Control in your Page(like .aspx).
<%# Register TagPrefix="UC" Src="~/UserControl.ascx" TagName="mycontrol" %>
and Use Like control.
<UC:mycontrol ID="my" runat="server" />

You need to get acquainted with the concept of UserControls. Read here for a complete understanding: MSDN LINK
User controls are powerful functionality of ASP.NET. With the help of the user control you can reuse the Design as well as code in the application.
This another link provides very basic introduction of UserControls and finally ends up creating a one:http://asp.net-tutorials.com/user-controls/introduction/

I am currently using a perl script that concatenates header/content/footer files together. However this is a bit OTT so I am in the process of converting to iframes. All you need do is add a line to the top of each page...
<iframe border=0 src="http://mysite.com/header.html"></iframe>
You can adjust the size of the frame if needed.

If you don't like master pages, you could always try Dreamweaver templates, which auto generates the template contents on each page using the templates.

Related

Is it a good idea to use plain HTML instead of ASPX

I'm developing an ASP.NET website. Sometimes some modules contain so few content that I present them inside a jQUeryUI dialog and communicate with the server via AJAX. I keep those contents inside a separate file and load them to a jQueryUI dialog according to the appropriate module.I was wondering if it's a good idea to have that content as a plain HTML elements instead of asp ones. Somehow I thought maybe this would reduce the overhead the conversion from asp elements to html elements can cause.
I'd allways go with the aspx Page, because a dynamic Page is more work at the beginning but in the end it almost ever saves time.
Specially when your not sure of the content that will be shown there, it is better.
And for the one reason i do it, is to have everything the same.
One style one way to code.
I'd say this is probably premature optimization. The overhead of an aspx page is in almost all cases negligible. I believe it's more likely that you will some day need to put dynamic things in that page, in which case you would have to convert the html file to an aspx, and change the url for your ajax dialog - which will cost time/money.
If you have aspx pages, or ascs user controls that you do not actually use/run any code, you can set the AutoEventWireup the EnableViewState, and maybe the EnableSessionState to false and stop the calling of the PageLoad and the rest functions and reduce the overhead. So on top of the controls you declare:
<%# Control AutoEventWireup="false" EnableViewState="false" ...
or for page:
<%# Page AutoEventWireup="false" EnableViewState="false" EnableSessionState="false" ...
The disable of the session is let the pages loads in parallel, the disable of the EnableViewState is reduce the size, the AutoEventWireup is reduce the callback hooks and calls.
In general you can use what ever you wish - if your pages can work, but if you like to keep it robust and easy to change or update, or add new functionality in the future, then use dynamic aspx pages.
Similar question: Master page and performance

master page menus

I want to create a master page for my already developed project.Since the project contains many forms it is quite difficult to include the master page in each form...Is there any possibilities to include the master page in any other simplest way...
Please give some suggestions..
Thanks in advance...
As far as I know, there is no easy way to do this.
You'll have to manually add the masterpage to the page directive
<%# Page MasterPageFile="~/Masterpage.master" ... %>
add the relevant content sections around your pages markup:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
and remove the <html>, <head>, <body> and <form> tags from every page.
Update
Here is an article (and source code) by Bob Powell describing a way to automatically convert html files to aspx files and add a master page. I'm sure you could adapt it to your needs.
This transition is not an easy one to make, as #geoff indicates. It is possible though if you have enough time and patience. The first step is to take all common elements (layout, menu, header, footer, whatever is common) and develop a master page structure. You'll likely need more than 1 depending on the differing layouts of forms in your application. Develop a user control for each of these common sections and make sure the master pages use these controls. Then systematically go through each page of your site and begin implementing the master pages.
As an assistance mechanism, you'll also probably want to have a page baseclass that is capable of communicating through the master page to the contained user controls. In our group we have a standard for setting a property on UserControls and MasterPages called ParentForm that is of type of our primary base page class, and this property is set during the Init of any page or control so that at any time, the developer has access (through Intellisense) to page itself. This is especially helpful since the parent of most controls is a container whose parent is a container whose parent ... you get the idea. For our controls it's just this.ParentForm.
It will be a long process, but MasterPages were really intended to be a "ground up" architectural decision rather than an "employ later" concept.

Alternatives to ASCX User Control without a server-side form?

I've got an ASP.NET 3.5 Web Forms application in which a large chunk of code needs to be duplicated between a few different pages. Sounds like the ideal candidate for a user-control right? Problem is, this cannot be contained within a <form runat="server"> because it contains a client-side form of it's own.
There are no runat=server controls or postbacks or anything that really need that webform - think of it just as a chunk of HTML with a few basic <% %> tags. I'd just want to set a property on the control when it's loaded, so that it knows what to output. This is purely an exercise to make the code easier to maintain.
Before I resort to using an oldskool <!--#include-->, is there some better way of doing this?
You can still use a normal user control. Just don't rely on viewstate and postbacks and you shouldn't have any problems.
<%=Response.Write(File.ReadAllText(Server.MapPath("~/includes/filename.ext")))%>
Something along those lines, anyway.
Edit: Same functionality as a server side include, but if I'm not mistaken, enabling the SSI syntax requires an IIS change, where as this wouldn't.
Edit 2: I didn't see the note that your include contains asp.net code. This would obviously only work for client side code only. My mistake.
You can have as many form controls as you want but only one can have runat="server".
Some other techniques:
http://webproject.scottgu.com/CSharp/UserControls/UserControls.aspx
http://weblogs.asp.net/scottgu/archive/2005/08/28/423888.aspx
I'd still make it a control. The <% %> stuff could be labels/literals for more flexibility, and as soon as you get done saying there are no postbacks needed, you'll need them. Best to set up the other pages to include it as a control now for easier changes later. Heck - you could even take advantage control-level caching!

Alternatives to MultiView in ASP.NET

The website I'm building contains a large number of views which will be displayed on the same place but hidden or shown according to how the user navigates the menu.
It gets quite messy in visual studios design view when you have a MultiView with 10 different views in it. I've already separated the content of each view in several user controls. But is there an alternative to MultiView?
I generally just use a Panel or Placeholder and toggle visibilities manually. But then I don't use the VS designer either...
Assuming you're using ASP.NET webforms here...
This is another reason to invest in some powerful web controls like the Telerik ASP.NET control suite.
You could use their RadTabStrip control along with the RadMultiPage control.
Example here: http://demos.telerik.com/aspnet-ajax/tabstrip/examples/multipage/loading-external-content/defaultcs.aspx
You can specify a ContentUrl for each RadPageView which allows you to separate each view into separate aspx files.
This simplifies your solution by separating each view into their own page and increases performance as RadPageView content is requested only when viewed initially.
The way that I would do this would be to have a placeholder on the page and dynamically add controls to it as needed.
From what you have said it looks like the user navigates a menu and this decides which view to display in the MultiView control...
I would so something like this
ASPX file:
<asp:PlaceHolder id=phContentContainer" runat="server"></asp:PlaceHolder>
Code Behind
switch (MenuSelection)
{
case "LOGIN" //Display the login control
{
ucLoginUserControlType loginControl = (ucLoginUserControlType)LoadControl("~/UserControls/ucLoginUserControlType.ascx");
phContentContainer.Controls.Add(loginControl);
}
}
Obviously you will want to work the codebehind to be a bit more efficient and cleaner than the above would be with 10 views... but you get the idea.
This keeps your aspx page simple and makes future control additions easier to implement.
Hope this helps, I have typed the code from memory, so some syntax may not be right - if you cant get it going give me a shout and Ill dig out some examples of when I have done this.
Good luck!

Register Startup Script Control

I am looking to make a web control where I can register client startup scripts inline with my aspx because I hate registering in the codebehind!
An example of what I have so far:
<Ben:StartupScript runat="server">
var form = document.getElementById("<% =form1.ClientID %>");
</Ben:StartupScript>
Currently, I am overriding the OnPreRender method of the control, rendering the contents of the control to a string and then manually registering that string as a startup script using ClientScript.RegisterStartupScript on the Page. I also override the Render method in order not to render the control. I should also note that I have the ParseChildren attribute set to false and the PersistChildren attribute set to true.
All seems to be working well, but my control allows other web controls inside it (such as Button). Apart from being unintuitive, this can result in runtime errors. I would love to be able to clear the controls but this is impossible given the <% ... %> code block.
So, is it possible to prevent developers from embedding child controls whilst still allowing code blocks?
Also, is this idea any good or is it fundamentally flawed? Lol.
Thanks a bunch,
Ben
it sounds like a good idea, but if you spend too much time fighting the inherited/default behaviors then it may be more trouble than it's worth
if this is a one-shot issue, a cheap-hack solution is to just directly embed your scripts in the header of a master page ;-)
on the other hand, allowing developers to embed their own scripts as children of your web control might be useful
If you are using master pages, add another control in your section. That way you can easily add css/js to your headers in your child pages.
ClientScript.RegisterScript is mostly for user/server controls,

Resources