using a masterpage's namespace from content page - asp.net

Can i use namespace which is imported on masterpage ?
i can import and use namespace on masterpage like that..
<%# Import Namespace="utl=portal.lib.SnkUtilities" %>
and on markup;
<a href='<%= "/" + utl.getSomeString() + "/cart.aspx" %>'>
but if want to use same namespace on a contentpage, i have to import same namespace on content second time as follows:
<%# Page Title="" Language="C#" MasterPageFile="~/Master" AutoEventWireup="true" CodeBehind="Account.aspx.cs" Inherits="portal.secret.Account" %>
<%# Import Namespace="utl=portal.lib.SnkUtilities" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<%= utl.getAnotherString() %>
</asp:Content>
Do you have any suggestions ?
Thanks in advance

You are thinking that the master page is read first, but in the page life cycle, the master page is read after the Page Request, so you have to include the namespace in every page. It first looks in the current page and if it does not find it at the compile time, you will get error.
When the page is fetched, the # Page directive is read. If the directive references a master page, the master page is read as well. If this is the first time the pages have been requested, both pages are compiled.
Read from the End of the page Run-time Behavior of Master Pages
Note that the master page becomes a part of the content page. In effect, the master page acts in much the same way a user control acts — as a child of the content page and as a container within that page.

Related

Use UserControl or WebForm?

In my home page I have 3 sections; Services, About and Portfolio.
I want to separate the HTML/code for each of these sections into their own files (WebForms/UserControls) so I can reuse these on other pages.
In ASP.NET WebForms; is the correct way to construct these sections as UserControls or WebForms with no master file?
As I understand a UserControl is more for a custom widget/tag such as a custom GridView or something. So maybe UserControls wouldn't suit this?
Eg;
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="My._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
// Include About
// Include Services
// Include Portfolio
</asp:Content>
In case you are going to reuse the sections across the web site I'd go with UserControls. It's more versatile. If you'd put the code to web forms you'd be dependent on the masterpage's ContentPlaceHolder.

Error on Adding Master Page.

I have a using ASP.net C# firstly I have created a webform FrmFirst.aspx. Then I have created a master page. Now I have to add my first page to master page then I am facing the same error :
I am using this code to add my first page to Master page
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master" CodeFile="FrmCustomerMaster.aspx.cs" Inherits="FrmCustomerMaster" %>
Tell me my Problem !! Thanks ....
You need to change your body to content sections. The master page works with contentplaceholders: your content pages need to provide the content for these placeholders
see LINK
if your master page contain this code
<asp:contentplaceholder id="Main" runat="server" />
your page that Inherits from master should have
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
</asp:Content>

Unable To See Master Pages While Adding A Page In The Project

i am trying to add a .aspx page in the project , but while adding the .aspx page i am not getting the option to choose existing master pages.
Don't know why it is not visible; if you cannot set it, simply add it later manually to the #Page directive of your page.
It's simple:
MasterPageFile="~/Path/To/My/MasterPage.master"
E.g. on top of your ASPX page, write something like:
<%# Page
Culture="auto"
UICulture="auto"
MaintainScrollPositionOnPostback="true"
Title="The Title of your Page"
Language="C#"
MasterPageFile="~/Path/To/My/MasterPage.master"
AutoEventWireup="true"
CodeFile="MyPage.aspx.cs"
Inherits="MyPage"
%>
In addition, if you want to access the derived type of your master page, use the #MasterType directive:
<%# MasterType VirtualPath="~/Path/To/My/MasterPage.master" %>
Could be a bug with VS, try save the project with your master page then close and reopen VS.
If not as Uwe Keim mentioned above it is really easy to add the master page manually.

How to add code behind manually on asp.net

I have a website on the IIS but it has only the aspx file. Now I need to add the code behind for some pages. How do I go about this?
I've been trying to add the attribute "codebehind" and "autoeventwireup" on the top of the aspx file but no luck (the page_load event is not being called). Also, if I double click on the button from the design view in Visual Studio, it creates the javascript handle (not the server code).
In your page directive on the aspx page (<%# Page...), you need two pieces:
1) CodeFile="Default.aspx.vb"
2) Inherits="_Default"
In your code-behind file, you need two things:
1) The file name has to be whatever you specified above (CodeFile = "...)
2) the class name has to be whatever you specified above (Inherits="_...)
For example, if your aspx page was named "default.aspx" then your page direction in the aspx page would look similar to this:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
And your code-behind would look similar to this:
Partial Class _Default
Inherits System.Web.UI.Page
End Class
you want the #page directive and the class attribute. MSDN docs here

Master Page Error on New MVC Project

I've just create a new MVC project, and have made no changes at all, but when I try and view the ChangePasswordSuccess view in design mode, I get the error:
The page has controls that require a Master Page reference, but none is supplied.
The page has no controls except for a content control and literal text, and it does have a master page reference. Anyone know what's up?
<%#Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="ChangePasswordSuccess.aspx.cs" Inherits="BasicMvcApp.Views.Account.ChangePasswordSuccess" %>
<asp:Content ID="changePasswordSuccessContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>Change Password</h2>
<p>
Your password has been changed successfully.
</p>
</asp:Content>
No sooner asked than noticed. The default ChangePasswordSuccess view's Page directive is missing the word Page.

Resources