Masterpage Vs Themes decision dilemma - c#

I am decision dilemma as to the requirement of a client
I want to change the look and Layout both of the site - XXX Client
Generally this is how it should work,
You change a theme the website is displayed with different images and Colors.
You change a masterpage your website changes it's layout (sidebar moved to right from left - provided the layout is specified in masterpage)
I am confused as to how one would accomplish this. If i design my pages using a default masterpage then when i switch to another masterpage there is no assurance that other masterpage might have same content placeholders.
This would throw an exception
How can i implement multiple masterpages Intelligently without creating 2 pages each for the respective masterpage ?
Edit
dilemma is caused by other colleague implementing his own ideas into pages developed by him, He over popups to display forms on sidebar link clicks mine directs to separate pages.

You should be able to achieve this in CSS without changes to masterpages or themes. That way you will guarantee that the content placeholders will be unchanged.
Change your images to be set from CSS (something like):
.imageFromCss { background:url("../images/myimage.png");}
And CSS can move sidebars from left to right:
.sidebar {display:inline; float:right;}

You can change dynamicly the masterpage on PreIint.
Select Case iForum
Case 6 '41
MasterPageFile = "/Children/Forum.master"
Case Else
MasterPageFile = "/Ezra/Forum.master"
End Select
(I know that I can write better example)
Now, Create a BaseMaster class, with your variables, functions, etc, and inherit the website MasterPage from this class.
Now add to the top of page:
<%# MasterType TypeName="ForumMaster"%>
And now just use Master.YourFunction(). It will work with all MasterPages.
Good Luck !

Related

Making a asp.net web site template without using master page?

I am very new to asp.net and my job.
I was assigned a project to make a simple online order web application using asp.net c#.
The specification has been strictly defined (copied below)
Regarding the common content of the site, I need to make head, top and left(a search function)
"The design in /design/: Head.aspx Top.aspx Left.aspx"
-- Does that mean I am not allowed to use (nested) master page?
--- If so, how can I make a template without using master page?
Another option is to use the Server.Execute() method to render the contents of three separate pages into your base page if you have to use pages instead of usercontrols and still must have three separate pages rendered into one.
In the old days we did this with iis html includes.
If the head/top/left content is just basic HTML, then you could just put the HTML in a separate file and use an include link/reference in the original .aspx page.
If the content is all like a search function, then I agree with Henk's comment, create an .ascx User Control (which is really no different than an .aspx page with an .ascx extension) and then just reference that control on your .aspx page like this:
//put this at the top of your .aspx page
<%# Register src="usercontrols/YourControl.ascx" tagname="nameOfControl" tagprefix="ucControlName" %>
//then reference your control where you want it in your .aspx page
<ucControlName:nameOfControl ID="nameOfControl" runat="server" Visible="True" />
Here's an MSDN article on creating user controls: MSDN User Control
Maybe shoot the person who wrote the spec an email and ask why they have an issue with using a master page - this is kind of exactly why you would use one?!
JM

Re-using another page section

I have created a web page that I use as a small dashboard to hold issue or no issue. It works great. The page uses an .aspx and .aspx.cs. I would like to be able to reuse the information on this page on other pages. My site already uses master pages and I have not been able to find an easy way to include this information.
How can I use an include from a page that has coding in the code behind easily?
Typically you use Web User Controls for this.
Web User Controls allow you to package up other controls into one that you can drop onto multiple pages. They are great for common UI items such as address entries, dashboards, etc. Basically anything that needs to be the same across multiple pages.
At the risk of seeming very obvious - do you mean usercontrols. These will allow you to reuse chunks of functionality across your site.
I guess this question falls into two categories: User Controls, and Code Reuse. Not sure which one you are after.
User Controls
If you are talking about the controls on your page you will want to create a common user control.
Code Reuse
You need to create a common class (whether it is static or not depends on how you intend to use it) and define functions within that class.
For instance, lets say you have a page that you want to print "Hello World!" on any aspx/.cs page.
You could do this
public static class MyClass
{
public string PrintHelloWorld()
{
return "Hello World!";
}
}
Then you call it from any of your pages like so:
MyClass.PrintHelloWorld();
Right click on the project > Add New Item...
Select User Control (.ascx)
Put your markup & code behind there.
Then you add that control in any other page (includding other controls [although I wouldn't recommend that])
It sounds like you may want to create an ascx User Control.
http://msdn.microsoft.com/en-us/library/ie/2x6sx01c.aspx

Navgation between XAML pages

I have a simple Silverlight application that consists of four pages (XAMLs).
Navigation is done by calling:
//from XamlPageA
this.Content = new XamlPageB();
Is this the right way. I need to have the entries in Browser history so that users can go page to the previous page(s). How can I do it.
You are bypassing the navigation system completely by setting content manually. You would have to implement updating the browser history yourself if you do it that way (certainly possible, but quite tedious).
A simpler approach is to generate a "Silverlight Business Application" project and see how the page navigation is simply handled with hyperlink buttons. All the browser history plumbing is done for you as is the mapping from URL to views.
e.g. A button with NavigateUri="/Home" will cause a view named Home.xaml to load into the navigation:Frame of the MainPage window.
if you look into the navigation:Frame element of MainPage.xaml, you will see a number of UriMapping entries like this:
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
They provide the pattern matching to convert from URLs to views.
Hope this helps your project.

Remove statically added controls at runtime

The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?

ASP.Net MVC: How can I easily change the tab color of my navigation menu based on the tab that I'm on?

I want to implement my navigation tabs somewhat like the ones on this site, and I've heard that this was built using ASP.Net MVC. If I'm on stackoverflow.com/users, than the "Users" menu tab is orange and all others stay grey, same if a different tab is selected.
I am pretty good with manipulating the css to change color when it's hovered or selected, etc, and adding/removing/authorizing items in the menu container, but not familiar with how to change the color of the tab based on the tab page that I'm on. Any quick and dirty way to accomplish this?
Assign a unique id to the body element of each page (e.g. <body id="users">). In ASP.NET MVC you could have the body tag in your master page written like:
<body id="<%= ViewData["bodyId"] %>">
And in the Controller methods for each page put something like ViewData["bodyId"] = "users"; to dynamically assign the id for each page.
Then in your nav markup, assign a class with the same name on the <a> tag that links to that page:
<ul>
<li>Users</li>
<li><!-- more links --></li>
</ul>
Then in the css do something like this:
body#users a.users, body#another-page a.another-page {
/* include rules for how you want the current page tab to appear */
}
That will assign your "current page" styles to any link tag with a class that matches the body tag id.
Further to what Bryan mentioned, I usually add a "CssClass" property to my view model in cases like this. It's also useful for the situation where the calculation of the CssClass is a little complex (since it can be tested in a VM).

Categories