I’m dynamically loading a control and passing through text to the control. But i’m getting an unhandeled exception on when i’m setting the public property.
My control is:
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 pcode
{
get
{
return pcode;
}
set
{
pcode = value;
}
}
}
}
Loading the control on the previous page with:
carousel_guards webUserControl = (carousel_guards)Page.LoadControl("~/controls/carousel-guards.ascx");
webUserControl.pcode = "rg402eg";
phGuardsList.Controls.Add(webUserControl);
The error goes on the set { saying just unhandled exception
Your property is referencing itself. You may change it to:
Or define a private string field and use that:
Also its better if you start the property name with Upper case, (Use Pascal case)