Items \ Contrls not recognized after created dynamically

Jul 9, 2010 at 8:56 AM
Edited Jul 10, 2010 at 4:26 PM
When adding controls dynamically, the events that are added dynamically aren't being raised. ExtAspNet.DropDownList catType = new ExtAspNet.DropDownList(); catType.DataSource = Application[LanguageConstants.NameType.UserCategoryPreferenceSelection.ToString()]; catType.DataTextField = "Value"; catType.DataValueField = "ID"; catType.DataBind(); catType.ID = "ddlCatType" + counter.ToString(); catType.Height = 20; catType.AutoPostBack = true; catType.CssClass = "CategoryLinks"; catType.Width = 181; catType.SelectedIndexChanged += new EventHandler(catType_SelectedIndexChanged); parent.Items.Add(CreatePanel(25, catType)); When trying to solve this issue, I stumbled upon another issue, that the "Controls" of the panel and the "Items" of the panel are not initialized upon post back action. I assume that is the same problem. Meaning that even if I use the Request.Params["__EVENTTARGET"] to find the fired control name, I cannot find the control using FindControl, cause it ends up being null and not found. I will really appreciate anyones help here, since it is stopping my project from continuing with these wonderful looking controls. Ray.
Coordinator
Jul 9, 2010 at 2:33 PM
Lucky, there is an example explaining how to add dropdownlist dynamically: http://extasp.net/#/form/form_dynamic.aspx
Jul 11, 2010 at 1:56 PM
I have seen it and attempted to use it. I am using a form that creates dynamic user controls, which each one creates several drop down lists dynamically. For some reason it doesn't work in this way. BTW - I am very fond of your work. The controls look amazing, and I am planning to go deeply into them.
Coordinator
Jul 14, 2010 at 5:43 AM
BTW: The code of dynamic created controls should be placed before PageLoad event, better inside Page_Init. Could you share some code, as I haven't reproduced your issue.
Jul 14, 2010 at 12:14 PM
Edited Jul 14, 2010 at 12:16 PM
OK, I have made a working example: I have a page (TestEmbeddedControl.aspx) and a user control (EmbeddedControl.ascx) The user control creates the object dynamically. The page calls a function in the user control, which searches for the dynamically added object. Here is their code (call the page to activate, and place them in the same directory) - if you want, I can send it to you directly, cause I can't see an option to add files here, and these messages tend to change the format of some html characters. User control:: Markup: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EmbeddedControl.ascx.cs" Inherits="ServiceExchangeApp.Test.EmbeddedControl" %> <ext:PageManager ID="PageManager1" runat="server" /> <ext:Panel ID="frmMain" runat="server" BodyPadding="5" AutoScroll="true" AutoWidth="true" > <Items> </Items> </ext:Panel> Code behind: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ServiceExchangeApp.Test { public partial class EmbeddedControl : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { } protected void Page_Init(object sender, EventArgs e) { ExtAspNet.CheckBox chk = new ExtAspNet.CheckBox(); chk.ID = "chk1"; chk.Checked = false; frmMain.Items.Add(chk); } internal void Save() { // Accessing the chk - this call fails with null reference bool toSave = (FindControl("chk1") as ExtAspNet.CheckBox).Checked; } } } Page::: Markup: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestEmbeddedControl.aspx.cs" Inherits="ServiceExchangeApp.Test.TestEmbeddedControl" %> <%@ Register Src="EmbeddedControl.ascx" TagName="EmbeddedControl" TagPrefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server">
<uc1:EmbeddedControl ID="embeddedCtl1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
</form> </body> </html> Code behind: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ServiceExchangeApp.Test { public partial class TestEmbeddedControl : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { embeddedCtl1.Save(); } } }
Coordinator
Jul 15, 2010 at 2:46 PM

Would you mind to use the "Insert Code Snippet" function to include the code fragment?

Jul 15, 2010 at 3:30 PM
Edited Jul 15, 2010 at 3:36 PM
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EmbeddedControl.ascx.cs" Inherits="ServiceExchangeApp.Test.EmbeddedControl" %>
<ext:PageManager ID="PageManager1" runat="server" />
<ext:Panel ID="frmMain" runat="server" BodyPadding="5" AutoScroll="true" AutoWidth="true" >
<Items>

</Items>
</ext:Panel>
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI;
 using System.Web.UI.WebControls; 
 namespace ServiceExchangeApp.Test 
{     
	public partial class EmbeddedControl : System.Web.UI.UserControl     
	{         
		protected void Page_Load(object sender, EventArgs e)         {          }          
		protected void Page_Init(object sender, EventArgs e)         
		{             
			ExtAspNet.CheckBox chk = new ExtAspNet.CheckBox();             
			chk.ID = "chk1";             
			chk.Checked = false;             
			frmMain.Items.Add(chk);         
		}          

		internal void Save()         
		{            
			 // Accessing the chk - this call fails with null reference             
			bool toSave = (FindControl("chk1") as ExtAspNet.CheckBox).Checked;         
		}     
	
}


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestEmbeddedControl.aspx.cs" Inherits="ServiceExchangeApp.Test.TestEmbeddedControl" %>
<%@ Register Src="EmbeddedControl.ascx" TagName="EmbeddedControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <uc1:EmbeddedControl ID="embeddedCtl1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServiceExchangeApp.Test
{
    public partial class TestEmbeddedControl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            embeddedCtl1.Save();
        }
    }
}