Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LoadControl .NET 2.0 1

Status
Not open for further replies.

RobHudson

Programmer
Joined
Apr 30, 2001
Messages
172
Location
GB
My problem - really annoying!!!:
UserControls. In 1.x you could create a usercontrol and give it properties. Nothing too flash yet! You could also do something like:

Code:
MyControl uc = (MyControl)this.LoadControl("VIRTUAL_PATH_OR_NAMESPACE");
uc.MyProperty = "Hello, I'm a dick :)";
MySpan.Controls.Add(uc);

Nifty! And really useful :D
Here comes .NET 2, all singing and all dancing - yeehah!
Can't seem to do that anymore.

The only way you can reference the UserControl is by having it in the App_Code folder. Then it can be called from somewhere else and have it's properties set. Well, sort of. When you compile you get an error saying can't use App_Code for this action. Something to do with it compiling code in App_Code differently (not that I care to be honest...).

Anyway....
Found this article discussing it: And then came across this as well:
So, I created an Interface:

Code:
namespace x.x
{

    public interface IUserLogin
    {
        string setMe
            {
            get;
            set;
        }
    }

}

Created my usercontrol:

Code:
public partial class _UserLogin : System.Web.UI.UserControl, x.x.IUserLogin
{
    protected void Page_Load.....
    {
    }

    public void IUserLogin.setMe()
    {
    }
}

So now when I compile I get: The modifier 'public' is not valid for this item. Relating to the UserControl.

I know nothing about interfaces - any ideas? Any experience on having done this already...

Please tell me there's a dead easy way to do this, cos at the moment I'm thinking turning back to VS2003/1.1!
 
Sorted!

Need to add this to the calling page:

Code:
<%@ Reference Control="~/WebControls/WebUserControl.ascx" %>

Found the answer here:
What I failed to mention was that the control was in a sub folder. Worked when it was in the root, but not when in a sub folder (after much playing about!)

Now the good old 1.1 way works :)

Code:
WebUserControl uc = (WebUserControl)this.LoadControl("~/WebControls/WebUserControl.ascx");
uc.Text = "Hello";
this.Controls.Add(uc);

Hope this helps someone :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top