am trying to load usercontrol in c#.
Can add the .ascx onto my .aspx page by using the code below:
Control MyUserControl;
MyUserControl = LoadControl("~/controls/Editor.ascx");
PlaceHolder1.Controls.Add(MyUserControl);
However, I want to pass ID into Editor.ascx, top of the Editor.ascx contains the following code:
private int m_id = 0;
public int ID
{
get { return m_id; }
set { m_id = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack && !Page.IsCallback)
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
TB_Editor.Text = db.DT_Control_Editors.Single(x => x.PageControlID == ID).Text.Trim();
}
}
}
I tried casting control to user control so I can get access to ID see below
UserControl Edit = (UserControl)MyUserControl;
But I get a cast error.
any ideas?
I think your problem is your casting when you load the control. You should cast to the most specific type (in this case,
Editor
), pass the parameters you need, and then add the control to the placeholder.Try this: