My scenario is I have a page name UserMain.aspx and in this page I have 2 sections (ie2 IFrames). Now from one of the IFrame pages I want to get parentUrl (ie www.xyz/UserMain.aspx). I have tried the Request.url but it's giving the url of IFrame, how to get the parentUrl?
Both IFrame and parent pages are on same domain.
You could try using the following. I tried it in one of my solutions but it was not quite what I needed. Maybe it will help you
Request.UrlReferrer.OriginalString.ToString();
The different windows and iframes only exist in the browser, the server code has no means to navigate between them.
In clientscript you can access the URL of the parent window, given of course that the page and iframe is from the same domain:
var parentUrl = window.parent.location.href;
string UrlBrowser= Request.UrlReferrer.OriginalString;
you can do this through client-side script using window.parent.location.href. This works in case you have an iframe at first level inside a loaded page. If you have more levels of hierarchy, like iframe within iframe ... then you can use window.top.location.href.
window.top always gets you to the topmost parent window.
Related
I have a _layout.cshtml page that renders the header and footer for every page. In my _footer.cshtml I have a newsletter form. As of right now, the newsletter form is rendering on every page. I need it to only render on the home page footer.
My thought for a solution is just using an if statement that checks if we're on the home page:
#if(Request.RawUrl == "/")
{
Newsletter form
}
My problem with this is that whenever I try to get the url, it's the same on every page.
For example:
the url might be https://something.com/checkout, so Request.RawUrl gets /checkout. But when I navigate to https://something.com, Request.RawUrl still gets /checkout. How can I get the actual path of the page?
I've tried using just about every request.url option I can find. None of them get the actual url path of the page after being redirected to a new page. Just whatever the original one was.
The best way would be to use a ViewModel instead of querying the Request object.
I have a user control in the Master page. In the code behind of that ascx, I want to get the title of the page. The title is being set in the head section with tag in the child pages.
Try this
var obj = this.Parent.Page;
var title= obj.Title;
Have you tried Page.Title?
However, I think the head tag needs to be ran server side to use this
I would think Page.Title would be enough. If not, you have to ride up through the object model a bit until you get to the page. Both solutions have been detailed above. The only variation may be if the ascx is set on the master page rather than the page. Worst case here is getting the title in the master page and feeding to the ascx as the page is rendered.
Now, an understanding of why this gets a bit confusing. Most people think the page sits on the master page. But, technically, the master page is set up as a control on the page. This is largely to avoid a complete rearchitecture of ASP.NET as master pages were introduced. This means the page is requested and starts to render. Then the master page linked tags are hit and that "control" is rendered, etc. In some cases, Microsoft has provided easy short cuts, in others, you have to navigate and the navigation is upside down from many people's expectation.
OK, so my problem is I need to pass variables to an iFrame. I can do the following:
<iframe src="mypage.aspx?var=myvariable">
Which is fine, but ideally I'dlike to set up the request for the iFrame to have var stored in POST variable, as opposed to the GET. I'm pulling out what little hair I have left trying to solve this problem.
Any gurus out there in SOLand got any ideas?
TIA
Peter
As for posting to IFrame, check out this post. Perhaps, you could set a session variable in the parent page and retrieve it the page that's loaded by the IFrame.
You could also try and inherit both the parent page and the page contained in the iframe from a basepage. Putting in whatever variables you want and being able to call them at will. Something like.....
Create a seperate vb or c# whatever your flavor in your appcode folder. Call it BasePage
On your Iframe and Parent page put in your
Inherits BasePage
Dim Public myvariable ....etc.
All the Google finds I ran into tell me how to use FindControl to access a control on the master from the content page itself.
However, what I'm trying to do is the opposite.
From the master page, I want to reference whichever child page is in the ContentPlaceHolder.
Why you ask.
I want the master page to know which tab should be active depending on the content Page currently in the placeholder.
This lets me avoid having each page to reference the master page and allow them to change the active tab; that should be the master page's job (if there's a way it can know whom it's enclosing).
Thanks. No rants please.
If you are looking to get the instance of the executing page class, you can retrieve it from the current HTTP context:
var page = HttpContext.Current.CurrentHandler as Page;
From there, you can navigate the page's control tree, call FindControl(), and so on. Be cautious about page lifecycle, though, as master page events tend to fire before their page event counterparts.
I'm trying to use ResolveUrl() to set some paths in the code behind of a custom ASP.NET user control. The user control contains a navigation menu. I'm loading it on a page that's loading a master page.
When I call ResolveUrl("~") in my user control it returns "~" instead of the root of the site. When I call it in a page I get the root path as expected. I've stepped through with the debugger and confirmed, ResolveUrl("~") returns "~" in my user control code behind.
Is there some other way I should be calling the function in my user control code behind to get the root path of the site?
look at System.Web.VirtualPathUtility.ToAbsolute.
wonde's comment above led me to the answer --
I was attempting to use ResolveUrl before the control's page load fired. Therefore there was no context page for the function yet.
I moved my code into the page load function and now it's resolving as expected.
Thanks for the nudge in the right direction.
Take a look at this MSDN example. http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx. and Rick Strahl's Web Log http://www.west-wind.com/Weblog/posts/154812.aspx
I suppose if it works on the page but not the control i guess you could try
this.Page.ResolveUrl("~");