Always create your dynamic control in Page_Init event (
Read this[
^] and
this[
^] for a detailed information).
Change your CS. Try this:
protected void Page_Init(object sender, EventArgs e)
{
DropDownList ddlAgent = new DropDownList();
ddlAgent.EnableViewState = false;
ddlAgent.CssClass = "RegsSelect";
ddlAgent.ID = "ddlAgent";
ddlAgent.ClientIDMode = ClientIDMode.Static;
ddlAgent.Attributes.Add("style", "margin-left:-4px;height:40px;width:328px;");
upText.ContentTemplateContainer.Controls.Add(ddlAgent);
}
Now, find the control and bind it in your function or method. Try this:
DropDownList ddlAgent=(DropDownList) upText.ContentTemplateContainer.FindControl("ddlAgent");
CUser objUser = new CUser();
ddlAgent.DataSource = objUser.GetUserByRoleId(4);
ddlAgent.DataValueField = "Id";
ddlAgent.DataTextField = "FirstName";
ddlAgent.DataBind();
--Amit