[英]Passing DataContext to a User Control
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 user control: 我想将此数据上下文传递给用户控件:
<%@ 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 user control's Page_Load somehow?
我会以某种方式在用户控件的Page_Load中执行此操作吗?
protected void Page_Load(object sender, EventArgs e)
{
myCtx = mydatacont;
this.setPreInfo();
this.getTableInfo();
this.setTableMeta();
}
You don't need to pass it to your UserControl
. 您无需将其传递给
UserControl
。 DataContext
objects are inexpensive to create, so if you need access to the object there you can just instantiate a new one. DataContext
对象的创建成本不高,因此,如果需要访问该对象,则可以实例化一个新对象。
However, to answer your question directly, you can share an HttpContext
-scoped DataContext
among all user controls by storing it in HttpContext.Items
such as 但是,要直接回答您的问题,可以通过将其存储在
HttpContext.Items
,在所有用户控件之间共享HttpContext
范围的DataContext
。
HttpContext.Items["DataContextKey"] = new MyDataContext();
Then, you can initialize a field in your constructor like from your controls via 然后,您可以通过以下方式在构造函数中初始化字段:
_dataContext = (MyDataContext)Context.Items["DataContextKey"];
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 a DataContextFactory
class. 通常,可以在http://www.west-wind.com/weblog/posts/246222.aspx的
DataContextFactory
类中找到用于管理DataContext
生命周期的此方法(和其他方法)的更优雅的实现。
The method that will be most interesting to you is: 您最感兴趣的方法是:
static object GetWebRequestScopedDataContextInternal(Type type, string key, string connectionString)
{
object context;
if (HttpContext.Current == null)
{
if (connectionString == null)
context = Activator.CreateInstance(type);
else
context = Activator.CreateInstance(type, connectionString);
return context;
}
// *** Create a unique Key for the Web Request/Context
if (key == null)
key = "__WRSCDC_" + HttpContext.Current.GetHashCode().ToString("x") + Thread.CurrentContext.ContextID.ToString();
context = HttpContext.Current.Items[key];
if (context == null)
{
if (connectionString == null)
context = Activator.CreateInstance(type);
else
context = Activator.CreateInstance(type, connectionString);
if (context != null)
HttpContext.Current.Items[key] = context;
}
return context;
}
您可能可以将公共属性放在用户控件上,然后从父页面进行设置。
Define an interface: 定义一个接口:
public interface IDataContextPage
{
MyDefinedDataContext DataContext { get; }
}
Have the page implement this interface, and do: 让页面实现此接口,然后执行以下操作:
public class MyPage: Page, IDataContextPage
{
public MyDefinedDataContext DataContext { get { return _context; } }
}
Your user control can refer to this page through the interface. 您的用户控件可以通过界面引用此页面。 Do this in the UC:
在UC中执行此操作:
protected void Page_Load(..)
{
var uc = ((IDataContextPage)this.Page).DataContext;
}
I would recommend creating the context in the init event in the page, not the load. 我建议在页面的init事件中创建上下文,而不是在加载中创建上下文。
HTH. HTH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.