[CLOSED] Combobox value and text fields

  1. #1

    [CLOSED] Combobox value and text fields

    Hi,

    I want to store employee ID into the value field and employee name into the text field of the combobox. The combobox will be filled with data during runtime. I tried the following but it didn't seem to work. The <combobox>.SelectedItem showing the employee name value for both Text and Value field. Can someone point out what I did wrong? Thanks in advance for the help.

    Dan

    - Client code
    
    <ext:Store ID="Area_Store" runat="server">
    <Reader>
              <ext:ArrayReader ReaderID="Area">
                    <Fields>
                        <ext:RecordField Name="id" Mapping="id" />
                        <ext:RecordField Name="text" Mapping="text" />
                    </Fields>
                </ext:ArrayReader>
            </Reader>
    </ext:Store>
    
    
    <ext:ComboBox
          ID="CbArea" 
          runat="server" 
          StoreID="Area_Store" 
          Editable="false" 
          DisplayField="text" 
          ValueField="id" 
    EmptyText="---Select---" OnItemSelected="CbArea_SelectedIndexChanged">
          <Listeners>
              <Change Handler="Area_Store.addRecord({id: 2, text: 'Area 51'});Area_Store.commitChanges();" />
         </Listeners>
    </ext:ComboBox>
    - server-side code
    ArrayList areaList = iDAO.SelectAreaList();
    List<object> areaData = new List<object>(areaList.Count);
    foreach (System.Web.UI.WebControls.ListItem area in areaList)
    {
         areaData.Add(new Object[] { int.Parse(area.Value), area.Text });
    }
    this.Area_Store.DataSource = areaData;
    this.Area_Store.DataBind();
    Last edited by Daniil; Nov 22, 2010 at 11:22 PM. Reason: [CLOSED]
  2. #2
    Hi,

    The SelectedItem should work as expected, see example below.

    ArrayList areaList = iDAO.SelectAreaList();
    What is content of areaList? Could you provide a full (but simplified) sample code?

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
    
    <script runat="server">
        protected void GetSelectedItem(object sender, AjaxEventArgs e)
        {
            Ext.Msg.Alert("", "Text: " + this.ComboBox1.SelectedItem.Text +
                            "<br/>Value: " + this.ComboBox1.SelectedItem.Value.ToString()).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:ScriptManager 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="SelectedItem">
            <AjaxEvents>
                <Click OnEvent="GetSelectedItem" />
            </AjaxEvents>
        </ext:Button>
        </form>
    </body>
    </html>
  3. #3
    Hi Daniil,

    Thanks for the quick reply. To answer your question regarding to the following

    ArrayList areaList = iDAO.SelectAreaList();
    The areaList content will hold Area ID in the ListItem.Value field and Area Name om the ListItem.Text field. However, the SelectedItem kept showing Area Name in both Value and Text fields. I wonder if the method I'm assigning the values in the areaList to the Store of the combobox is correct method (see the server-side code from the oringinal email thread).

    Sample code -- (Sorry. I couldn't go any further into details. Hope you'd understand)
    
            public ArrayList SelectAreaList()
            {
                FwGenericDAO dao = new FwGenericDAO(FactoryName);
                //Get a list of Areas from database
                ArrayList list = ((FwDataCollection)dao.Select("AREA_NAME_ALL", this.InvestigatorConfigFile)).GetValues();
                return ConvertToArrayListOfListItems("SelectAreaList",list, "AREA_ID", "AREA_NAME");
            }
    Also please notice that I'm not building the comboxbox list items from client-side. I'm doing this from the server-side during the runtime. I am just not sure why it's not working.

    Dan
    Last edited by Daniil; Nov 18, 2010 at 5:21 PM. Reason: Please use [CODE] tags
  4. #4
    Hi,

    Seems the Mapping is a root evil. I'm not sure why you are using this. Why?

    It works fine without this property.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Coolite.Ext.Web" Namespace="Coolite.Ext.Web" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList areaList = new ArrayList();
            areaList.Add(new System.Web.UI.WebControls.ListItem("text1", "1"));
            areaList.Add(new System.Web.UI.WebControls.ListItem("text2", "2"));
            
            List<object> areaData = new List<object>(areaList.Count);
            foreach (System.Web.UI.WebControls.ListItem area in areaList)
            {
                areaData.Add(new Object[] { int.Parse(area.Value), area.Text });
            }
            this.Store1.DataSource = areaData;
            this.Store1.DataBind();
        }
    
        protected void GetSelectedItem(object sender, AjaxEventArgs e)
        {
            Ext.Msg.Alert("", "Text: " + this.ComboBox1.SelectedItem.Text +
                            "<br/>Value: " + this.ComboBox1.SelectedItem.Value.ToString()).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>Coolite 0.8.X Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ScriptManager runat="server" />
        <ext:Store ID="Store1" runat="server">
            <Reader>
                <ext:ArrayReader ReaderID="Area">
                    <Fields>
                        <ext:RecordField Name="id" />
                        <ext:RecordField Name="text" />
                    </Fields>
                </ext:ArrayReader>
            </Reader>
        </ext:Store>
        <ext:ComboBox 
            ID="ComboBox1" 
            runat="server" 
            StoreID="Store1" 
            ValueField="id" 
            DisplayField="text" />
        <ext:Button runat="server" Text="SelectedItem">
            <AjaxEvents>
                <Click OnEvent="GetSelectedItem" />
            </AjaxEvents>
        </ext:Button>
        </form>
    </body>
    </html>
  5. #5
    Daniil,

    You're correct! The example below does show the correct values for both SelectedItem.Text and .Value. My next question is if I have multiple combobox and these boxes will be filled with data during runtime. The selected item of some of these boxes will depend on other combo boxes selection. Here is an example of the scenarios:

    - A stored object hold selected value for combo box A.
    - During page load, combo box A items is loaded at this time, and then the SeletedItem.Text will be set to the value stored in the object. i.e. "Area 51".

    ComboBoxA.SelectedItem.Text = object.valueToAssign;  //which is "Area 51"
    - Combo box B items will be pull from database based on the selected value of combo box A.

    The problem I am running into is the arrayList (see original post) hold both value and text for "Area 51", but after the assignment above, both SelectedItem.Value and .Text showing as "Area 51" instead of SelectedItem.Value has the ID of the "Area 51".

    To make is short, I am not sure what I need to invoke the Combox box A to recognize the value assignment abvove. Hope I'm not murky up the water here :-)

    Dan
    Last edited by Daniil; Nov 19, 2010 at 4:45 AM. Reason: Please use [CODE] tags
  6. #6
    Hi,

    I'm not sure where the problem is.

    Please post a full (but simplified) sample code to reproduce.

    Please note that you can attach the whole test project (zipped and without any dlls, excepting specific ones). Also you can send it on our support email - support@object.net
  7. #7
    Daniil,

    Thanks for the reply. The problem I am experiencing is after the combo box has been populated with data, you won't be able to simply get the SelectedItem.Value (ID field if you will) from anywhere in the code unless there is some sort of event (i.e. click, etc.) has occurred. For example, after populating the combo box (please see original post), you can't simply doing something like the following within any method caller (not event handler)

    string id = Combobox1.SelectedItem.Value;
    Hope this help.

    Dan
    Last edited by Daniil; Nov 19, 2010 at 2:25 PM. Reason: Please use [CODE] tags
  8. #8
    Quote Originally Posted by dnguyen View Post
    For example, after populating the combo box (please see original post), you can't simply doing something like the following within any method caller (not event handler)

    string id = Combobox1.SelectedItem.Value;
    Hope this help.

    Dan
    Just after populating in Page_Load? Do I understand properly?
  9. #9
    Daniil,

    Yes, that's correct. By the way, will it make any difference if the combo box get populate during page_load or page_prerender?

    Dan
  10. #10
    Quote Originally Posted by dnguyen View Post
    Daniil,
    Yes, that's correct.
    Dan
    What is a selected item just after data binding? The SelectedItem property makes sense if user select one of ComboBox's items.


    Quote Originally Posted by dnguyen View Post
    Daniil,
    By the way, will it make any difference if the combo box get populate during page_load or page_prerender?
    Dan
    No, it should not make any difference.

Similar Threads

  1. [CLOSED] Issue with text boxes/spinner fields in IE6
    By musher in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Jul 05, 2012, 10:37 AM
  2. Combobox DisplayField 2 fields???
    By 78fede78 in forum 1.x Help
    Replies: 3
    Last Post: Sep 06, 2010, 2:07 PM
  3. Replies: 10
    Last Post: Apr 05, 2010, 1:42 AM
  4. ComboBox to Sort Store By Fields
    By Tbaseflug in forum 1.x Help
    Replies: 2
    Last Post: Apr 22, 2009, 4:02 PM
  5. [CLOSED] Empty values from text fields
    By CSG in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Apr 21, 2009, 10:14 AM

Posting Permissions