English 中文(简体)
在伙伴关系中使用密码的用户控制。 NET
原标题:Accessing usercontrols in code-behind in ASP.NET

这个问题要由协会解决。 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?

谢谢!

最佳回答

首先,你们需要建立一个接口,并将其落实到Widget用户控制阶层。

For instance,

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

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

以及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;
}
问题回答

就你而言,或许可以更好地利用一些服务器物体,如观察国或会议。 将其放在可上网查阅的网页上,并放在网页上。 ccx用户控制。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签