ASP.NET C# - Showing right contentplaceholder in default.aspx - c#

I am trying to help friend with his website which is written in asp.net. I am newbie in asp.net but I know php and mysql. Right know I am trying to figure out, how to declare which page (something.aspx) is shown in ContentPlaceHolder.
For example:
I have one master page (web.master) on which I have:
< asp:ContentPlaceHolder id="cpMainContent" runat="server">
Then I have many content pages (f.e. article.aspx, section.apsx) on which I have:
< asp:Content ID="Something" ContentPlaceHolderID="cpMainContent" runat="server">Some content
So my question is, how the website knows which .aspx file to open? If I open my friends website, I found out, that in cpMainContent is content from file section.aspx. If I make new page, like section2.aspx, how should I let the website know, that it should use the new created page?
Thank you very much for responses.

When creating a new page, you give it a name and before you press "ADD" you can check off to "Select Master Page". It will bring up a dialog box of all master pages in the project and you can select one. Easy as that!

Master pages are not accessed directly by the user. Redirection can be performed by adding the directory pathto the anchor tag to other pages( content pages associated with master pages/ not associated with master page ).

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

Masterpage Vs Themes decision dilemma

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 !

Getting Child page title from user control on Master page

I have a user control in the Master page. In the code behind of that ascx, I want to get the title of the page. The title is being set in the head section with tag in the child pages.
Try this
var obj = this.Parent.Page;
var title= obj.Title;
Have you tried Page.Title?
However, I think the head tag needs to be ran server side to use this
I would think Page.Title would be enough. If not, you have to ride up through the object model a bit until you get to the page. Both solutions have been detailed above. The only variation may be if the ascx is set on the master page rather than the page. Worst case here is getting the title in the master page and feeding to the ascx as the page is rendered.
Now, an understanding of why this gets a bit confusing. Most people think the page sits on the master page. But, technically, the master page is set up as a control on the page. This is largely to avoid a complete rearchitecture of ASP.NET as master pages were introduced. This means the page is requested and starts to render. Then the master page linked tags are hit and that "control" is rendered, etc. In some cases, Microsoft has provided easy short cuts, in others, you have to navigate and the navigation is upside down from many people's expectation.

Not able to get data in content page through javascript in asp.net

I have created a small web application for display the selected employees data. In this application I coded a javascript. I have create a dropdownbox with username details along with them a checkbox is in front of every user. These username get from database. I want that manager dropdown the list and select the employee which he want to see the details. I did this with the help of javascript but the issue is that it successfully display the usernames in dropdown when it is saparate form from the master page but when i merge it with master page means make it content page of that master page it doesn't display the usernames in dropdown.
It display it blank. Where I m doing wrong. Any suggestions will be appreciated.
Thanks in advance.
As Pleun says the masterpage will change the HTML control ids rendered to the browser. There are a couple of ways round this If you are using asp.net 4 you can specify the client ids as follows:
<asp:Label Text="text" runat="server" ID="SomeID" ClientIDMode="Static" />
That will maintain the ID rendered to the browser.
If you are user a previous version of asp.net then i tend to user JQuery to get the controls using something like:
$("[id$='SomeId']")
The $= means ends with.
You can also user server markup mixed in with your javascript code e.g.
var control = document.getElementById("<% =SomeId.ClientId %>");
asp.net will then render out whatever client side id it assigns to the control with server id SomeId
The master page changes the names of the controls:
Have a look here:
http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs
(Not your question - but the functionality can also be done out of the box with ASP.NET without coding javascript yourself)
You need to give the client id of the controls in the function.
When you were using master page den the id of ur controls changes.
For eg:
The clientID of Checkbox chktest will be chktest without master page.But with masterpage the clientID changes to something like 'ctl00_ContentPlaceHolder1_chktest'
So the javascript will not detect if you use chktest when using master page and your functions wont work as expected.So use clientId of controls.

Can I know what page is in my master page's ContentPlaceHolder?

All the Google finds I ran into tell me how to use FindControl to access a control on the master from the content page itself.
However, what I'm trying to do is the opposite.
From the master page, I want to reference whichever child page is in the ContentPlaceHolder.
Why you ask.
I want the master page to know which tab should be active depending on the content Page currently in the placeholder.
This lets me avoid having each page to reference the master page and allow them to change the active tab; that should be the master page's job (if there's a way it can know whom it's enclosing).
Thanks. No rants please.
If you are looking to get the instance of the executing page class, you can retrieve it from the current HTTP context:
var page = HttpContext.Current.CurrentHandler as Page;
From there, you can navigate the page's control tree, call FindControl(), and so on. Be cautious about page lifecycle, though, as master page events tend to fire before their page event counterparts.

Categories