[CLOSED] Set initial value in DateTime combobox

  1. #1

    [CLOSED] Set initial value in DateTime combobox

    Hi,

    Just simply trying to set the value in a server side, dynamically configured combo box.
    Why doesn't this work? It sets the value, but doesn't pick the correct item in the list with the corresponding value.

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <ext:ResourceManager ID="extResourceManager" runat="server" />
    <script type="C#" runat="server">
        public class DateRange
        {
            public DateRange(string name, DateTime startDate)
            {
                this.Name = name;
                this.StartDate = startDate;
            }
            public DateTime StartDate { get; set; }
            public string Name { get; set; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest && !this.IsPostBack)
            {
                System.Collections.Generic.List<DateRange> ranges = new System.Collections.Generic.List<DateRange>();
                ranges.Add(new DateRange("Range1", DateTime.Parse("01/01/2011")));
                ranges.Add(new DateRange("Range2", DateTime.Parse("01/02/2011")));
                ranges.Add(new DateRange("Range3", DateTime.Parse("01/03/2011")));
                ranges.Add(new DateRange("Range4", DateTime.Parse("01/04/2011")));
    
                this.cbRangeStart.ValueField = "StartDate";
                this.cbRangeStart.DisplayField = "Name";
    
                JsonReader reader = new JsonReader();
                reader.Fields.Add("StartDate", RecordFieldType.Date);
                reader.Fields.Add("Name");
                var store = new Store();
                store.ID = string.Format("{0}_store", this.cbRangeStart.ID);
                store.Reader.Add(reader);
                store.DataSource = ranges;
                this.cbRangeStart.Store.Add(store);
                store.DataBind();
    
                //try to select a value
                //this.cbRangeStart.SelectByValue(DateTime.Parse("01/02/2011").ToString());
                this.cbRangeStart.SetValue(DateTime.Parse("01/02/2011"));           
                //this.cbRangeStart.SelectedItem.Value = DateTime.Parse("01/02/2011").ToString();
                //this.cbRangeStart.SelectedItem.Value = "01/02/2011";
            }
        }
    
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <ext:Panel Layout="FormLayout" ID="FormLayout1" runat="server">
                <Items>
                    <ext:ComboBox ID="cbRangeStart" runat="server" Editable="false" TypeAhead="true">
                    </ext:ComboBox>
                </Items>
            </ext:Panel>
        </div>
        </form>
    </body>
    </html>
    Last edited by Daniil; Feb 02, 2012 at 10:31 AM. Reason: [CLOSED]
  2. #2
    Hi,

    The problem is the fact that the RecordField's Type is Date, but these things:
    this.cbRangeStart.SelectedItem.Value = "01/02/2011"
    and others tries to set a string.

    A string doesn't match a date object, therefore that string is set up as a raw text value.

    Please clarify is it possible in your case to select a value via SelectedIndex?
    this.cbRangeStart.SelectedIndex = 1;
  3. #3
    Hmmm, yes, I can find the index of the item in the list I want to select by iterating over the datasource if that is the only way.
  4. #4
    Well, I think it's not a single way, but appears to be the easiest and clearest way.
  5. #5
    Okay, you can close this. Thankyou.

Similar Threads

  1. Replies: 1
    Last Post: Jan 29, 2012, 7:08 PM
  2. [CLOSED] DateTime PickerDate initial month
    By jeybonnet in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Dec 03, 2010, 1:26 PM
  3. Replies: 0
    Last Post: Oct 11, 2010, 4:38 AM
  4. Replies: 0
    Last Post: Jun 23, 2009, 8:31 AM
  5. Replies: 7
    Last Post: Nov 12, 2008, 9:53 PM

Posting Permissions