C# 将数据从 ASP.NET 页面传递到动态加载的 ASCX 用户控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2201312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass data from a ASP.NET page to ASCX user controls loaded dynamically
提问by VansFannel
I'm developing an ASP.NET application with C# and Ajax.
我正在使用 C# 和 Ajax 开发 ASP.NET 应用程序。
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.
现在我使用Session来传递这些值,但我想我可以用另一种方式;诸如 VIEWSTATE 或隐藏输入之类的东西。
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.
我动态加载控件这一事实很重要,因为控件是在每次回发时加载的,而且我无法在控件上存储任何值。
采纳答案by Oded
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.
使用您要传递给它的数据的数据类型在您的用户控件上创建一个属性,并在创建控件时将其填充到您的页面中。
public class myControl : Control
{
...
public int myIntValue {get; set;}
...
}
In the code behind:
在后面的代码中:
myControl ctrl = new myControl();
ctrl.myIntValue = 5;
You can also do this directly in markup:
您也可以直接在标记中执行此操作:
<uc1:myControl ID="uc1" runat="server" myIntValue="5" />
回答by Hyman Marchetti
Setup public properties within your user control.
在您的用户控件中设置公共属性。
public string TestValue { get;set;};
And then when you put your user control in your aspx page:
然后当您将用户控件放入 aspx 页面时:
<uc1:UserControl ID="uc1" runat="server" TestValue="Testing" />
You can also change the values within your code behind:
您还可以更改后面代码中的值:
uc1.testValue = "some value";
回答by Pierreten
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.
有点违背使用 ascx 控制 IMO 的目的,因为这破坏了控制封装。您的页面应该通过订阅控件发布的事件从控件获取数据。
回答by Broam
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();
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.
如果我必须重新这样做,我会让页面通过事件将数据发送到控件,如果我需要偷懒,可能会通过反射。
回答by OrionRobillard
You couldset 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.
您可以在 HttpContext.Items 集合中设置值并在您的控件中读取它们。这就像使用 Session 不同之处在于它仅在每个请求中可用,而不是在整个会话生命周期内可用。
http://www.4guysfromrolla.com/articles/060904-1.aspx
http://www.4guysfromrolla.com/articles/060904-1.aspx
IMHO this is a bit lazy but it might be a good solution in some situations.
恕我直言,这有点懒惰,但在某些情况下它可能是一个很好的解决方案。
回答by Billy Coover
You'll have to re-load those controls on each post-back... Give this a read. It might help.
您必须在每次回发时重新加载这些控件...阅读一下。它可能会有所帮助。