[CLOSED] Problem Setting a ComboBox Value

  1. #1

    [CLOSED] Problem Setting a ComboBox Value

    Hi,

    My problem is that I cant set a default ComboBox value without a page load.


    Here is an example with a delailed explanation below.

    .aspx:
    <%@ Page Title="Extranet Jhayber" Language="C#" 
        CodeBehind="Pruebaextnet.aspx.cs" Inherits="Extranet.Pruebaextnet" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Xml" %>
    
    <html>
    <head>
    	
    </head>
    <body>
    <form runat="server">
    
        <ext:ResourceManager runat="server" />
        <ext:Viewport ID="Viewport1" runat="server" Layout="columnlayout" AutoScroll="true" >
            <Items>
                    <ext:Panel runat="server" id="panel1" ColumnWidth="1" Height="800" Frame="true">
                        <Items>
                            <ext:ComboBox
                                id="ComboExample"
                                runat="server"
                                FieldLabel="Select a single state"
                                DisplayField="abbr"
                                Width="320"
                                LabelWidth="130"
                                QueryMode="Local"
                                TypeAhead="true"
                                ValueField="abbr">
                                <Store>
                                    <ext:Store runat="server" id="Storecombo" 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>
                            <ext:Button runat="server" ID="button" Text="Combo default">
                                <DirectEvents>
                                    <Click OnEvent="changecombo"></Click>
                                </DirectEvents>
                            </ext:Button>
                        </Items>
                    </ext:Panel>
            </Items>
        </ext:Viewport>
           
    
    </form>
    </body>
    </html>
    aspx.cs:
    namespace Extranet
    {
        public partial class Pruebaextnet : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Storecombo.DataSource = TestData;
                Storecombo.DataBind();
    
                string defaultvalue = (string)(Session["default"]);
                if(!string.IsNullOrEmpty(defaultvalue))
                {
                    this.ComboExample.SelectedItems.Add(new Ext.Net.ListItem() { Value = defaultvalue });
                }
                
            }
    
            protected void changecombo(object sender, EventArgs e)
            {
                this.ComboExample.SelectedItems.Add(new Ext.Net.ListItem() { Value = "AL" });
    
                Session["default"] = "AL";
            }
    
            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" },
    
                    };
                }
            }
        }
    
    }
    What I want to do is to set the default value "AL" when I press the button, but this is not working. However, as you can see in the example, If I create a session with the default value "AL" and I do the same in the pageload, if i reload the page, It works nice.

    I want to do this, without reloading the page and use a session variable.

    Thank you for your time.
  2. #2
    Hello @PascuV!

    So your problem is not really setting the default selection but, once you change what should be the default selection, also set the combo box to the value you set as default?

    If so, I believe you are just missing a ComboExample.UpdateSelectedItems(); after you update the selection. It works during page load without this because the combo box is being built and a call to update the selection will be made during page/components rendering. Once it is already loaded you got to inform that the selection change you made you want to be made effective that moment, and extra client-side code is called to update the value just set.

    I hope this helps!
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. Replies: 10
    Last Post: May 16, 2014, 4:39 AM
  2. [CLOSED] Setting SelectedItems of a combobox from MVC Model
    By hikalkan in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Apr 19, 2013, 10:50 AM
  3. [CLOSED] javascript error while setting combobox first value
    By ISI in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Aug 26, 2011, 2:22 PM
  4. [CLOSED] Setting a ComboBox Value
    By sisa in forum 1.x Legacy Premium Help
    Replies: 11
    Last Post: May 25, 2011, 9:45 AM
  5. [CLOSED] Setting Combobox value in code behind
    By iansriley in forum 1.x Legacy Premium Help
    Replies: 9
    Last Post: Dec 15, 2010, 7:35 PM

Posting Permissions