这个问题要由协会解决。 NET guru。 其驱动力是肉子。
I have inherited an ASP.NET Web Forms application. 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. We will call this UserControl root control. This UserControl is defined as follows:
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
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
<uc:Widget ID="myWidget" runat="server" Source="/userControls/widgets/info.ascx" />
page.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = GetData();
myWidget.Records = table;
}
请注意这种信息。 我们想要在此案中装上用户目录。 在这种情况下,这种做法是必要的。 我删除了它有理由关注这一问题的外在法典。 不管怎样,在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. Unfortunately, I can t seem to access the Widget class from my code-behind. Are there some rules about UserControl visibility at compile time that I m not aware of? How do I access the Widget class from the code-behind of info.ascx.cs?
谢谢!