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?
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.
You still have access to the request object from the user control, so do something like this:
string currentUrl = Request.Url.AbsoluteUri.ToString();
Request.UrlReferrer will get you the URL of the previous page... usually. There are some situations where it could be empty:
As long as your code "plays nicely" when UrlReferrer is empty or invalid, you should be good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With