[CLOSED] Default Item Selection in Combo Box

  1. #1

    [CLOSED] Default Item Selection in Combo Box

    I have combo box having display value and value field.

    Out of N items, I would like to get selected default value based on some transaction perform in CS - code behind.

    Can any one help on this?

    <ext:Store ID="StoreSt" runat="server" ClientIDMode="Static">
            <Model>
                <ext:Model ID="Model1" runat="server" IDProperty="StateID">
                    <Fields>
                        <ext:ModelField Name="StateID" />
                        <ext:ModelField Name="StateName" />                    
                    </Fields>
                </ext:Model>
            </Model>
        </ext:Store>
    
    <ext:ComboBox ID="cmbTest" runat="server" ClientIDMode="Static" ValueField="StateID" DisplayField="StateName"
            Selectable="true" MultiSelect="false" StoreID="StoreSt" EmptyText="Select State" SelectOnFocus="true">
            
        </ext:ComboBox>
    
    Say State Jammy is lying somewhere, when page gets load, it should be default selected,
    
     protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    if (!X.IsAjaxRequest)
                    {
                        AssignMaxLength();
    
                        StoreStateMst.DataSource = EasyPayManager.StateMst.ToList();
                        StoreStateMst.DataBind();
                        StoreSt.DataSource = EasyPayManager.StateMst.ToList();
                        StoreSt.DataBind();
    /* Logic to get state selected */
                       string state = "Jammu";
                       int stateID = 5;
                        cmbTest.SetValue(state);
                            // alos have tried with below
                        //cmbTest.SetValue(stateID);
                       // cmbTest.SetValueAndFireSelect("Jammu");
                        
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
    Please help us out on this,.

    Thank You.

    Regards,
    Last edited by Daniil; Oct 18, 2013 at 4:44 AM. Reason: [CLOSED]
  2. #2
    Hi @shaileshsakaria,

    Please edit the post wrapping the code in [CODE] tags. This is mentioned in these guidelines.
    Forum Guidelines For Posting New Topics
    More Information Required
  3. #3
    Hi,

    Here is the code

    <ext:Store ID="StoreSt" runat="server" ClientIDMode="Static">
    <Model>
    <ext:Model ID="Model1" runat="server" IDProperty="StateID">
    <Fields>
    <ext:ModelField Name="StateID" />
    <ext:ModelField Name="StateName" />
    </Fields>
    </ext:Model>
    </Model>
    </ext:Store>
    
    <ext:ComboBox ID="cmbTest" runat="server" ClientIDMode="Static" ValueField="StateID" DisplayField="StateName"
    Selectable="true" MultiSelect="false" StoreID="StoreSt" EmptyText="Select State" SelectOnFocus="true">
    
    </ext:ComboBox>
    
    Say State Jammy is lying somewhere, when page gets load, it should be default selected,
    
    protected void Page_Load(object sender, EventArgs e)
    {
    try
    {
    if (!X.IsAjaxRequest)
    {
    AssignMaxLength();
    
    StoreStateMst.DataSource = EasyPayManager.StateMst.ToList();
    StoreStateMst.DataBind();
    StoreSt.DataSource = EasyPayManager.StateMst.ToList();
    StoreSt.DataBind();
    /* Logic to get state selected */
    string state = "Jammu";
    int stateID = 5;
    cmbTest.SetValue(state);
    // alos have tried with below
    //cmbTest.SetValue(stateID);
    // cmbTest.SetValueAndFireSelect("Jammu");
    
    }
    }
    catch (Exception ex)
    {
    throw;
    }
    }
    Thanks
  4. #4
    Maybe related to this: http://forums.ext.net/showthread.php...selected-value

    I had the same problem at first. Here is my thread.
  5. #5
    Hi tMp,

    Thanks for your reply.

    I have tried with following code

    cbCountries.SelectedItems.Add(new Ext.Net.ListItem { Value = "4", Mode = ParameterMode.Raw });
    Its working , but problem is combobox became multiselect automatically after applying this, eventhough I didn't set anywhere multiselect property.

    Do you have solution of this?

    Thanks
    Last edited by Daniil; Oct 02, 2013 at 6:15 AM. Reason: Please use [CODE] tags
  6. #6
    Hello!

    Couldn't reproduce your issue. I set default value and my combobox remained single-selectable.

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        private object TestData
        {
            get
            {
                return new object[]
                {
                    new object[] { "AL", "Alabama", "The Heart of Dixie" },
                    new object[] { "AK", "Alaska", "The Land of the Midnight Sun" },
                    new object[] { "AZ", "Arizona", "The Grand Canyon State" },
                    new object[] { "AR", "Arkansas", "The Natural State" },
                    new object[] { "CA", "California", "The Golden State" },
                    new object[] { "CO", "Colorado", "The Mountain State" },
                    new object[] { "CT", "Connecticut", "The Constitution State" },
                    new object[] { "DE", "Delaware", "The First State" },
                    new object[] { "DC", "District of Columbia", "The Nation's Capital" },
                    new object[] { "FL", "Florida", "The Sunshine State" },
                    new object[] { "GA", "Georgia", "The Peach State" },
                    new object[] { "HI", "Hawaii", "The Aloha State" },
                    new object[] { "ID", "Idaho", "Famous Potatoes" },
                    new object[] { "IL", "Illinois", "The Prairie State" },
                    new object[] { "IN", "Indiana", "The Hospitality State" },
                    new object[] { "IA", "Iowa", "The Corn State" },
                    new object[] { "KS", "Kansas", "The Sunflower State" }
                };
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Combobox1.SelectedItems.Add(new Ext.Net.ListItem("AL"));
        }
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
            <ext:ComboBox 
                runat="server"
                ValueField="abbr"
                DisplayField="name"
                Width="320"
                ID="Combobox1"
                LabelWidth="130"
                QueryMode="Local">
                <Store>
                    <ext:Store runat="server" Data="<%# TestData %>" AutoDataBind="true">
                        <Model>
                            <ext:Model runat="server">
                                <Fields>
                                    <ext:ModelField Name="abbr" />
                                    <ext:ModelField Name="name" />
                                    <ext:ModelField Name="slogan" />
                                </Fields>
                            </ext:Model>
                        </Model>
    
                        <Reader>
                            <ext:ArrayReader />
                        </Reader>
                    </ext:Store>
                </Store>
            </ext:ComboBox>
        </form>
    </body>
    </html>
  7. #7
    Hi

    Can you try with this code. In this code combobox becomes multiselect automatically.

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        private object TestData
        {
            get
            {
                return new object[]
                {
                    new object[] { 1, "Alabama", "The Heart of Dixie" },
                    new object[] { 2, "Alaska", "The Land of the Midnight Sun" },
                    new object[] { 3, "Arizona", "The Grand Canyon State" },
                    //new object[] { "AR", "Arkansas", "The Natural State" },
                    //new object[] { "CA", "California", "The Golden State" },
                    //new object[] { "CO", "Colorado", "The Mountain State" },
                    //new object[] { "CT", "Connecticut", "The Constitution State" },
                    //new object[] { "DE", "Delaware", "The First State" },
                    //new object[] { "DC", "District of Columbia", "The Nation's Capital" },
                    //new object[] { "FL", "Florida", "The Sunshine State" },
                    //new object[] { "GA", "Georgia", "The Peach State" },
                    //new object[] { "HI", "Hawaii", "The Aloha State" },
                    //new object[] { "ID", "Idaho", "Famous Potatoes" },
                    //new object[] { "IL", "Illinois", "The Prairie State" },
                    //new object[] { "IN", "Indiana", "The Hospitality State" },
                    //new object[] { "IA", "Iowa", "The Corn State" },
                    //new object[] { "KS", "Kansas", "The Sunflower State" }
                };
            }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
            Bind();
            if(ddlCombo.SelectedItem.Text == null)
                ddlCombo.SelectedItems.Add(new Ext.Net.ListItem("Alabama", 1));
        }
    
        private void Bind()
        {
            List<Data1> datalist = new List<Data1>();
            datalist.Add(new Data1 { id = 1, Name = "Alabama" });
            datalist.Add(new Data1 { id = 2, Name = "Alaska" });
            datalist.Add(new Data1 { id = 3, Name = "Arizona" });
            this.matchCombo.DataSource = datalist.ToList();
            this.matchCombo.DataBind();
        }
        public class Data1
        {
            public int id { get; set; }
            public string Name { get; set; }
        }
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head id="Head1" runat="server">
    </head>
    <body>
        <form id="Form1" runat="server">
            <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
            <ext:Container ID="container11" runat="server">
                <Items>
                    <ext:ComboBox
                        ID="ddlCombo"
                        runat="server"
                        FieldLabel="Select name"
                        DisplayField="Name"
                        ValueField="matchId"
                        EmptyText="Select a Name"
                        Width="300"
                        AnchorHorizontal="90%"
                        SelectOnFocus="true">
                        <Store>
                            <ext:Store ID="matchCombo" runat="server">
                                <Model>
                                    <ext:Model ID="Model2" runat="server">
                                        <Fields>
                                            <ext:ModelField Name="id" Mapping="id" Type="Int" />
                                            <ext:ModelField Name="Name" Mapping="Name" Type="String" />
                                        </Fields>
                                    </ext:Model>
                                </Model>
                            </ext:Store>
                        </Store>
                      
                    </ext:ComboBox>
                </Items>
            </ext:Container>
        </form>
    </body>
    </html>
  8. #8
    Thank you for the test case.

    Please use the following Page_Load.
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            Bind();
            ddlCombo.SelectedItems.Add(new Ext.Net.ListItem("Alabama", 1));
        }
    }
    or
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            Bind();
            ddlCombo.SelectedItem.Value = "1";
            ddlCombo.SelectedItem.Mode = ParameterMode.Raw;
        }
    }

Similar Threads

  1. [CLOSED] How to set default selected item for dataview?
    By jchau in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Dec 24, 2012, 12:00 PM
  2. Combo default value
    By PetrSnobelt in forum 2.x Help
    Replies: 1
    Last Post: Oct 26, 2012, 2:19 PM
  3. ComboBox Default Selection
    By josmar52789 in forum 1.x Help
    Replies: 1
    Last Post: Sep 02, 2009, 1:55 PM
  4. ComboBox - Add Default Item
    By Tbaseflug in forum 1.x Help
    Replies: 0
    Last Post: Feb 03, 2009, 3:26 PM
  5. [OPEN] Default value in a Combo Box
    By Jurke in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Oct 15, 2008, 2:42 PM

Tags for this Thread

Posting Permissions