[CLOSED] Unable to access dynamically created controls

  1. #1

    [CLOSED] Unable to access dynamically created controls

    Hi,

    I have created label and textfield controls dynamically, and added to one container and that container added to panel. Now I am trying to access the value specified in textfield with button direct event, but textfield object is showing null. I tried by create control in every postback also. Here is the code

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.tabSections.HideTabStripItem(pnlSection2);
                this.tabSections.HideTabStripItem(pnlSection3);
                //PopulateSection2Controls();
                //PopulateSection3Controls();
            }
            PopulateSection2Controls();
        }
    
        public void ShowSectionSelected(Object sender, DirectEventArgs e)
        {
            Ext.Net.Button btn;
            btn = (Ext.Net.Button)sender;
            if (btn.ID == "btn2")
            {
                this.tabSections.UnhideTabStripItem(pnlSection2);
                PopulateSection2Controls();          
            }
            else
            {
                PopulateSection3Controls();            
                this.tabSections.UnhideTabStripItem(pnlSection3);
            }
        }
    
        public void PopulateSection2Controls()
        {
            pnl1center.Items.Clear();
            
            Ext.Net.Container c = new Ext.Net.Container();
            Ext.Net.Label lblLabel = new Ext.Net.Label();
            Ext.Net.TextField txtFld = new Ext.Net.TextField();
    
            lblLabel.ID = "lblfiled1";
            lblLabel.Text = "Field Label1";
            txtFld.ID = "txtField1";
            txtFld.EmptyText = "Enter Field value";
            c.Items.Add(lblLabel);
            c.Items.Add(txtFld);
    
            //pnl1center.Items.Add(c);
            c.AddTo(pnl1center);
            this.tabSections.SetActiveTab(pnlSection2);
        }
    
        public void PopulateSection3Controls()
        {
            
            Ext.Net.Container c = new Ext.Net.Container();
            Ext.Net.Label lblLabel = new Ext.Net.Label();
            Ext.Net.TextField txtFld = new Ext.Net.TextField();
    
            lblLabel.ID = "lblfiled2";
            lblLabel.Text = "Field Label2";
            txtFld.ID = "txtField2";
            txtFld.EmptyText = "Enter Field value";
            c.Items.Add(lblLabel);
            c.Items.Add(txtFld);
    
            //pnl2center.Items.Add(c);
            c.AddTo(pnl2center);
            this.tabSections.SetActiveTab(pnlSection3);
        }
    
        public void GetFieldValue(object sender, DirectEventArgs e)
        {
            TextField txtfld;
            txtfld = (TextField)(FindControlRecursive(this,"txtField1"));
    
            MessageBoxConfig msgCng = new MessageBoxConfig();
            msgCng.Message = txtfld.Text;
            msgCng.Buttons = MessageBox.Button.OK;
    
            X.MessageBox.Show(msgCng);
            
        }
    
        public static Control FindControlRecursive(Control Root, string Id)
        {
            if (Root.ID == Id)
            {
                return Root;
            }
    
    
            foreach (Control Ctl in Root.Controls)
            {
                Control FoundCtl = FindControlRecursive(Ctl, Id);
    
    
                if (FoundCtl != null)
                {
                    return FoundCtl;
    
                }
            }
    
            return null;
        }
    
            
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:ResourceManager ID="ResourceManager" runat="server" />
        <ext:Viewport ID="vpReqFields" runat="server" Layout="border">
            <Items>
                <ext:TabPanel ID="tabSections" runat="server" ActiveTabIndex="0" Title="TabPanel"
                    Region="Center">
                    <Items>
                        <ext:Panel ID="pnlSection1" runat="server" Title="Section 1" Layout="Fit" BodyStyle="background-color:#d9d9d9">
                        </ext:Panel>
                        <ext:Panel ID="pnlSection2" runat="server" Title="Section 2" Layout="Border" BodyStyle="background-color:#d9d9d9" Closable="true" CloseAction="Hide">
                        <Items>
                            <ext:Panel ID="pnl1center" runat="server" Region="Center"></ext:Panel>
                        </Items>
                        <Buttons>
                        <ext:Button ID="btnGetText1" runat="server" Text="Get Text" OnDirectClick ="GetFieldValue"></ext:Button>
                        </Buttons>
                        </ext:Panel>
                        <ext:Panel ID="pnlSection3" runat="server" Title="Section 3" Layout="Border" BodyStyle="background-color:#d9d9d9" Closable="true" CloseAction="Hide">
                        <Items>
                            <ext:Panel ID="pnl2center" runat="server" Region="Center"></ext:Panel>
                        </Items>
                        </ext:Panel>
                    </Items>
                </ext:TabPanel>
                <ext:Panel ID="pnlButtons" runat="server" Region="South" Height="40" ButtonAlign="Center">
                    <Items>
                    </Items>
                    <Buttons>
                        <ext:Button ID="btn2" runat="server" Text="Show Section 2">
                            <DirectEvents>
                                <Click OnEvent="ShowSectionSelected">
                                    <EventMask ShowMask="true" Msg="Loading..." />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                        <ext:Button ID="btn3" runat="server" Text="Show Section 3">
                            <DirectEvents>
                                <Click OnEvent="ShowSectionSelected">
                                    <EventMask ShowMask="true" Msg="Loading..." />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                    </Buttons>
                </ext:Panel>
            </Items>
        </ext:Viewport>
        </form>
    </body>
    </html>
    Last edited by Daniil; Apr 09, 2011 at 1:02 PM. Reason: [CLOSED]
  2. #2
    Hi,

    Lets consider
    //pnl1center.Items.Add(c);c.AddTo(pnl1center);
    Please use .AddTo() method to render a control during DirectEvent only. It doesn't add a control to page's controls.

    Please use the commented line instead of .AddTo().
    pnl1center.Items.Add(c);

Similar Threads

  1. saving dynamically-created controls' values
    By Skizzot223 in forum 1.x Help
    Replies: 1
    Last Post: Apr 16, 2012, 12:54 PM
  2. [CLOSED] How to clean up dynamically created controls?
    By jchau in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Aug 23, 2011, 9:51 AM
  3. Replies: 6
    Last Post: Jun 18, 2010, 4:23 PM
  4. Dynamically created controls cookbooks
    By arodier in forum 1.x Help
    Replies: 15
    Last Post: May 07, 2010, 7:12 PM
  5. [CLOSED] Unable to render controls within dynamically created TabPanel
    By Hari_CSC in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 29, 2010, 10:04 AM

Tags for this Thread

Posting Permissions