如何从 .aspx 页面检索查询字符串值并将其传递到 ascx 页面
是否可以从 aspx 文件中的 Request.QueryString 检索 ID 值并将其传递到 ascx 文件,以便使用检索到的 ID 成功更新配置文件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,如果某个内容位于 UserControl 中,则可能是因为该控件中的功能足够重要,可以分解到它自己的可重用容器中,以便在另一个页面上重用。如果该控件实际上要在另一个页面上重用,那么它确实不应该引用查询字符串参数,因为该控件不应该假设它所在的页面。如果该控件包含在查询字符串参数名称不同的另一个页面上怎么办?或者也许在另一个页面上该值将来自数据库或 ViewState 或者将以某种方式自动确定?所以我的一般规则是,如果您要创建一个 UserControl,永远永远永远不要对其托管的页面做出任何假设。
因此,就像大多数人所说的那样,您仍然可以从 UserControl 内部访问 Request.QueryString 属性,但这可能不是最好的主意。在由容器页面设置的控件上创建一个属性是一个更好的主意。
我认为最好的想法,也是我几乎总是做的,是在控件上创建名为 LoadData (或类似的东西)的方法,并为您需要的所有查询字符串值提供参数。这样,您就拥有该数据的单个入口点,因此很清楚这些值在什么时候被设置以及它们将被设置为什么。如果您走属性路线,总是会担心是否所有属性都已设置,以及它们是否在页面生命周期的正确位置设置(在回发期间可能会变得棘手)
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)
在您的
aspx
页面、您的ascx
用户控件、您的母版页、您的自定义控件以及几乎在任何地方,您都可以访问查询字符串。使用以下方法之一:Page.Request.QueryString
直接访问用户控件内的查询字符串HttpContext.Current.Request
访问请求(包括查询字符串)In your
aspx
page, in yourascx
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
您可以从 UserControl 的代码隐藏中访问 Request.QueryString 集合。
You can access Request.QueryString collection from code-behind of your UserControl.
您可以将查询字符串值作为 ascx 控件的属性传递,例如:
然后在自定义控件的后面代码中,在类中添加以下内容:
You could pass the querystring value as a Property of the ascx control like:
Then in your code behind for the custom control, add the following in your class: