Dynamic UserControl + Direct Events

  1. #1

    Dynamic UserControl + Direct Events

    Hello
    After checking couple of related post, I thought following scenario should work. But after clicking the button inside the usercontrol, I sill get
    "The control with ID 'button-1030' not found"

    aspx:
    <%@ Page Language="C#" %>
    
    <%@ Import Namespace="System.Collections.Generic"%>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Button1_Click(object sender, DirectEventArgs e)
        {
    		if (string.IsNullOrEmpty(hid.Value as string))
    		{
    			hid.Value = "1";
    		}
    		else
    		{
    			hid.Value = (int.Parse(hid.Value as string) + 1).ToString();
    		}
    		
    		UserControl uc1 = (UserControl)this.LoadControl("~/WindowsApp/UserControls/TextControll.ascx");
    		uc1.ID = "UC" + hid.Value;
    		
    		var pnl = new Ext.Net.Panel()
    		            	{
    		            		ID = "dynamicPanel"+hid.Value,
    							Title = "Tab"+hid.Value
    							
    		            	};
    		pnl.ContentControls.Add(uc1);
    
    		pnl.Render("App.tabPanel", RenderMode.AddTo);
    		
    		
    		
        }
    
    	protected override void OnInit(EventArgs e)
    	{
    		var countStr = !IsPostBack ? "" : Request[hid.UniqueID];
    		if (countStr != null && countStr != "")
    		{
    			int count = int.Parse(countStr);
    			for (int i = 0; i < count; i++)
    			{
    
    				UserControl uc1 = (UserControl)this.LoadControl("~/WindowsApp/UserControls/TextControll.ascx");
    				uc1.ID = "UC" + hid.Value;
    
    				var pnl = new Ext.Net.Panel()
    				{
    					ID = "dynamicPanel" + hid.Value,
    
    				};
    				pnl.ContentControls.Add(uc1);
    			}
    		}
    		
     	base.OnInit(e);
    	}
       
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head id="Head1" runat="server">
        <title>Update Controls and Content during a DirectEvent - Ext.NET Examples</title>
        <link href="/resources/css/examples.css" rel="stylesheet" type="text/css" />
        
    </head>
    <body>
        <form id="Form1" runat="server">
        	<ext:Hidden runat="server" ID="hid" ></ext:Hidden>
            <ext:ResourceManager ID="ResourceManager1" runat="server" />
            
            <h1>Update Controls and Content during a DirectEvent</h1>
            
            <h3>Load UserControls</h3>
            
            <ext:TabPanel 
                ID="tabPanel" 
                runat="server" 
                Title="Tab with dynamic controls" 
                Width="500" 
                Height="200"
                BodyPadding="5">
                <TopBar>
                    <ext:Toolbar ID="Toolbar1" runat="server">
                        <Items>
                            <ext:Button 
                                ID="Button1" 
                                runat="server" 
                                Text="Add Tab"
                                OnDirectClick="Button1_Click" 
                                />
                           
                        </Items>
                    </ext:Toolbar>
                </TopBar>
            </ext:TabPanel>
        </form>
    </body>
    </html>
    ascx:
    <script runat="server">
    	public void btn1_click(object sender, DirectEventArgs e)
    	{
    		txt.Text = DateTime.Now.ToString();
    	}
    </script>
    <ext:TextField runat="server" ID="txt" />
    <ext:Button runat="server" Text="Click me" OnDirectClick="btn1_click"/>
  2. #2
    Id of your button is not rendered (i guess that you use non default IDMode)
    Please set explicit ID for the button
  3. #3

    No Success so far

    Thanks for reply

    I tried various combinations, like

    <ext:Button runat="server" Text="Click me" OnDirectClick="btn1_click" ID="btn1" IDMode="Static"/>

    or

    <ext:Button runat="server" Text="Click me" OnDirectClick="btn1_click" ID="btn1" />
    but still getting exception:

    The control with ID 'btn1' not found
    or
    The control with ID 'UC1_btn1' not found


    I'm on Ext.net 2 RC2

    Zdenek
  4. #4
    Your application has two problems (except the issue from my first answer: you did not define id for the button)

    1. Hidden field is not initialized by submitted value in OnInit (ASP.NET calls LoadPostData after page init). You can call EnsureLoadPostData methof for hidden field manually

    2. You recreate panel but did not add to Controls collection

    Here is updated OnInit
    protected override void OnInit(EventArgs e)
        {
            var countStr = !IsPostBack ? "" : Request[hid.UniqueID];
            if (countStr != null && countStr != "")
            {
                hid.EnsureLoadPostData();
                int count = int.Parse(countStr);
                for (int i = 0; i < count; i++)
                {
     
                    UserControl uc1 = (UserControl)this.LoadControl("Child.ascx");
                    uc1.ID = "UC" + hid.Value;
     
                    var pnl = new Ext.Net.Panel()
                    {
                        ID = "dynamicPanel" + hid.Value,
     
                    };
                    pnl.ContentControls.Add(uc1);
                    this.Controls.Add(pnl);
                }
            }
             
            base.OnInit(e);
        }
  5. #5

    Perfect :-)

    I see, silly mistake, thanks a lot

    Zdenek

Similar Threads

  1. [CLOSED] Access Direct Method in Dynamic Usercontrol
    By SymSure in forum 1.x Legacy Premium Help
    Replies: 9
    Last Post: Jun 11, 2013, 11:35 AM
  2. Direct Events with Razor
    By gdog_5021 in forum 2.x Help
    Replies: 3
    Last Post: Aug 15, 2012, 10:46 AM
  3. Replies: 4
    Last Post: May 03, 2012, 8:11 PM
  4. Direct Events dynamic Url
    By Tallmaris in forum 1.x Help
    Replies: 1
    Last Post: Aug 18, 2011, 1:26 PM
  5. Calendar - EventEditForm - Direct Events
    By vwagoner in forum 1.x Help
    Replies: 4
    Last Post: Jun 17, 2011, 5:27 PM

Posting Permissions