Form tag inside master page and content page - asp.net

I have one master page and several content pages , I need to put a form tag inside a master page to handle sending data from my html elements and do this for my content pages as well .but I need to understand what is the best way to structure for such this scenario and what would be the effect of form tag of master page on content pages ? is it possible to put form tag in content pages when the master page has this tag inside itself ? I appreciate if I have in detail explanation ?

The <form runat="server"> element lives in the master page by default when you add a new one to your project; all child pages are implemented using ContentPlaceHolders.
For example, the master page: -
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
You can have as many ContentPlaceHolders as you need (often only one is needed though). If you then add a "child page using master page", the page-specific content is added inside the relevant <asp:Content> element - these are added by default once you have specified the master page to use when adding a "child page using master page": -
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<!-- your markup, controls etc.. -->
</asp:Content>
Have a read of the MSDN docs for more - http://msdn.microsoft.com/en-us/library/aa581781.aspx

Related

ASP.NET Master Page not updating Child Page

I have made a site that is based off of a MasterPage and after creating a few pages I added a new <asp:ContentPlaceHolder> but it seems the existing pages wont update to include it, however new pages do.
Is there a way to force an update, or is there a proper way to do this? I just saved the Master Page and hoped for the best.
You need to add the new <asp:ContentPlaceHolder> to your old aspx pages manually.
in your master page:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="new_one" runat="server"></asp:ContentPlaceHolder>
in your old aspx pages, add:
<asp:Content ID="Content3" ContentPlaceHolderID="new_one" runat="server">
</asp:Content>
If you create a new content placeholder in your master page, your content pages don't NEED to have a corresponding <asp:Content>. They only need one if you want to replace the default content placed in the master page.
And if you're going to update each page with its own content, then adding the <asp:Content> tags is likely a relatively small inconvenience.
Unfortunately there is not a way for Visual Studio to search out all your content pages and update them with new <asp:Content> controls.
However, you could do it fairly quickly by finding using the id of an existing <asp:Content> control, and do a ctrl+shift+F and replace with your new <asp:Content> plus what you searched for.
For example, if your content pages already all had a
<asp:Content ID="Content3" ContentPlaceHolderID="old_one" ... />
then you could search for that and replace with
<asp:Content ID="Content4" ContentPlaceHolderID="new_one" runat="server" /><asp:Content ID="Content3" ContentPlaceHolderID="old_one" ... />
Not exactly pretty, but it could save some time.

How to add master page to already created webform?

I have a asp.net webform application. Now I have to add master page in this application, but I don’t know how to merge or add new created master page with old webforms? How to handle html in webforms like <head> , <body> ? Any link in this regard will be helpful.
1- Define the fixed elements in your design, and put them inside the newly created master page
2- Define the dynamic ones, and add asp:ContentPlaceHolder for them ( most commonly one for HEAD, one for main content in your BODY , and one for side content "if applicable")
<asp:ContentPlaceHolder ID="CphHead" runat="server">
</asp:ContentPlaceHolder>
3- In your pages, add MasterPageFile="~/MASTER_PAGE_PATH" inside the Page directive.
4- Add asp:Content sections inside your pages which will hold the dynamic content in your pages, and don't forget to reference the correct ContentPlaceholder ID.
<asp:Content ID="HeadContent" ContentPlaceHolderID="CphHead" runat="server">
// Your content goes here...
</asp:Content>
5- Copy your page content inside these asp:content sections, and BOOOOM....you are done.
at the top of the new page in the '<%# page #>' tag add 'MasterPageFile="~/Site.Master"'
then add the needed placeholders
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
of course modify these to the names you are using
you can add the content holder tag in master page. So when you add 'MasterPageFile="~/Site.Master"' then you able to add content of other pages.

Help in solution more than one form in asp.net as to my logic its compulsory

