[CLOSED] I can not obtain a ComboBox created dinamically in the code behind

  1. #1

    [CLOSED] I can not obtain a ComboBox created dinamically in the code behind

    I have created some ComboBoxes in the code behind, but when I try to obtain one of those controls, the function "X.GetCmp<ComboBox>("ComboBoxID")" gives me a ComboBox that is not the one I created (it doesn't have any value that i have created). In fact, when I use "X.GetCmp("ComboBoxID")", it returns me null.
    Both functions work fine with the ComboBoxes created normally in the aspx.

    This is mi code:

    I create the ComboBox here:
    ComboBox cmbNivelX = new ComboBox(cmbConfig);
    cmbNivelX.ID = "cmbNivel" + intNivel;
    cmbNivelX.AutoDataBind = true;
    cmbNivelX.FieldLabel = "Nivel " + intNivel;
    
    (...)
    
    foreach (Jerarquia jerarquia in lstJerarquiasNivelX)
         cmbNivelX.Items.Add(new Ext.Net.ListItem(jerarquia.Descripcion.Trim(), jerarquia.Id.Trim()));
    
    Container ctnNivelX = new Container(ctnNivelConfig);
    ctnNivelX.ID = "ctnNivel" + intNivel;
    ctnNivelX.Add(cmbNivelX);
    
    Container ctnFilaX = new Container(ctnFilaConfig);
    ctnFilaX.ID = "ctnFila" + intFila;
    ctnFilaX.Add(ctnNivelX);
    
    //This panel was created in the aspx
    pnlCombosNiveles.Add(ctnFilaX);
    
    (...)
    I try to obtain the ComboBox here:
    [DirectMethod]
    protected void cmbNivel1_Seleccion(object sender, DirectEventArgs e)
    {
         try
         {
              (...)
              
               //This returns me an empty ComboBox
               ComboBox cmbNivelX = X.GetCmp<ComboBox>("cmbNivel" + intNivel.ToString());
    
               //This returns me null
               ComboBox cmbNivelX = X.GetCmp("cmbNivel" + intNivel.ToString());
    
               (...)
    Thank's a lot!
    Last edited by Daniil; Oct 25, 2010 at 8:24 AM. Reason: [CLOSED]
  2. #2
    Quote Originally Posted by asztern View Post
    I have created some ComboBoxes in the code behind, but when I try to obtain one of those controls, the function "X.GetCmp<ComboBox>("ComboBoxID")" gives me a ComboBox that is not the one I created (it doesn't have any value that i have created).
    This works only for form submitted values.

    Please look at the example which demonstrates it. (click the first button, then the second one).

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void CreateComboBox(object sender, DirectEventArgs e)
        {
            ComboBox combo = new ComboBox()
            {
                ID = "ComboBox1",
                Text = "Hi! I'm a ComboBox",
                FieldLabel = "FieldLabel",
                Width = Unit.Pixel(300)
                
            };
            combo.AddTo(this.Form);
        }
    
        protected void GetComboBoxProperties(object sender, DirectEventArgs e)
        {
            ComboBox combo = X.GetCmp<ComboBox>("ComboBox1");
            string text = 
                    "ComboBox1.Text is \"" + 
                    combo.Text + 
                    "\" (because of the Text is in a postback collection)";
            string fieldLabel =
                "ComboBox1.FieldLabel is \"" + 
                (combo.FieldLabel.Equals("") ? "Empty" : combo.FieldLabel) +
                "\" (because of the FieldLabel is NOT in a postback collection)";
            X.Msg.Alert("DirectEvent", text + "<br/>" + fieldLabel).Show();
        }
    </script>
    
    <!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>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:Button runat="server" Text="Create a ComboBox" OnDirectClick="CreateComboBox" />
        <ext:Button runat="server" Text="Get a ComboBox's properties" OnDirectClick="GetComboBoxProperties" />
        </form>
    </body>
    </html>
  3. #3
    Please post your questions in the Premium Help forum.

    We can miss your request because we are focused on the Premium Help form.
    I'm moving this thread there.
  4. #4
    Quote Originally Posted by asztern View Post
    In fact, when I use "X.GetCmp("ComboBoxID")", it returns me null.
    For getting a created during another control please use the X.GetCmp<T> only.
  5. #5
    Quote Originally Posted by Daniil View Post
    Please post your questions in the Premium Help forum.

    We can miss your request because we are focused on the Premium Help form.
    I'm moving this thread there.
    ok thank's!
  6. #6
    Daniil, thank's for the example. I've taken some things from your example code, but I still have some issues with my case.
    For example, the reason why I need to recover the comboboxes dinamically created is that i need to refill them with another collection. My doubt is if I need to re-create those controls. Untill now, I was doing that (also i'm creating dinamically the Containers to place the comboboxes), but when I do that, the controls don't appear placed where I put for the first time. (I use always the same code to create the comboboxes and the containers)

    This are the captures from my screen. You can observ that after the update, the comboboxes are disordered.
    Click image for larger version. 

Name:	CombosOK.JPG 
Views:	93 
Size:	21.0 KB 
ID:	1770
    Click image for larger version. 

Name:	CombosWrong.JPG 
Views:	101 
Size:	17.4 KB 
ID:	1769

    The event handler that I'm using for update those comboboxes is a 'OnDirectSelect' defined in another ComboBox created normally in the aspx.

    Thank's!!
  7. #7
    Hi,

    What about this?

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void AddItem(object sender, DirectEventArgs e)
        {
            ComboBox1.Items.Add(new Ext.Net.ListItem("NEW ITEM", "3"));
            ComboBox1.GetStore().DataBind();
        }
    
        protected void RemoveAll(object sender, DirectEventArgs e)
        {
            ComboBox1.Items.Clear();
            ComboBox1.GetStore().DataBind();
        }
    </script>
    
    <!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>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:ComboBox ID="ComboBox1" runat="server">
            <Items>
                <ext:ListItem Text="Text1" Value="1" />
                <ext:ListItem Text="Text2" Value="2" />
            </Items>
        </ext:ComboBox>
        <ext:Button runat="server" Text="Add item" OnDirectClick="AddItem" />
        <ext:Button runat="server" Text="Remove all items" OnDirectClick="RemoveAll" />
        </form>
    </body>
    </html>
  8. #8
    Finally I could solve this! Your last example was really helpfull, because I wasn't doing 'cmbNivelX.GetStore().DataBind()'.
    Now my code is this:

     
    ComboBox cmbNivelX = X.GetCmp<ComboBox>("cmbNivel" + intNivel);
    cmbNivelX.Items.Clear();
     
    (...)
     
    foreach (Jerarquia jerarquia in lstJerarquiasNivelX)
        cmbNivelX.Items.Add(new Ext.Net.ListItem(jerarquia.Descripcion.Trim(), jerarquia.Id.Trim()));
     
    cmbNivelX.SelectedIndex = 0;
     
    (...)
     
    cmbNivelX.GetStore().DataBind();
    Thank you very much Daniil!
    Greetings...

Similar Threads

  1. Color combobox created by code behind
    By xtremexploit in forum 1.x Help
    Replies: 9
    Last Post: Jul 19, 2013, 12:45 PM
  2. [CLOSED] Add button to dinamically created window toolbar
    By Pablo_Azevedo in forum 1.x Legacy Premium Help
    Replies: 9
    Last Post: Oct 13, 2011, 4:07 PM
  3. Replies: 6
    Last Post: Nov 02, 2010, 3:25 PM
  4. [CLOSED] [1.0] get value of dinamically created control
    By miguelon in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Mar 27, 2010, 4:01 PM
  5. [CLOSED] Closing dinamically created windows
    By jcanton in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jul 23, 2009, 9:24 AM

Tags for this Thread

Posting Permissions