I’m dynamically loading a user control on to a page. I’m usually a vb.net person and can normally get by but this has me stumped. I’m trying to pass a variable from the page to the control before I load it.
Heres is where I’m calling the control:
Control webUserControl = (Control)Page.LoadControl("~/controls/carousel-guards.ascx");
phGuardsList.Controls.Add(webUserControl);
I’ve put the following property on the carousel-guards.ascx:
public String PostCode
{
get
{
return this.PostCode;
}
set
{
this.PostCode = value;
}
}
But I don’t seem to have a webUserControl.PostCode available to me.
Any help will be really appreciated
EDIT – Of course, I have to reference the control. Silly me! However it’s not letting me call it with carousel_guards: Error 96 The type or namespace name 'carousel_guards' could not be found (are you missing a using directive or an assembly reference?) C:\Development\Guards247\g247 Test\FindGuard.aspx.cs 197 13 g247 Test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace g247_Test.controls
{
public partial class carousel_guards : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public String PostCode
{
get
{
return this.PostCode;
}
set
{
this.PostCode = value;
}
}
}
}
You can’t access the property from your control because you are casting your loaded control to
Control. You should cast it to your control type. If your Control class name isCarouselGuardsthen you can do:and then you can access the property.