How to find the url of parent page of a User control
Asked Answered
O

4

10

I have a user control where if a certain action is performed I want to redirect to the page the user was on with some additional query string parameters.

So, if UserControl.ascx was on Home.aspx, I want to redirect to Home.aspx?action=true, and if UserControl.ascx was on Profile.aspx, I want to redirect to Profile.aspx?action=true

So basically, in my UserControl.ascx.cs I want to get the URL of the Parent Page. How can I get it?

Osseous answered 12/8, 2011 at 13:43 Comment(0)
U
15

You can look at the Request.Url, Request.RawUrl, Request.FilePath, and some of the other similar properties of the Request object - depending on how you're using this.

This will give you the requested URL from the browser, which will in turn tell you which page your control is living on.

Undesigned answered 12/8, 2011 at 13:49 Comment(1)
Request.Url brings back a System.Uri - on MSDN herePolytonality
C
2

You still have access to the request object from the user control, so do something like this:

string currentUrl = Request.Url.AbsoluteUri.ToString();
Cat answered 12/8, 2011 at 13:50 Comment(0)
M
1

Request.UrlReferrer will get you the URL of the previous page... usually. There are some situations where it could be empty:

  • links clicked from an email message
  • shortcuts saved to a desktop
  • spoofed URLs
  • perhaps some settings or browsers
  • probably other scenarios as well

As long as your code "plays nicely" when UrlReferrer is empty or invalid, you should be good to go.

Meaganmeager answered 12/8, 2011 at 13:50 Comment(0)
R
-1
Request.Url.Scheme + "://" + Request.Url.Host  + Request.RawUrl
Ramentum answered 27/4, 2016 at 19:18 Comment(1)
Your response would be more helpful if you explained why it resolves the problem.Gerstein

© 2022 - 2024 — McMap. All rights reserved.