Use UserControl or WebForm? - asp.net

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.

Related

Content tag is not loading when i add a asp.net web page with master page (master page having two master page

Someone please look at this issue.
<%# Page Title="" Language="C#" MasterPageFile="~/Master/Nested.master" AutoEventWireup="true" CodeBehind="Index2.aspx.cs" Inherits="XYZ.Admin.AdminDashboard.Index2" %>

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>

using a masterpage's namespace from content page

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.

ASP.NET MVC RC2 template head after body?

When I installed ASP.NET MVC RC2, I noticed that the template had changed from RC1. Now, all new views have the header placeholder after the main content place holder. Why is this? It seems very illogical to me and it most definitely was not the case with RC1. I googled but couldn't find any reasoning for this change. Do you know of any?
Example:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Rules</h2>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
Note how the placeholder with ContentPlaceHolderID of "head" is at the bottom? Weird...
I can't say I noticed it when I upgraded, but it shouldn't matter. The two <asp:Content> sections get mapped by the ID property to their places as defined in Site.master.
Definitely check the order in Site.master, but it should be fine.
If you want to change this, you can look into the T4 template your views are using. Check out:
t4-templates-in-asp.net-mvc
t4-templates-a-quick-start-guide-for-asp-net-mvc-developers
overriding-global-t4-templates-in-asp-net-mvc-project-with-per-project-templates

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