Combobox with indention - select value initially

  1. #1

    Combobox with indention - select value initially

    Hi,

    I'm struggling around with a combobx that shall be used to show up indented items with comments and be able to initially select a value on page load.

    I managed to show indention by adopting the following sample:

    Custom drop down list

    I also can select a value during a button click. I also can handle the selected value in code behind. But I don't get managed to select a value initially during page load - see lines 39-42 of code-behind.

    Markup:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestCustomDropDownList.aspx.cs" Inherits="skai_maschinendatenbank_webapp.Pages.TestCustomDropDownList" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Comboboxes - Ext.NET Examples</title>
        <link href="/resources/css/examples.css" rel="stylesheet" />
        <style>        
            .myindent1 { padding:3px 10px 3px 10px; }
            .myindent2 { padding:3px 10px 3px 20px; }
            .myindent3 { padding:3px 10px 3px 30px; }
            
            .list-item {
                font:normal 11px tahoma, arial, helvetica, sans-serif;
                border:0px solid #fff;
                border-bottom:1px solid #eeeeee;
                white-space:normal;
                color:#555;
            }
            
            .list-item h3 {
                display:block;
                font:inherit;
                font-weight:bold;
                margin:0px;
                color:#222;
            }
        </style>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager ID="ResourceManager1" runat="server" />
            
            <ext:ComboBox 
                runat="server"
                ID="mycombobox"
                Width="250"
                Editable="false"
                DisplayField="Name"
                ValueField="ID"
                QueryMode="Local"
                ForceSelection="true"
                TriggerAction="All"
                EmptyText="Select department">                        
                <Store>
                    <ext:Store ID="Store1" runat="server">
                        <Model>
                            <ext:Model runat="server" IDProperty="ID">
                                <Fields>
                                    <ext:ModelField Name="ID" />
                                    <ext:ModelField Name="Abbrev" />
                                    <ext:ModelField Name="Name" />
                                    <ext:ModelField Name="Indent" Type="Int" />
                                </Fields>
                            </ext:Model>
                        </Model>            
                    </ext:Store>
                </Store>
                <ListConfig>
                    <ItemTpl runat="server">
                        <Html>
    						<div class="list-item myindent{Indent}">
    							    <h3>{Name}</h3>
    							    {Abbrev:ellipsis(15)}
    						</div>
    				    </Html>
                    </ItemTpl>
                </ListConfig>
            </ext:ComboBox>
            <ext:Button runat="server" ID="btnSelect" Text="Select UI-Dev" OnDirectClick="btnSelect_DirectClick" />
            <ext:Button runat="server" ID="btnShowSelect" Text="Show Selection" OnDirectClick="btnShowSelect_DirectClick" />
            <ext:Label runat="server" ID="lblShowSelection" Text="{ show selection }" />
        </form>
    </body>
    </html>
    Code-behind:
    using System;
    using Ext.Net;
    
    namespace skai_maschinendatenbank_webapp.Pages
    {
    	public class UIDepartment
    	{
    		public long ID { get; set; }
    		public string Name { get; set; }
    		public string Abbrev { get; set; }
    		public int Indent { get; set; }
    
    		public UIDepartment(long id, string name, string abbrev, int indent)
    		{
    			this.ID = id;
    			this.Name = name;
    			this.Abbrev = abbrev;
    			this.Indent = indent;
    		}
    	}
    
    	// Source: https://examples2.ext.net/#/Form/ComboBox/Custom_Drop_Down_List/
    
    	public partial class TestCustomDropDownList : System.Web.UI.Page
    	{
    		protected void Page_Load(object sender, EventArgs e)
    		{
    			if (!X.IsAjaxRequest)
    			{
    				this.Store1.DataSource = new UIDepartment[4]
    				{
    					new UIDepartment(1L, "Head department", "IMPORTANT", 0),
    					new UIDepartment(2L, "Dev", "D", 1),
    					new UIDepartment(3L, "UI-Dev", "UI", 2),
    					new UIDepartment(4L, "Sales", "Sa", 1),
    				};
    				this.Store1.DataBind();
    
    				/* NOT WORKING: select initially on page-load: */
    				// this.mycombobox.Select(1); // 2nd item by index
    				// this.mycombobox.Select(2L); // 2nd item by value
    				// this.mycombobox.Value = 2L; // 2nd item by property-value
    			}
    
    			this.mycombobox.DirectEvents.Select.Event += Select_Event;
    		}
    
    		void Select_Event(object sender, DirectEventArgs e)
    		{
    			string selectedIDStr = this.mycombobox.SelectedItem.Value;
    			this.lblShowSelection.Text = DateTime.Now.ToLongTimeString() + " Select_Event -> " + selectedIDStr;
    		}
    
    		protected void btnSelect_DirectClick(object sender, Ext.Net.DirectEventArgs e)
    		{
    			long valueToSelect = 4L;
    			this.mycombobox.Select(valueToSelect);
    			this.lblShowSelection.Text = DateTime.Now.ToLongTimeString() + " btnSelect_DirectClick -> " + valueToSelect;
    		}
    
    		protected void btnShowSelect_DirectClick(object sender, Ext.Net.DirectEventArgs e)
    		{
    			string selectedIDStr = this.mycombobox.SelectedItem.Value;
    			this.lblShowSelection.Text = DateTime.Now.ToLongTimeString() + " btnShowSelect_DirectClick -> " + selectedIDStr;
    		}
    	}
    }
  2. #2
    Hello! Thanks for the runnable example! This makes it much easier to understand and help on the problem!

    Short answer is: use this.mycombobox.SelectedItem.Value = "2"; // 2L.ToString(); -- where 'value' is the ValueField on your ComboBox -- ID in the case.

    Long answer:
    The cause you can do the selection when you click a button after the page is loaded, but can't do it during page load is because of the state of the page during the command.

    On Page_Load where it is not a postback nor ajax request is when you initially load the page. It has no controls, no DOM, no data.

    At this point, you can interpret the controls as being set up. They do not exist yet, you are defining its properties, that will be used when the controls are rendered on the screen. So, there's no combo box at all, there's nothing to execute the select() action at this point!..

    By the other hand, on either the direct events -or- a Page_Load during PostBack or Ajax Request, you may already have the controls built and running. So, changing a setting governing how the component should be rendered has absolutely no effect -- unless you or something else redraws the control/component afterwards.

    And that is why you can't just interchange the selection approaches between what you already achieved (mycombobox.Select()) and what I've shown you above (mycombobox.SelectedItem.Value).

    That has much to do with the so-called ASP.NET Pages life cycle.

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

    solved :)

    Thanks a lot for the quick and detailed answer!!

    Now it works as aspected.


    I assumed that this.mycombobox.SelectedItem is null during Page_Load so I did not try to set this.mycombobox.SelectedItem.Value property.

Similar Threads

  1. [CLOSED] RowExpander initially expanded
    By barnali in forum 3.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 09, 2015, 4:37 PM
  2. [CLOSED] Problem with combobox when select a data from another combobox
    By opendat2000 in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Feb 25, 2015, 6:24 PM
  3. Replies: 2
    Last Post: May 05, 2011, 10:16 AM
  4. [CLOSED] PagingToolbar initially incorect
    By dev in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Aug 27, 2010, 9:00 AM
  5. tab content initially missing
    By [WP]joju in forum 1.x Help
    Replies: 6
    Last Post: Jan 14, 2010, 2:18 AM

Posting Permissions