Hi,
I need to load multiple user controls from code behind in a div that is also created from the code behind..
The problem is when I place the UserControl with a stringbuilder on the page, the UserControl is not loaded..
So to work arround this I've found a script, that wil create the user control..problem with this script is that it only create`s the last UserControl because the script make`s a new page everytime a UserControl needs to be loaded..Does anybody knows how to work arround this?
Here's some code:
[WebMethod] public static string LoadUserControl(string sUserControl) { try { using (Page page = new Page()) { UserControl userControl = (UserControl)page.LoadControl(sUserControl); page.Controls.Add(userControl); using (StringWriter writer = new StringWriter()) { HttpContext.Current.Server.Execute(page, writer, false); return writer.ToString(); } } } catch (Exception e) { string sR = e.ToString(); return sR; } }
If no body knows to workarround this, I've another tought.
I create the div also from the code behind...In the onload complete event I want to load the div with a new User Control:
Someting like this:
public class cControls { public string sDiv { get; set; } public string sUserControlID { get; set; } public string sUserControlPath { get; set; } } public List<cControls> lControls = new List<cControls>(); protected void Page_Load(object sender, EventArgs e) { //Load controls onto the page //I use a stringbuilder for Javascript and HTML and add the sbHTML to a <asp:Literal ID="lContent" runat="server"></asp:Literal> ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "temp", sbJavaScript.ToString(), false); lContent.Text = sbHTML.ToString(); } protected override void OnLoadComplete(EventArgs e) { foreach (var sItem in lControls) { string sTemp = sItem.sUserControlID.ToString(); UserControl uc1 = (UserControl)this.Page.FindControl(sTemp); UserControl uc = (UserControl)this.Page.LoadControl(sItem.sUserControlPath.ToString()); this.Page.FindControl(sItem.sDiv).Controls.Add(uc1); //Tried two different tests. this.Page.FindControl(sItem.sDiv).Controls.Add(uc); } }
The problem is that the controls are notyet loaded when the OnLoadComplete method is executed..
Can some one help me that I can Load my UserControls in the page after login?
Best regards,
Mark