简体   繁体   English

在ASP.NET中访问代码隐藏中的用户控件

[英]Accessing usercontrols in code-behind in ASP.NET

This question is for an ASP.NET guru. 这个问题适用于ASP.NET大师。 Its driving me nuts. 它让我疯狂。

I have inherited an ASP.NET Web Forms application. 我继承了一个ASP.NET Web窗体应用程序。 This application uses a complex structure of nested user controls. 此应用程序使用嵌套用户控件的复杂结构。 While complex, it does seem necessary in this case. 虽然很复杂,但在这种情况下确实有必要。 Regardless, I have a page that uses a single UserControl. 无论如何,我有一个使用单个UserControl的页面。 We will call this UserControl root control. 我们将调用此UserControl根控件。 This UserControl is defined as follows: 此UserControl定义如下:

widget.ascx widget.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="resources_userControls_widget" %>
<div>
  <asp:Panel ID="bodyPanel" runat="server" />
</div>

widget.ascx.cs widget.ascx.cs

public partial class resources_userControls_widget : System.Web.UI.UserControl
{
    private string source = string.Empty;
    public string Source
    {
        get { return source; }
        set { source = value; }
    }

    private string parameter1 = string.Empty;
    public string Parameter1
    {
        get { return parameter1; }
        set { parameter1 = value; }
    }

    private DataTable records = new DataTable();
    public DataTable Records
    {
        get { return records; }
        set { records = value; }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        UserControl userControl = LoadControl(source) as UserControl;
        if (parameter1.Length > 0)
            userControl.Attributes.Add("parameter1", parameter1);
        bodyPanel.Controls.Add(userControl);
    }

    private void InsertUserControl(string filename)
    {
    }
}

In my application, I am using widget.ascx in the following way: page.aspx 在我的应用程序中,我使用widget.ascx,方法如下: page.aspx

<uc:Widget ID="myWidget" runat="server"  Source="/userControls/widgets/info.ascx" />

page.aspx.cs page.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
  DataTable table = GetData();
  myWidget.Records = table;
}

Please notice how info.ascx is set as the UserControl we want to load in this case. 请注意在这种情况下如何将info.ascx设置为我们要加载的UserControl。 This approach is necessary in this case. 在这种情况下,此方法是必需的。 I've removed the extraneous code that justifies it to focus on the problem. 我删除了无关的代码,证明它可以专注于问题。 Regardless, in info.ascx.cs I have the following: 无论如何,在info.ascx.cs中我有以下内容:

info.ascx.cs info.ascx.cs

protected void Page_Load(object sender, EventArgs e)
{
  // Here's the problem
  // this.Parent.Parent is a widget.ascx instance.
  // However, I cannot access the Widget class. I want to be able to do this
  // Widget widget = (Widget)(this.Parent.Parent);
  // DataTable table = widget.Records;
}

I really need to get the value of the "Records" property from the Parent user control. 我真的需要从Parent用户控件获取“Records”属性的值。 Unfortunately, I can't seem to access the Widget class from my code-behind. 不幸的是,我似乎无法从我的代码隐藏中访问Widget类。 Are there some rules about UserControl visibility at compile time that I'm not aware of? 在编译时是否有一些关于UserControl可见性的规则,我不知道? How do I access the Widget class from the code-behind of info.ascx.cs? 如何从info.ascx.cs的代码背后访问Widget类?

Thank you! 谢谢!

Firstly you need to create an interface and implement it to the Widget user control class. 首先,您需要创建一个接口并将其实现到Widget用户控件类。

For instance, 例如,

public interface IRecord
{
    DataTable Records {get;set;}
} 

public partial class resources_userControls_widget : System.Web.UI.UserControl, IRecord
{
 ...
}

And in code behind of Info.ascx.cs, 在Info.ascx.cs背后的代码中,

protected void Page_Load(object sender, EventArgs e)
{
  // Here's the problem
  // this.Parent.Parent is a widget.ascx instance.
  // However, I cannot access the Widget class. I want to be able to do this
  // Widget widget = (Widget)(this.Parent.Parent);
  // DataTable table = widget.Records;

  IRecord record=this.Parent.Parent;
  DataTable table = widget.Records;
}

In your case, maybe better to use some server object's like ViewState or Session. 就您而言,最好使用某些服务器对象,例如ViewState或Session。 Fill it within DataTable on your page and get it in Page_load event handler on info.ascx user control. 将其填充到页面上的DataTable中,并在info.ascx用户控件上的Page_load事件处理程序中获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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