Reputation: 53
Is is possible to retrieve the ID value from the Request.QueryString from a aspx file and pass it onto a ascx file in order to successfully update a profile using the retrieved ID?
Upvotes: 4
Views: 19087
Reputation: 11989
Often if something is in a UserControl, it is either because the functionality in the control is significant enough to be broken out into it's own reusable container that could be reused on another page. If that control is actually ever going to be reused on another page, it really shouldn't be referencing query string parameters because the control should make no assumptions about what page it is on. What if that control gets included on another page whose query string parameters are named differently? Or maybe on another page that value will come from the database or ViewState or will be automatically determined somehow? So my general rule is that if you are going to make a UserControl, never, never never make any assumption about the page it is being hosted on.
So like most people said, you can still access the Request.QueryString property from inside the UserControl, but that would probably not be the best idea. Creating a property on the control that gets set by the container page is a far better idea.
The best idea in my opinion, and what I almost always do, is create method called LoadData (or something similar) on the control, with parameters for all of those query string values you need. That way you have a single entry point for that data, so it's clear at what point those values are getting set and what they are getting set to. If you go the property route, there is always concern about whether all of the properties got set, and whether they got set in the right point in the page lifecycle (it can get tricky during postbacks)
Upvotes: 6
Reputation: 35822
In your aspx
page, in your ascx
user control, in your master page, in your custom control and almost everywhere, you can access the query string. Use one of these methods:
Page.Request.QueryString
HttpContext.Current.Request
Upvotes: 3
Reputation: 5986
You can access Request.QueryString collection from code-behind of your UserControl.
Upvotes: 2
Reputation: 103348
You could pass the querystring value as a Property of the ascx control like:
<cc:myControl id="myControl" runat="server" myValue='<%=request.querystring("id")' />
Then in your code behind for the custom control, add the following in your class:
Public myValue as String
Upvotes: 0