将 DataContext 传递给 UserControl
我有一个包含数据集的页面:
protected void Page_Load(object sender, EventArgs e)
{
MyDefinedDataContext mydatacont = new MyDefinedDataContext();
}
我想将此数据上下文传递给 UserControl
:
<%@ Page Language="C#" MasterPageFile="~/Site2.Master" Inherits="WebApplication3._Default" %>
<%@ Register src="inc/Tabular.ascx" tagname="Tabular" tagprefix="testing" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<testing:Tabular ID="tabularArticles" runat="server" currentPage="3" title="Articles"/>
</asp:Content>
我该如何执行此操作?我会以某种方式在 UserControl
的 Page_Load
中执行此操作吗?
protected void Page_Load(object sender, EventArgs e)
{
myCtx = mydatacont;
this.setPreInfo();
this.getTableInfo();
this.setTableMeta();
}
I have a page that contains a dataset:
protected void Page_Load(object sender, EventArgs e)
{
MyDefinedDataContext mydatacont = new MyDefinedDataContext();
}
I'd like to pass this datacontext through to a UserControl
:
<%@ Page Language="C#" MasterPageFile="~/Site2.Master" Inherits="WebApplication3._Default" %>
<%@ Register src="inc/Tabular.ascx" tagname="Tabular" tagprefix="testing" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<testing:Tabular ID="tabularArticles" runat="server" currentPage="3" title="Articles"/>
</asp:Content>
How can I do this? Would I do it in the UserControl
's Page_Load
somehow?
protected void Page_Load(object sender, EventArgs e)
{
myCtx = mydatacont;
this.setPreInfo();
this.getTableInfo();
this.setTableMeta();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要将其传递给您的
UserControl
。DataContext
对象的创建成本很低,因此如果您需要访问该对象,只需实例化一个新对象即可。但是,要直接回答您的问题,您可以通过将其存储在
HttpContext.Items
中,在所有用户控件之间共享HttpContext
范围的DataContext
,例如然后,您可以在构造函数中初始化一个字段,就像通过控件一样
。用于管理
DataContext
生命周期的此方法(和其他方法)的更优雅的实现通常可在 http://www.west-wind.com/weblog/posts/246222.aspx ,在DataContextFactory
类中。您最感兴趣的方法是:
You don't need to pass it to your
UserControl
.DataContext
objects are inexpensive to create, so if you need access to the object there you can just instantiate a new one.However, to answer your question directly, you can share an
HttpContext
-scopedDataContext
among all user controls by storing it inHttpContext.Items
such asThen, you can initialize a field in your constructor like from your controls via
A more elegant implementation of this approach (and other approaches) for managing the
DataContext
life cycle in general is available at http://www.west-wind.com/weblog/posts/246222.aspx , in aDataContextFactory
class.The method that will be most interesting to you is:
您可以将公共属性放在用户控件上并从父页面进行设置。
You can probably put a public property on your user control and set that from your parent page.
定义一个接口:
让页面实现该接口,并执行以下操作:
您的用户控件可以通过该接口引用该页面。在 UC 中执行此操作:
我建议在页面的 init 事件中创建上下文,而不是加载。
HTH。
Define an interface:
Have the page implement this interface, and do:
Your user control can refer to this page through the interface. Do this in the UC:
I would recommend creating the context in the init event in the page, not the load.
HTH.