How to programatically select an item from the ComboBox?

Page 1 of 2 12 LastLast
  1. #1

    How to programatically select an item from the ComboBox?

    For a standard ASP.NET DropDownControl, I used this code to select an item from the DropDown:

    DropDown1.SelectedIndex = DropDown1.Items.IndexOf(DropDown1.Items.FindByText ("John F"));
    How to do the same with ext.net ComboBox control?

    I tried:

    ComboBox1.SelectedItem.Text = "John F";

    But this displays the name on the ComboBox but it doesn't returns the Value Member of the selected item.
  2. #2
    Hi,

    But this displays the name on the ComboBox but it doesn't returns the Value Member of the selected item.
    Please provide a sample to reproduce.
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi,
    Please provide a sample to reproduce.
    Create a ComboBox and fill three items with values associated with them.

    For exmaple:

    Display Member Value Member
    ------------------------------------------------
    Item1 .... 001
    Item2 .... 002
    Item3 .... 003

    Now programatically select second item using:
    ComboBox1.SelectedItem.Text = "Item2";
    You won't get it's value member, if you try:

    MessageBox.Show(ComboBox1.SelectedItem.Value.ToString());
    Last edited by Daniil; Mar 05, 2012 at 8:53 AM. Reason: Please use [CODE] tags
  4. #4
    At the same DirectEvent request?

    Yes, there is no such functionality to fill SelectedItem.Value basing on SelectedItem.Text.
  5. #5
    Quote Originally Posted by Daniil View Post
    At the same DirectEvent request?
    Yes, there is no such functionality to fill SelectedItem.Value basing on SelectedItem.Text.
    Let me clarify.

    I don't want to fill Values. The ComboBox is already filled with Items (Display Member) and Values (Value Member).

    When I directly select an item from ComboBox, I am able to select an item and value.

    But when I select an item using code-behind (.cs), I am not able to pick the Value of the Item.

    I want to know how to select an item using .cs code.
  6. #6
    Got it.

    When you call
    ComboBox1.SelectedItem.Text = "Item2";
    it sets ComboBox value to "item2", not just its text.

    You should select an item by its index or value, i.e:
    this.ComboBox1.SelectedIndex = 1;
    or
    this.ComboBox1.SelectedItem.Value = "2";
    Example
    <%@ 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)
            {
                Store store = this.ComboBox1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "1", "item1" },
                    new object[] { "2", "item2" },
                    new object[] { "3", "item3" }
                };
                store.DataBind();
            }
        }
    
        protected void Select(object sender, DirectEventArgs e)
        {
            this.ComboBox1.SelectedIndex = 1;
        }
    
        protected void Get(object sender, DirectEventArgs e)
        {
            X.Msg.Alert("Value", this.ComboBox1.SelectedItem.Value).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:ResourceManager runat="server" />
            <ext:ComboBox ID="ComboBox1" runat="server">
                <Store>
                    <ext:Store runat="server">
                        <Reader>
                            <ext:ArrayReader>
                                <Fields>
                                    <ext:RecordField Name="value" />
                                    <ext:RecordField Name="text" />
                                </Fields>
                            </ext:ArrayReader>
                        </Reader>
                    </ext:Store>
                </Store>
            </ext:ComboBox>
            <ext:Button runat="server" Text="Select the 2nd item" OnDirectClick="Select" />
            <ext:Button runat="server" Text="Get SelectedItem.Value" OnDirectClick="Get" />
        </form>
    </body>
    </html>
  7. #7
    Hi,

    I have the same problem, I put de code to select the item in .cs by using selecteditem.value. The selectedvalue (Value Member) it´s Ok, but the Display Member is wrong, the combobox show the same value of Value Member. If I click in the item, the display member change it´s update to the to the Display Member.

    Code to fill the records:
        private void posicionarFuncionario()
        {
            var lista = from f2 in dc.Funcionarios.AsEnumerable() where f2.IdFuncionario == 1 select new { IdFuncionario = 0, NomeFuncionario = "Selecionar" };
    
            var lista2 = (lista.Union(from f in dc.Funcionarios.AsEnumerable()
                                      select new
                                      {
                                          f.IdFuncionario,
                                          f.NomeFuncionario
                                      }));
    
            this.Store1.DataSource = lista2;
            this.Store1.DataBind();
        }
    Code to select the item:
    ddlFuncionario.SelectedItem.Value = usu.IdFuncionario.ToString();
    How can I do to correct this?

    Thanks!
  8. #8
    Hello!

    I solve my problem setting the property AutoLoad=true for the Store. Now the Display Value is correct when the form is show.
  9. #9
    My ComboBox is loaded with query when the page loads. I programatically select an item using:

    ComboBox1.setValue("Item 3");
    But with this method I am not able to get Value. The Display Member is available but the Value Member is not available.

    I tried:

    ComboBox1.SelectedItem.Value
    but gives error till the time I manually re-select the Item from the ComboBox.
    Last edited by geoffrey.mcgill; Oct 27, 2012 at 4:44 PM.
  10. #10

    I have the same problem please help.........

    hi im using the v2 version i write the code in the below code

    ComboBox1.setValue("3")
    it will set the value for combobox1 but it disply the id , i want to display the text field of that related id please hellp.......
    Last edited by geoffrey.mcgill; Oct 27, 2012 at 4:44 PM. Reason: please use [CODE] tags
Page 1 of 2 12 LastLast

Similar Threads

  1. [CLOSED] How to select a ComboBox item by value?
    By vadym.f in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Apr 16, 2012, 4:15 PM
  2. 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
  3. [CLOSED] Combobox, select first item
    By Yevgeniy in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 28, 2011, 12:05 PM
  4. Replies: 0
    Last Post: Sep 20, 2010, 8:56 PM
  5. Select a item in a Combobox
    By eliezer in forum 1.x Help
    Replies: 1
    Last Post: Apr 16, 2009, 12:23 PM

Tags for this Thread

Posting Permissions