How to change content of master page dynamically - asp.net

I am developing a master page for an e-commerce website. I have Left-Side Menubar on the Master Page. This left menubar contains Category list and changes as user change categories. Categories are stored in SQL DB. So, I want to rebuild this menubar as category changes, Is it possible?
Have look on www.actgreen.com.au/www
And also I want to create Breadcrumb for website, how can I do.
I am using asp.net 4.0.
Thanks.

Well , we don't know what exact approach you are using in your code to bind left menu..Also you must be binding your left menu in your master page itself. So on each time you click on left menu you can rebind it again by providing some parameter to it.
or if you need to call it from some different page or from your content pages..then you can try it like this..
System.Reflection.MethodInfo mInfo = this.Page.Master.GetType().GetMethod("RefreshLeftMenu");
mInfo.Invoke(this.Page.Master, null);
I do it this way...hope its helpful for you too..

You can use controls on masterpage and change them on page OnLoad
Label MyLabelBrand = (Label)Master.FindControl("LabelBrand");
MyLabelBrand.Text = "Fabrikam";
If you want to call method Refresh() of Master page, you need call Master.Refresh();.
To do this you need register master type on ContentPage:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%#MasterType VirtualPath="Site.Master" %>

Related

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 apply master page to pages in asp.net 2008

I am using .net 2008 for developing web application.
So I created a master page and some other pages. I want to apply master page to all other pages but I'm not getting it. Earlier i use .net2005 here there is option when we created a new page in application and check the master page option, but in .net2008 this option is not there so please tell me how to do it. I'm new to .net 2008 .
Please tell me
Thank you
If you want to manually set the masterpage for pages you have already created you can do the following: In the Page directive of your ASPX pages you set the masterpage like this:
<%# Page Language="C#" MasterPageFile="~/MasterPages/Master1.master" Title="Content Page"%>
Your MasterPage defines a few Content areas. You then should reuse those in your page and fill them with the local content.
When adding a new page the documentation says that when adding a Web Forms page you can select the Select master page check box, and then click Add. Then the Select a Master Page dialog box should appear.
You indicating that in your question
"when we created a new page in application and check the master page option"
If you are using a web application when you wan't to add a page with masterpage
you should add the page like this
Project ->
Add New Item ->
Web Form Using Master Page (instead of selecting Web Form)
Then you will have the option to select the masterpage
Use this code:
<%# Page Title="test" Language="VB" MasterPageFile="~/MasterPageName.master" AutoEventWireup="false" CodeFile="PageName.aspx.vb" Inherits="PageNameClass" %>
<asp:Content ID="Content1" ContentPlaceHolderID="maincontent" runat="Server">
//here you can add you page data start from form tag till end of form tag and remove everything else.
</asp:Content>

scroll go to the top page

net use web methods to save data from JavaScript when i press save the all page go the top how i can save state position after post back
You can set it programmatically
Page.MaintainScrollPositionOnPostBack = true;
In the page declaration
<%# Page MaintainScrollPositionOnPostback="true" %>
Or in the web.configs <system.web> section.
<pages maintainScrollPositionOnPostBack="true" />
The reason the page is returning to the top, is because it is being Submitted and hence reloaded.
If you want to keep the position of the page you have 2 options;
1) Use a Anchor (See: http://www.hypergurl.com/anchors.html)
2) Post your form using AJAX and then you do not need to reload the page
Option 2 is the prefered, if you are using webforms you could use an AJAX Update Panel, or if you are using MVC you can do it using JQuery.
try to setting the following page directive in your page
<%# Page MaintainScrollPositionOnPostback="true" %>
I solved the question, and my method of doing this was the following:
I put a javascript:void(0) in an a tag, as shown here:
<a href="javascript:void(0)">

How to access controls inside a nested master page? why does it behave differently from content pages?

Is there a difference between these two scenarios:
(1) Accessing a property on a master page from a regular child
(2) Accessing a property on a master page from a nested master page
I tried to access a textbox in the master page from a content page like this:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
defaultTextbox.Text = a.Text; // defaultTextBox is a textbox control inside default.aspx
it works, but then when I applied the same method on a nested master page:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
myTextBox.Text = a.Text; // myTextBox is a textbox control inside child.master
this does not work, am I missing something? I call both codes inside regulare page_load handler...
I also noticed I cannot set textbox value inside the nested master page from code behind, there is definitely something im missing, what is it?
To shed light on this issue, here is an example:
Nested Master Page:
<%# Master Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:textbox id="tx2" runat="server" text="this is two"></asp:textbox>
<asp:contentplaceholder id="newstuff" runat="server"></asp:contentplaceholder>
</asp:Content>
Code behind:
Response.Wrote(tx2.Text);
I get NOTHING, why what did I miss? note that I also tried the recursive find control:
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
still nothing
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
//base content place holder id
Label objLabel3 = (Label)cp.FindControl("lblNested");
//lblNested is id in nested master page
I read few things here:
http://www.odetocode.com/Articles/450.aspx
and found out that the nested page in the middle never calls Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following:
protected override void OnLoad(EventArgs e)
{
myTextBox.Text = "anything";
base.OnLoad(e);
}
This should work without any problems, so something else must be wrong. I just tried it inside a simple test project and I have no problems finding a control on the master page in both cases.
I would check (again) if you refer to the correct master page inside your nested master page. What you could also check is the runtime type of the Master property inside your nested master page. It should be the type of your master page.
EDIT: I thought the issue was about finding a control in the root master page from a nested master page and this should work without any problems. For finding a control inside a content placeholder in a nested master page, take a look at the following forum post.
You can have absolute control of your content in both master page and nested page from your content page using the directives:
<%# MasterType VirtualPath="your_master.master" %>
<%# Reference VirtualPath="~/your_master.master" %>
See the excellent article from K. Scott Allen in Ode To Code

Form tag on ASP.net page

I have a web application that has a page that loads the content from the database. I want to be able to put a form in the dynamic content, but .net doesn't let the inside form perform it's action. Is there a way to allow this or some other way I can get a form on a dynamic content page?
--EDIT--
I think I need to clarify something. This is an aspx page that loads content from the database. As far as I know, the text I pull from the db and stick in the Label is never compiled or processed by the .net wp, thus I can't use the code behind to fix this issue.
This is a common problem, when you want to have a non-postback form to a 3rd party site (like a PayPal button, for example).
The problem occurs because HTML doesn't let you have form within a form, and most ASP.NET pages have a <form runat="server" /> "high up" in the HTML (or in the Master page).
My favorite solution is to hide the "high up" form tag, while still showing all of the content. Then you can feel free to dump any tags you want in the body. If you do this dynamically you can choose on a page-by-page basis which pages have custom forms.
I created a class called GhostForm.cs to handle this. You can read all about it here:
http://jerschneid.blogspot.com/2007/03/hide-form-tag-but-leave-content.html
There can only be one form on the page (the asp form); you have to use that form somehow.
To clarify, there can only be one form processed.
Not with webforms, no. You have to work within the one, full page form by using an event handler connected to a Button to LinkButton. Fortunately, it's pretty easy to do:
foo.aspx:
...
<asp:TextBox id="txtFoo" runat="server" />
<asp:Button id="btnFoo" runat="server" onclick="btnFoo_Click />
...
foo.aspx.cs:
...
protected void btnFoo_Click(object sender, EventArgs e)
{
string s = txtFoo.Text;
// do something with s
}
...
Dino Esposito has an article from MSDN magazine that covers handling multiple forms or "simulating" sub forms in ASP.Net that might just answer all your questions.
http://msdn.microsoft.com/en-us/magazine/cc164151.aspx
Any work around would be hacky and very ugly. By design asp.net uses a form tag to post and get data. This is why they call it a Web Forms Application. Html does not allow nested forms. What you want to do is use a WebRequest in your code behind.
If you are trying something like a paypal button you could simply use something like this.
Markup:
<div id="PayPalButtonContainer" runat="server"></div>
Code Behind:
public static string GetPayPalButtonMarkup()
{
const string markup = #"https://www.paypal.com/cgi-bin/webscr
?cmd=_xclick&business={0}
&item_name=Widget
&amount={1}
&currency_code=USD";
return markup;
}
PayPalButtonContainer.InnerHtml = string.format(GetPayPalButtonMarkup,"YOUR PAYPAL USER NAME", "YOUR PRICE VALUE");
you either have to deal with the postback by adding a server side click event handler to what you want to be the "sub forms" submit button (this is how web formas deals with multiple submit type buutons on the same page) or do soemthing clever with AJAX if you dont want a full post back
I've run across this issue before. One workaround that I have done is to place my code that I want my action to be done upon inside of an asp:Panel. With the panel you can set the attribute of "DefaultButton" to a button inside of the panel, and clicking the button (or pressing "enter") will fire that button's click event. I've found this quite handy when wanting to submit a "form" by pressing enter when I have a master page that contains the only allowable asp:Form.
Hope this helps.
When I first came across this problem, I found the simplest solution for me was to simple COPY and PASTE the Master page and give it a slightly different name, something like:
SiteNameMasterPage 'Default page with FORM tag
SiteNameMasterPageNF 'No Form tag
And then depending on wether I wanted a FORM tag or or not, simply change the masterpage link at the top of my CONTENT-PAGES, like this
<%# Page Title="" Language="VB" MasterPageFile="~/SiteName.master" %>
<%# MasterType VirtualPath="~/SiteName.master" %>
<!-- This masterpage has the default FORM tag -->
or
<%# Page Title="" Language="VB" MasterPageFile="~/SiteNameNF.master" %>
<%# MasterType VirtualPath="~/SiteNameNF.master" %>
<!-- This masterpage does NOT have the default FORM tag -->
and then in the content page, wherever I want to place my form I can include the <form> tag

Resources