将 DataContext 传递给 UserControl

发布于 2024-10-27 11:10:17 字数 898 浏览 4 评论 0原文

我有一个包含数据集的页面:

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>

我该如何执行此操作?我会以某种方式在 UserControlPage_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Hello爱情风 2024-11-03 11:10:17

您不需要将其传递给您的UserControlDataContext 对象的创建成本很低,因此如果您需要访问该对象,只需实例化一个新对象即可。

但是,要直接回答您的问题,您可以通过将其存储在 HttpContext.Items 中,在所有用户控件之间共享 HttpContext 范围的 DataContext,例如

  HttpContext.Items["DataContextKey"] = new MyDataContext();

然后,您可以在构造函数中初始化一个字段,就像通过控件一样

  _dataContext = (MyDataContext)Context.Items["DataContextKey"];

。用于管理 DataContext 生命周期的此方法(和其他方法)的更优雅的实现通常可在 http://www.west-wind.com/weblog/posts/246222.aspx ,在 DataContextFactory 类中。

您最感兴趣的方法是:

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;
}

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-scoped DataContext among all user controls by storing it in HttpContext.Items such as

  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.

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;
}
眼睛会笑 2024-11-03 11:10:17

您可以将公共属性放在用户控件上并从父页面进行设置。

You can probably put a public property on your user control and set that from your parent page.

Spring初心 2024-11-03 11:10:17

定义一个接口:

public interface IDataContextPage
{
     MyDefinedDataContext DataContext { get; }
}

让页面实现该接口,并执行以下操作:

public class MyPage: Page, IDataContextPage
{
     public MyDefinedDataContext DataContext { get { return _context; } }
}

您的用户控件可以通过该接口引用该页面。在 UC 中执行此操作:

protected void Page_Load(..)
{
    var uc = ((IDataContextPage)this.Page).DataContext;
}

我建议在页面的 init 事件中创建上下文,而不是加载。

HTH。

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:

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.

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文