[CLOSED] How to select existing item in ComboBox on the server?

  1. #1

    [CLOSED] How to select existing item in ComboBox on the server?

    Hi,

    It's probably a very basic question. I need to select an existing list item on the server. The code below doesn't work. Could you please suggest the right approach or point out what's wrong?

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <script runat="server">     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                var list = new List<object>
                {
                    new {Text = "Text3", Value = 3},
                    new {Text = "Text4", Value = 4},
                    new {Text = "Text5", Value = 5}
                };
    
                this.Store1.DataSource = list;
                this.Store1.DataBind();
    
                // This call doesn't work
                ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "4" });
    
                // Item reference resolves to null so this call doesn't work
                /*
                var item = ComboBox1.Items.Where(it => it.Value.Equals("4")).FirstOrDefault();
                if (item != null)
                {
                    ComboBox1.SelectedItems.Add(item);
                }
                */
    
                // This call doesn't work either
                //ComboBox1.Select(1);
            }
        }
    </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 id="Head1" runat="server">
        <title>Ext.Net 2.x</title>
        <ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="Script" />
        <ext:ResourcePlaceHolder ID="ResourcePlaceHolder2" runat="server" Mode="Style" />
    </head>
    <body>
        <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server">
        </ext:ResourceManager>
        <ext:Viewport runat="server" Layout="FitLayout">
            <Items>
                <ext:Panel runat="server">
                    <Items>
                        <ext:ComboBox ID="ComboBox1" runat="server" DisplayField="Text" ValueField="Value"
                            FieldLabel="Select" QueryMode="Local">
                            <Store>
                                <ext:Store ID="Store1" runat="server">
                                    <Model>
                                        <ext:Model ID="Model1" runat="server" IDProperty="Value">
                                            <Fields>
                                                <ext:ModelField Name="Text" />
                                                <ext:ModelField Name="Value" />
                                            </Fields>
                                        </ext:Model>
                                    </Model>
                                </ext:Store>
                            </Store>
                        </ext:ComboBox>
                    </Items>
                </ext:Panel>
            </Items>
        </ext:Viewport>
        </form>
    </body>
    </html>
    Last edited by Daniil; Jul 17, 2013 at 4:24 AM. Reason: [CLOSED]
  2. #2
    Hello!

    Values in the list should have string values:

    <%@ Page Language="C#" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <script runat="server">     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                var list = new List<object>
                {
                    new {Text = "Text3", Value = "3" },
                    new {Text = "Text4", Value = "4" },
                    new {Text = "Text5", Value = "5" }
                };
     
                this.Store1.DataSource = list;
                this.Store1.DataBind();
     
                ComboBox1.SelectedItems.Add(new Ext.Net.ListItem { Value = "4" });
            }
        }
    </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 id="Head1" runat="server">
        <title>Ext.Net 2.x</title>
        <ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="Script" />
        <ext:ResourcePlaceHolder ID="ResourcePlaceHolder2" runat="server" Mode="Style" />
    </head>
    <body>
        <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server">
        </ext:ResourceManager>
        <ext:Viewport runat="server" Layout="FitLayout">
            <Items>
                <ext:Panel runat="server">
                    <Items>
                        <ext:ComboBox ID="ComboBox1" runat="server" DisplayField="Text" ValueField="Value"
                            FieldLabel="Select" QueryMode="Local">
                            <Store>
                                <ext:Store ID="Store1" runat="server">
                                    <Model>
                                        <ext:Model ID="Model1" runat="server" IDProperty="Value">
                                            <Fields>
                                                <ext:ModelField Name="Text" />
                                                <ext:ModelField Name="Value" />
                                            </Fields>
                                        </ext:Model>
                                    </Model>
                                </ext:Store>
                            </Store>
                        </ext:ComboBox>
                    </Items>
                </ext:Panel>
            </Items>
        </ext:Viewport>
        </form>
    </body>
    </html>
  3. #3
    Thanks much Daulet! Your suggestion works. I've got to convert the key Integer ID fields in the Models to String and then create lists from original data sources to databind the combo stores. It feels unfortunate that item selection on the server has to draw such an overhead going against natural ModelField data types.
  4. #4
    You also can use numbers in this way:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            var list = new List<object>
            {
                new {Text = "Text3", Value = 3 },
                new {Text = "Text4", Value = 4 },
                new {Text = "Text5", Value = 5 }
            };
     
            this.Store1.DataSource = list;
            this.Store1.DataBind();
     
            ComboBox1.SelectedItems.Add(new Ext.Net.ListItem(4));
        }
    }
  5. #5
    Thanks again! That was it, please close this thread down.

Similar Threads

  1. I can not select any item in combobox
    By marco.amusquivar in forum 1.x Help
    Replies: 1
    Last Post: Mar 21, 2012, 8:09 PM
  2. Replies: 3
    Last Post: Dec 21, 2011, 8:40 AM
  3. Replies: 0
    Last Post: Oct 25, 2010, 9:49 AM
  4. Select a item in a Combobox
    By eliezer in forum 1.x Help
    Replies: 1
    Last Post: Apr 16, 2009, 12:23 PM
  5. multi select add selected item on server side
    By [WP]joju in forum 1.x Help
    Replies: 4
    Last Post: Jan 09, 2009, 7:01 AM

Tags for this Thread

Posting Permissions