[CLOSED] Problem with selectedItem.value in combobox

  1. #1

    [CLOSED] Problem with selectedItem.value in combobox

    The following code gives a strange problem:

    <%@ Page Language="C#" %>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ExtNet.IsAjaxRequest)
            {
                strState.DataSource = TestData;
                strState.DataBind();
                cbState.SetValueAndFireSelect("KS");
            }
        }
    
    
        protected void Button_Click(object sender, DirectEventArgs e)
        {
            ExtNet.Msg.Alert("Selected Value", string.Format("{0}",cbState.SelectedItem.Value)).Show();
        }
             
    
    
        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" },
                    new object[] { "KY", "Kentucky", "The Bluegrass State" },
                    new object[] { "LA", "Louisiana", "The Bayou State" },
                    new object[] { "ME", "Maine", "The Pine Tree State" },
                    new object[] { "MD", "Maryland", "Chesapeake State" },
                    new object[] { "MA", "Massachusetts", "The Spirit of America" },
                    new object[] { "MI", "Michigan", "Great Lakes State" },
                    new object[] { "MN", "Minnesota", "North Star State" },
                    new object[] { "MS", "Mississippi", "Magnolia State" },
                    new object[] { "MO", "Missouri", "Show Me State" },
                    new object[] { "MT", "Montana", "Big Sky Country" },
                    new object[] { "NE", "Nebraska", "Beef State" },
                    new object[] { "NV", "Nevada", "Silver State" },
                    new object[] { "NH", "New Hampshire", "Granite State" },
                    new object[] { "NJ", "New Jersey", "Garden State" },
                    new object[] { "NM", "New Mexico", "Land of Enchantment" },
                    new object[] { "NY", "New York", "Empire State" },
                    new object[] { "NC", "North Carolina", "First in Freedom" },
                    new object[] { "ND", "North Dakota", "Peace Garden State" },
                    new object[] { "OH", "Ohio", "The Heart of it All" },
                    new object[] { "OK", "Oklahoma", "Oklahoma is OK" },
                    new object[] { "OR", "Oregon", "Pacific Wonderland" },
                    new object[] { "PA", "Pennsylvania", "Keystone State" },
                    new object[] { "RI", "Rhode Island", "Ocean State" },
                    new object[] { "SC", "South Carolina", "Nothing Could be Finer" },
                    new object[] { "SD", "South Dakota", "Great Faces, Great Places" },
                    new object[] { "TN", "Tennessee", "Volunteer State" },
                    new object[] { "TX", "Texas", "Lone Star State" },
                    new object[] { "UT", "Utah", "Salt Lake State" },
                    new object[] { "VT", "Vermont", "Green Mountain State" },
                    new object[] { "VA", "Virginia", "Mother of States" },
                    new object[] { "WA", "Washington", "Green Tree State" },
                    new object[] { "WV", "West Virginia", "Mountain State" },
                    new object[] { "WI", "Wisconsin", "America's Dairyland" },
                    new object[] { "WY", "Wyoming", "Like No Place on Earth" } 
                };
            }
        }
    </script>
    
    
    <!DOCTYPE html>
    
    
    <html>
    <head id="Head1" runat="server">
        <title>Combo Boxes - Ext.NET Examples</title>
        <link href="/resources/css/examples.css" rel="stylesheet" type="text/css" />
    
    
        <style type="text/css">
            .example {
                width: 600px;
                border: 1px solid #CCC;
                padding: 0px 10px 10px 10px;
                margin: 0 0 10px;
            }
            
            #transformCombo label {
                display: block;
                margin: 1em 0 2px;
            }
        </style>
    </head>
    <body>
        <form id="Form2" runat="server">
            <ext:ResourceManager ID="ResourceManager1" runat="server" />
    
    
            <ext:ObjectHolder ID="DataHolder" runat="server" />
           
            <h1>Combo Boxes</h1>        
    
    
            <div class="example">
                <ext:ComboBox ID="cbState" 
                    runat="server"
                    FieldLabel="Select a single state"
                    ValueField="abbr"
                    DisplayField="name"
                    Width="320"
                    LabelWidth="130"
                    QueryMode="Local"               
                    TypeAhead="true">
                    <Store>
                        <ext:Store ID="strState" runat="server" AutoDataBind="true">
                            <Model>
                                <ext:Model ID="mdlStore" runat="server">
                                    <Fields>
                                        <ext:ModelField Name="abbr" />
                                        <ext:ModelField Name="name" />
                                    </Fields>
                                </ext:Model>
                            </Model>
    
    
                            <Reader>
                                <ext:ArrayReader />    
                            </Reader>
                        </ext:Store>
                    </Store>
                </ext:ComboBox>
                <ext:Button runat="server" ID="btnSubmit" Text="Test">
                     <DirectEvents>
                            <Click OnEvent="Button_Click">
                                <EventMask ShowMask="true" Msg="Test..." MinDelay="500" />
                            </Click>
                        </DirectEvents>
                </ext:Button>
            </div>
        </form>
    </body>
    </html>
    Preselecting the value (SetValueAndFireSelect) gives the correct state in the combo, but when clicking the button (without selecting another state) gives the selected Text in stead of the Selected Value. When selecting another state in the combobox and you click on teh button, the value is given. Any idea how to solve ? (I always want to get the value...also when no other state is selected)

    Regards,

    Martin
    Last edited by Daniil; Sep 10, 2012 at 1:49 PM. Reason: [CLOSED]
  2. #2
    SetValueAndFireSelect must be used during ajax request only (when combo is rendered on the client side and data is loaded)

    Please use SelectedItems collection to set init selection
    cbState.SelectedItems.Add(new Ext.Net.ListItem { Value = "KS" });
  3. #3
    Hi Martin,

    The SetValueAndFireSelect method is designed to be used during a DirectEvent/DirectMethod.

    For initial page load, please populate the SelectedItems collection of the ComboBox.
  4. #4
    Thanks guys ! Mark as closed..

    Martin
  5. #5
    BTW..

    I needed to add some extra infor because in my own code the selected value is an Integer..

    combobox.SelectedItems.Add(new Ext.Net.ListItem { Value = Number.ToString(),Mode = ParameterMode.Raw });
    Martin
  6. #6
    Yes, you are right, in this case you should set ParameterMode.Raw.

Similar Threads

  1. Replies: 10
    Last Post: May 16, 2014, 4:39 AM
  2. [CLOSED] Problem to show SelectedItem in Combobox v2.0
    By Oliver in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: May 28, 2012, 3:56 PM
  3. Replies: 2
    Last Post: Mar 03, 2012, 3:33 PM
  4. Replies: 4
    Last Post: Nov 30, 2011, 5:25 AM
  5. ComboBox SelectedItem according to Value problem
    By juredecman in forum 1.x Help
    Replies: 0
    Last Post: Aug 25, 2010, 8:57 PM

Posting Permissions