简体   繁体   中英

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?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM