开发者

Newbie .net question - Can't find control?

I have a file called MasterPages/Wrapper.master.cs that has the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class vehicles_2011_experience_MasterPages_Wrapper_news : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
                Header_global.Controls.Add(Page.LoadControl("~/en/Controls/Header_global.ascx"));
    }
}

When I try to build the sol开发者_高级运维ution, I get he error message:

The name "Header_global" does not exist in current context. The file ~/en/Controls/Header_global.ascx DOES EXIST.

So how do I "configure" Visual Studios to successfully build this solution?


What exactly are you trying to do? From this line of code:

Header_global.Controls.Add(Page.LoadControl("~/en/Controls/Header_global.ascx"));

It looks like you're trying to add a control to itself. I doubt that's the intent.

The error is basically telling you that there's no declared instance of the glass Header_global in the context of that Page_Load method. Does an instance of this header control exist on the master page? Or are you trying to dynamically create one?

If you're trying to dynamically create one then you need to add it to the page, not to itself. Generally a good way to do this is to have a PlaceHolder control somewhere on the page and to add it to that. So, after creating the PlaceHolder control, the line of code would be something like:

PlaceHolder1.Controls.Add(Page.LoadControl("~/en/Controls/Header_global.ascx"));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