How to get querystring value from .aspx page and transfer it to ascx page

Is it possible to get the identification value from Request.QueryString from the aspx file and transfer it to the ascx file in order to successfully update the profile using the received identifier?

+4
source share
4 answers

Often, if something is in the UserControl, it is either because the functionality in the control is significant enough to be split into its own reusable container, which could be reused on another page. If this control will actually be reused on another page, it really should not refer to the query string parameters, because the control should not make any assumptions about which page it is on. What if this control is included on another page whose query string parameters are named differently? Or maybe on another page this value will come from a database or ViewState or will it be automatically determined in some way? So my general rule is that if you are going to create a UserControl, never, never make any assumptions about which page it is placed on.

Like most people, you can still access the Request.QueryString property inside the UserControl, but this is probably not a good idea. Creating a property in a control that is set on a container page is a much better idea.

The best idea, in my opinion, and what I almost always do is to create a LoadData control method (or something like that) in a control with parameters for all the required query string values. Thus, you have one entry point for this data, so itโ€™s clear at what point these values โ€‹โ€‹are set and what they get. If you go along the property path, there is always a problem if all the properties were set and whether they were set at the right point in the page life cycle (there may be difficulty during postback)

+6
source

On an aspx page in an ascx user element on the main page in a custom control and almost everywhere you can access a query string. Use one of the following methods:

  • Access directly to the query string directly inside the user control via Page.Request.QueryString
  • Create a property in your user control, then run your user control on your page to get a link to this property and set this property. Then use this property in your custom element.
  • Anywhere inside the ASP.NET environment, access the request (including the query string) through HttpContext.Current.Request
+3
source

You can access the Request.QueryString collection from the code of your UserControl.

+2
source

You can pass the value of querystring as a property of an ascx control, for example:

 <cc:myControl id="myControl" runat="server" myValue='<%=request.querystring("id")' /> 

Then, in your code for the custom control, add the following to your class:

 Public myValue as String 
0
source

All Articles