问题
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?
回答1:
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)
回答2:
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:
- Access directly to query string right inside your user control via
Page.Request.QueryString
- Create a property in your user control, then in your page, cast your user control to get a reference to that property and set that property. Then in your user control, use that property.
- Anywhere inside ASP.NET environment, get access to request (including query string) via
HttpContext.Current.Request
回答3:
You can access Request.QueryString collection from code-behind of your UserControl.
回答4:
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
来源:https://stackoverflow.com/questions/6621703/how-to-retrieve-a-querystring-value-from-an-aspx-page-and-pass-it-to-ascx-page