将数据从 ASP.NET 页面传递到动态加载的 ASCX 用户控件
我正在使用 C# 和 Ajax 开发 ASP.NET 应用程序。
我有一个页面,其中包含动态加载的用户控件。我需要将一些数据(整数值和一些字符串)传递给已动态加载的用户控件。
现在我使用Session来传递这些值,但我想我可以使用另一种方式;像 VIEWSTATE 或隐藏输入之类的东西。
你给我推荐什么?
更新:
动态加载控件这一事实很重要,因为控件会在每次回发时加载,并且我无法在控件上存储任何值。
I'm developing an ASP.NET application with C# and Ajax.
I have a page that holds user controls loaded dynamically. I need to pass some data (integer values and some strings) to the user control that has been loaded dynamically.
Now I use Session to pass these values, but I think I can use another way; something like VIEWSTATE or hidden input.
What do you recommend me?
UPDATE:
The fact that I load the controls dynamically is important because controls are loaded on every postback, and I can't store any value on controls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用要传递给它的数据的数据类型在用户控件上创建一个属性,并在创建控件时将其填充到页面中。
在后面的代码中:
您也可以直接在标记中执行此操作:
Create a property on your user control with the datatype of the data you want to pass to it, and populate it in your page on creation of the control.
In the code behind:
You can also do this directly in markup:
在用户控件中设置公共属性。
然后,当您将用户控件放入 aspx 页面中时:
您还可以更改后面代码中的值:
Setup public properties within your user control.
And then when you put your user control in your aspx page:
You can also change the values within your code behind:
要真正回答你的问题,就像其他人似乎都不希望你这样做一样,我同意......我以前做过这种事情。
我要做的第一件事是让您的页面实现一个接口。
在控件中:
IVansFannelDataProviderPage provider = this.Page as IVansFannelDataProviderPage;
if (provider != null) { //从接口获取数据 } else throw YouCantPutThisControlOnThisKindOfPageException();
这不是最优雅的方法,但是当我们有很多控件想要共享一个非常昂贵的对象时这符合要求。
这种方法效果很好,但它会使您的控件在未实现该接口的页面上无法使用,从而使控件与您的页面耦合过于紧密。其他人都说让页面从控件获取数据是正确的;您将控件放在页面上,而不是控件中的页面。
您应该有一个很好的理由来这样做。对于我们来说:共享对象的加载非常昂贵,并且无论哪个控件正在该对象上工作,页面加载/保存它都非常有用。
遗憾的是,许多没有真正实现该接口的页面必须硬塞以提供某种支持或代理支持,只是为了让控件正常工作,并使页面和控件的可重用性大大降低。
如果我必须再次执行此操作,我会让页面通过事件将数据发送到控件,如果我需要偷懒的话,可能会通过反射。
To actually answer your quesiton, as much as everyone else seems to not want you to do this, and I agree...I have done this sort of thing before.
The first thing I'd do is make your page implement an interface.
In the control:
IVansFannelDataProviderPage provider = this.Page as IVansFannelDataProviderPage;
if (provider != null) { //grab data from interface } else throw YouCantPutThisControlOnThisKindOfPageException();
It's not the most elegant way to do it, but when we had a lot of controls wanting to share a very expensive object this fit the bill.
This works well, but it makes your control unusable on pages that don't implement the interface--and that makes the controls too tightly coupled to your page. Everyone else saying to have the page get data from the controls is correct; you put controls on the page, not pages in controls.
You should have a very good reason in order to do it. For us: the load of the shared object was very expensive, and having the page load/save it no matter what control was working on that object was quite useful.
It was too bad that a lot of pages that didn't really implement the interface had to be shoehorned to provide some sort of support or proxy support just to get the controls to work, and made the pages and controls that much less reusable.
If I had to do this over again, I'd have the page send data to the controls with events, probably through reflection if I needed to be lazy.
您必须在每次回发时重新加载这些控件...请阅读本文。这可能有帮助。
动态 Web 控件、回发、和查看状态
You'll have to re-load those controls on each post-back... Give this a read. It might help.
Dynamic Web Controls, Postbacks, and View State
您可以在 HttpContext.Items 集合中设置值并在控件中读取它们。这与使用 Session 类似,只不过它仅针对每个请求可用,而不是在整个会话生命周期内可用。
https://web. archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx
恕我直言,这有点懒,但在某些情况下可能是一个很好的解决方案。
You could set values in the HttpContext.Items collection and read them in your controls. This is like using Session except that it's only available per request not for the whole session lifetime.
https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx
IMHO this is a bit lazy but it might be a good solution in some situations.
在我看来,这有点违背了使用 ascx 控件的目的,因为这破坏了控件封装。您的页面应该通过订阅控件发布的事件来从控件获取数据。
Kind of defeats the purpose of using an ascx control IMO since this breaks control encapsulation. Your page should be -getting- data from the control through subscribing to events published by the control.