开发者

Passing DataContext to a User Control

开发者 https://www.devze.com 2023-02-20 11:55 出处:网络
I have a page that contains a dataset: protected void Page_Load(object sender, EventArgs e) { MyDefinedDataContext mydatacont = new MyDefinedDataContext();

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 开发者_如何学Pythondatacontext 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;
}


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号