如何从一个.aspx页面检索查询字符串值,并将其传递给ASCX页页面、字符串值、并将其、aspx

2023-09-05 01:31:40 作者:朩冇魚芄朩冇粗緬

时,可以从一个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?

推荐答案

通常,如果事情是在一个用户控件,这可能是因为在控制的功能足以显著被分解成它自己的可重复使用的容器,可重复使用在另一页。如果控制实际上是以往任何时候都在另一页被重用,这确实不应该引用查询字符串参数,因为控制应该不作任何假设什么网页它是。如果这也就控制被包含的查询字符串参数被命名为不同的另一页?或者在另一页上该值将来自数据库或ViewState的或以某种方式将被自动确定?所以我的一般规则是,如果你打算做一个用户控件,永远,永远永远不会让这件事被托管在该页面的任何假设。

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.

所以像大多数人说,你仍然可以从用户控件内访问的Request.QueryString属性,但是这可能不会是最好的主意。创建上得到由容器页面设置控件的属性是一个更好的主意。

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.

在我什么,我几乎总是这样的意见,最好的办法,就是创建方法中调用LoadData(或类似的东西)的控制,对所有你需要的查询字符串值的参数。这样,你有一个单一入口点的数据,所以很清楚在什么时候这些值越来越设定什么之后就被设置为。如果你去酒店的路线,总有关于是否所有的属性得到了设定的关注,以及他们是否得到了在正确的点,在页面生命周期设置(它可以在回发棘手)

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)