I'm writing code on the master page, and I need to know which child (content) page is being displayed. How can I do this programmatically?
I use this:
string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;
It retuns the class name in this format "ASP.default_aspx", but I find that easy to parse for most purposes.
Hope that helps!
It's better to let the ContentPage notify the MasterPage. That's why the ContentPage has a Master Property and MasterPage does not have Child property.
Best pratice in this is to define a property or method on the MasterPage and use this through the Master property of the ContentPage.
If you use this technique it's best to explicitly specify the classname for the MasterPage. This makes to use the MasterPage in the ContentPage.
Example:
//Page_Load
MyMaster m = (MyMaster)this.Master;
m.TellMasterWhoIAm(this);
Hope this helps.
This sounds like a bad idea to start with. The idea of the master is that it shouldn't care what page is there as this is all common code for each page.
I have had a reason to check the child page in the master page.
I have all my menu options on my master page and they need to be disabled if certain system settings are not set up.
If they are not then a message is displayed and the buttons are disabled. As the settings page is a content page from this master page I don't want the message to keep being displayed on all the settings pages.
this code worked for me:
//Only show the message if on the dashboard (first page after login)
if (this.ContentPlaceHolder1.Page is Dashboard)
{
//Show modal message box
mmb.Show("Warning Message");
}
Use the Below code.
Page.ToString().Replace("ASP.","").Replace("_",".")
You can use:
Request.CurrentExecutionFilePath
Here is my solution to the problem (this code goes into the code behind the master page):
if (Page.TemplateControl.AppRelativeVirtualPath == "~/YourPageName.aspx")
{
// your code here
}
or a bit more sophisticated, but less readable:
if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase))
{
// your code here
}
Request.CurrentExecutionFilePath;
or
Request.AppRelativeCurrentExecutionFilePath;
I do something similar to this in a project of mine to dynamically attach css files based on the page being loaded. I just get the name of the file from the request:
this.Request.Url.AbsolutePath
And then extract the file name from there. I'm not sure if this will work if you are doing URL re-writes though.
You can do this by getting the last segmant or the request and I'll be the Form name
string pageName = this.Request.Url.Segments.Last();
if (pageName.Contains("EmployeeTermination.aspx"))
{
}
You can try this one:
<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>
Put that code within the title tags of the Site.Master
string s = Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
if (s == "default.aspx")
{ /* do something */ }
so many answers I am using
<%if(this.MainContent.Page.Title != "mypagetitle") { %>
<%}%>
this makes it easy to exclude any single page and since your comparing a string you could even prefix pages like exclude_pagetitle and comparing a sub-string of the title. I use this commonly to exclude log in pages from certain features I don't want to load like session timeouts and live chat.
Below code worked like a charmed ..try it
string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
Page.Request.Url.PathAndQuery or one of the other properties of the Url Uri object should be available to you from the master page code.
You can check the page type in the code-behind:
// Assuming MyPage1, MyPage2, and MyPage3 are the class names in your aspx.cs files:
if (this.Page is MyPage1)
{
// do MyPage1 specific stuff
}
else if (this.Page is MyPage2)
{
// do MyPage2 specific stuff
}
else if (this.Page is MyPage3)
{
// do MyPage3 specific stuff
}
Related
Assume I have an list or array (doesn't matter) in a aspx.cs page. How can I pass that list/array etc to send it to a master page. Then in a function use that information to pass to an .aspx page without passing the list/array to the function?
Hope this makes sense. Thank you.
My best guess to your answer would be storing the variable in session (Session.MyVariable) and once the child page loads, access it via that way. You wouldn't necessarily be passing it from the layout page to the view but this would allow you to get whatever data to the view once it's loaded under the layout page. If that helps, not entirely sure I understand.
In the "header" of the child page
#{
if (Session.MyVariable != null)
{
// do something on the webpage with this data
}
}
May be this link will help you
https://www.codeproject.com/Articles/333650/Beginner-s-Tutorial-on-Master-Pages-in-ASP-NET
Go to the part where he describes "Changing the Master Page's Properties from Content Pages", he defined a label in site master, then he was able to change the text of this label from the content pages.
My best guest is to pass like a serialized XML and then parse it in master page
I have one aspx page where I want to display contents of another aspx or html page based on the sessions. Code behind logic will fetch the file from particular location based on the session. I do not want to use frames, iframes because I have to make sure of cross browser compatibility. Is there any other way to achieve this?
User control may be a solution for this. I wanted to know more alternate approach.
Below is my code:
I don't have anything in aspx except the references to the external js file.
Code behind:
private void Page_Load(object sender, System.EventArgs e)
{
_getMainMenu();
}
protected string _getMainMenu()
{
string HomePageMenu = string.Empty;
string homeExist = Server.MapPath("../homepages/" + Session["ACCOUNT"].ToString() + "/HomePage.htm");
if (File.Exists(homeExist ))
{
HomePageMenu = "../homepages/" + Session["ACCOUNT"].ToString() + "/HomePage.htm";
}
else
{
HomePageMenu = "../homepages/NewMenu.html";
}
return HomePageMenu ;
}
Thanks.
Sounds like maybe something like jQuery load is something that may be helpful. This will asynchronously retrieve content and place it into a control of your choice, and not necessarily an iframe of course. This is called from the client, but you can have your server side logic/session state determine the content returned.
You should change the way you manage your content in generally.
In your case, your aspx-page is your content. And if you want to show contant b in contant a, you will have to show an aspx-page inside another aspx-page.
Change the point of view: your aspx-page should only be your content-holder. Extract your content to an ascx-usercontrol or store it in a database.
Then you are able to fetch the content whereever you need them.
Showing an aspx-page inside another is a very bad solution.
But if it's not possible to change the way your content is stored: I would prefere an iframe.
Is it possible to acces a control thats located in a content page (withing a content place holder, a multiview control to be more exactp) from the master page?
The situation is, i have a menu with buttons thats located in the master page.
Now in my content page i have 1 content place holder.
In which a multiview with several views is located.
If i press a button in the menu (MasterPage) then it should open the proper view (with its controls) displayed in the content place holder area.
I have set the ActieveViewIndex=0 but i am getting all sorts of wierd behavour.
I have to do something with the ActiveViewIndex++ somewhere but nothing seems to work.
edit::
string a = Request.Querystring["one"]
string b = Request.QueryString["two"]
if ( a == "addOne") // where addone is a redirection to the content page from the master page button
{
mvMultieView.SetActiveView(vView1);
}
else
if ( b == "addTwo")
{
mvMultieView.SetActiveView(vView2);
}
Any suggestions?
Kind Regards.
You can easily do that using find control
View myView = (View)this.Master.FindControl("PlaceHolderFullMain").FindControl("PlaceHolderMain").FindControl("Mymultiview")
The way my team and I accomplished this task (and I don't know that it's the best method, but it was effective) was to use query strings (as you had in your previous question it looks like). We established a standard QS variable called iView that would determine the name of the view in question (not necessarily the control name itself, but some keyword that the content control would respond to). Since all of our pages/controls have a page base class they inherit from, we put a method in the base class (in our case it was at the page level, but in yours a control level might work) that was responsible for getting the requested view. In the control we would have a mechanism (switch perhaps) that would set the activeView. In some cases we just used the actual ID of the control (since it was a mystery to be obfuscated) and avoided the switch altogether.
http://www.mydomain.com/mypage.com?iView=mySecondView
partial class MyControl : System.Web.UI.WebControl
{
// blah blah control stuff
public string getRequestedView()
{
return (Request.QueryString["iView"]) ? Request.QueryString["iView"] : String.Empty;
}
}
...
protected void Page_Load(object sender, EventArgs e)
{
View myView = myMultiView.FindControl(this.getRequestedView());
if(myView != null)
this.MyView.SetActiveView(myView);
}
have you done this change in the Master page or content page?
because i also got the same problem.
I have link buttons in my master page which i need to activate view controls in content page.
the content page is not the currently loaded page.
will this method help for my solution?
Forgive me if this has already been asked somewhere, but I cannot figure out the best way to accomplish this task. I want to be able to create a rendering system that will allow me to render out content from thousands of different .aspx pages without having to create thousands of .aspx pages. That being said, I still want to be able to render out the appropriate .aspx page if it exists in my code.
For example, when a request is made to the site, I want to check and see if that URL is in the database, if it is, then I want to render the content appropriately. However, if it doesn't, then I want it to continue on to rendering the real .aspx page.
In trying to use an HTTPModule, I cannot get the page that exists in the database to write out the appropriate content. Here's my code.
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
Uri url = application.Context.Request.Url;
//Checks to see if the page exists in the database
PageInformation page = PageMethods.GetPageFromUrl(url.AbsolutePath);
if (page != null)
{
string renderedPage = Renderer.RenderPage(page);
application.Context.Response.Write(renderedPage);
}
}
However, when trying to use an HTTPHandler, I can't get the real .aspx pages to render appropriately because the *.aspx verb is being dealt with by the handler.
If anyone has any better ideas on how to completely re-design this, I'm completely open to that as well. Thanks.
This will do the trick:
Type page_type = BuildManager.GetCompiledType ("~/page.aspx");
Page page = (Page) Activator.CreateInstance (page_type);
page.ProcessRequest (Context);
I think you're lookign for a simple URL rewriting example.
So you have a single page "default.aspx" that could take an argument of the content you want to display "default.aspx?page=home", but you don't want the nasty query string part "?page=home".
this is best solved by URL rewriting which can be used as an ISAPI module in IIS. So instead of the URL string above, people see a page called "home.aspx", and the web server translates this into "default.aspx?page=home" for your page which can go get the content for the "home" page out of the DB and display it on the screen.
Here's a page with more information on a good implementation of this process:
http://www.opcode.co.uk/components/rewrite.asp
I believe this shows how to process the "normal" pages inside a handler
other example
I need to have a button on the master page.
Once that button is clicked I generate a string that represents URL.
test.apx is a content page I use and the string will look like something like this:
Example:
www.blah.com/test.aspx?user=blax&develop=extreme_all
Now all I need is to reload the page while content is redirected to the URL I generated.
I hope this makes more sense.
Thanks guys I am new to asp.net and really appreciate any help
Why dont you use Update Panel?
Have the page postback with the updated query string to change what is in your content area
Assuming your masterpage is set up correctly
within the <asp:content> tag of your aspx page that is using the masterpage you created add code to get the query string
Request.QueryString["key"]
example url: http://www.whatever.com?foo=bar&bar=foo
string tmp = Request.QueryString["foo"]
tmp will become "bar"
Now just check the "postback" option of the asp:control you're using to reload the content page or do whatever you to make the page refresh.
If I understand your question correctly, you want to reuse the same code to parse out your user and develop variables from different content pages that use the same master page.
It sounds like you need a strongly typed master page.
First, put your shared code in your master page. Then, expose the parsed data as properties of the master page. Next, simply add the following directive in your content pages:
<%# MasterType VirtualPath="~/mymasterpage.master" %>
Finally, in your content pages, you can reference your properties as such (assuming you created a property called MyUser):
string user = this.Master.MyUser;
You can also use inheritance if you want a different approach. Simply create class that inherits from Page. Then put your shared code in that class. Finally, make your content pages inherit from your new class, instead of Page.