As per my layout one form(all forms with runat=server) should be in header right top and
the other one I want to place in section's article. So I will have no problem in positioning of contents. How do I solve it?
My design: Login & logout are contained in form at header top-right;
article:Embedding AJAX4 accordian & tab container
but this requires it to be placed in form as <asp:scriptManager should be in form having runat=server.
(article has aside_left & aside_right near to it seperated and header at top seperated which contains the form)
So any Solution for this?
Do I have to change the logic?
I am working with visual studio 2010 asp.net 4 IIS v6 of windows7 x86
If I understand your problem correctly, you can do what you are looking for with Master pages: http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
Create a master page with a content section for the header and another for the rest of the page. The create pages for both of those content sections.
This sounds like you need a MasterPage containing your login and logout buttons, and then a content page containing your 'article'.
Master page (.master)
<form id="form1" runat="server">
<!-- login / logout controls here -->
<asp:ContentPlaceHolder ID="Article" runat="server">
</asp:ContentPlaceHolder>
</form>
Content page (.aspx):
<asp:Content ID="ArticleContent" ContentPlaceHolderID="Article" runat="server">
<!-- Accordion and things here -->
</asp:Content>
If that's not what you're after, then this link provides details on how to implement multiple forms on a page in ASP.NET.

How to identify content page loads in a master page dynamically?

I have a master page 'Master1' and i have used a content place holder 'content1' and loads
pages 'Page1' and 'page2' in the 'content1'. Is there any way to identify which page is loaded to the content place holder whether it is 'Page1' or 'Page2' dynamically.
I think maybe you have misunderstood how master pages work?
In you master page you define the guts of your page that you want all pages to share, like headers and footers etc. The you define a content holder:
<asp:ContentPlaceHolder runat="Server" ID="MainContent">
</asp:ContentPlaceHolder>
The contents of Page1.aspx etc will be injected into that ContentPlaceHolder.
Then in your aspx pages you simply define which master page it should use:
<%# Page Language="C#" MasterPageFile="~/MyMasterPage.Master" ... />
and then define the content that should be injected in to the place holder in the master:
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
put all your html in here
</asp:Content>
Note the id used for the ContentPlaceHolderID property, so that it gets injected into the right place - the master page could have several place holders. So both Page1.aspx and Page2.aspx can be loaded in to the place holder, all you have to do is navigate to whichever page you want to display. Nothing needs to be done on the master page.
Try this
ViewContext.RouteData.GetRequiredString("action")

ASP.NET page content - where does this belong?

I am working on a web application that has a single master page and several content pages. Each page will have a small sidebar to the right of the main content with some brief content. However, that brief content is specific to the page you are on. I can't decide whether to put that on each individual page, or in the master page in a MultiView with some logic in code-behind to specify which view is shown based on which page you are on.
Which seems more elegant? I'm still fairly new with ASP.NET and I'm trying to get a good feel for proper architecture, etc.
You can create multiple content placeholders in a single masterpage. So in your case I would create two. One for the article's content and one for the sidebar like:
<!-- some html-->
<asp:contentplaceholder id="ArticleContents" runat="server">
</asp:contentplaceholder>
<!-- some more html-->
<asp:contentplaceholder id="ArticleSidebar" runat="server">
</asp:contentplaceholder>
<!-- even more html-->
then you could have the article contents and the sidebar contents both in the same page and place it in the correct spot using something like
<asp:Content ID="article" ContentPlaceHolderID="ArticleContents" Runat="Server">
Your article
</asp:Content>
<asp:Content ID="sidebar" ContentPlaceHolderID="ArticleSidebar" Runat="Server">
Your sidebar
</asp:Content>
If you want some manageability in the case of your site getting larger and needing more of these custom sidebars then I would not put anything beyond standard layout in the master page.
What is wrong with having an additional ContentPlaceHolder in the side bar and just adding the content into Content controls on each content page?
Your approach seems overly complex to me.
You could also put that additional code into a user control (or two). The content page would then include the user control appropriate to that page for the right sidebar.

Resources