How to know which page has called the Usercontrol - c#

I have a usercontrol(.ascx) page that is used by about 10 pages to display something in common. In the control i should know which aspx page is using(or calling). How is that possible?
Thank you in advance!

You can get the page object by calling:
this.Page
Or the URL by calling
HttpContext.Current.Request.Url

You can check Request.Url or (Page)HttpContext.Current.Handler.

The control has a Page property that will be set to the page that it is on.

Related

Master Page to pass information?

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

Windows Phone Silverlight page navigation

I have two questions here about Windows Phone page navigation:
Is there a way to get the instance of the page I am navigating to? That is, if I am on page one and want to navigate to page2 to when button click, can I get the page2 instance after page2 is initialized by NavigationService.Navigate("page_2_uri") call in page 1?
Is there a way I can know which page I navigate from? For example, I am currently on page3, and I want to do something like: if page 3 is navigated from page 2, I will do this, otherwise I will do that.
Thank you.
Is there a way to get the instance of the page I am navigating to?
No.
Is there a way I can know which page I navigate from?
Yes. Traverse the NavigationService.BackStack
The idea for using the NavigationService to navigate between pages is that you don't need to know any details about your destination. So in your example, Page 2 isn't initialized until you've left Page 1, and therefore Page 1 is no longer in scope, and won't be able to do anything with Page 2. If you want to pass information/context to Page 2, id recommend using Query Parameters (see next answer). If you want to know where the navigation is going, you can override the OnNavigatedFrom event and look at the Uri property of the NavigationEventArgs.
I would recommend looking at the NavigationContext property of the Silverlight Page class. This property lets you view the QueryString of the navigation request. Using this approach, you could navigate to page 3 using a uri like "page_3?previous_page=2" and then extract the previous_page from the QueryString of the NavigationContext to see where you came from.

Refresh Page C# ASP.NET

Is there a Page.Refresh type of command to refresh a page?
I don't want to redirect to the page or refresh in JavaScript.
I think this should do the trick (untested):
Page.Response.Redirect(Page.Request.Url.ToString(), true);
Careful with rewriting URLs, though. I'm using this, so it keeps URLs rewritten.
Response.Redirect(Request.RawUrl);
Response.Redirect(Request.Url.ToString());
You can just do a regular postback to refresh the page if you don't want to redirect. Posting back from any control will run the page lifecycle and refresh the page.
To do it from javascript, you can just call the __doPostBack() function.
Use:
Response.Redirect(Request.RawUrl, true);
You shouldn't use:
Page.Response.Redirect(Page.Request.Url.ToString(), true);
because this might cause a runtime error.
A better approach is:
Page.Response.Redirect(Page.Request.Url.ToString(), false);
Context.ApplicationInstance.CompleteRequest();
Depending on what exactly you require, a Server.Transfer might be a resource-cheaper alternative to Response.Redirect. More information is in Server.Transfer Vs. Response.Redirect.
I use
Response.Redirect(Page.Request.Path);
If you have to check for the Request.Params when the page is refresh use below. This will not rewrite the Request.Params to the URL.
Response.Redirect(Page.Request.Path + "?Remove=1");
I use # for Current page url address at Redirect to Refresh and that working currectly.
What do you think about this:
Response.Redirect("#")
Call Page_load function:
Page_Load(sender, e);
To refresh the whole page, but it works normally:
Response.Redirect(url,bool)

Master Pages ASP.net in C# adding dynamic flavor

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.

How to determine which Child Page is being displayed from Master Page?

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
}

Categories

Resources