Add Items to combobox dynamically problem

  1. #1

    Add Items to combobox dynamically problem



    Hi,

    I have a next problem..
    I want to fill the combobox with items.. but I can do this only in page load... I need fill new items when AjaxEvent happen..
    I prepare little example of my problem.

    
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test_combobox.aspx.vb" Inherits="test_combobox" %>
    
    <%@ Register assembly="Coolite.Ext.Web" namespace="Coolite.Ext.Web" tagprefix="ext" %>
    
    <!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>Untitled Page</title>
    </head>
    <body>
    *   <form id="form1" runat="server">
    *   
    *       <ext:ScriptManager ID="ScriptManager1" runat="server" />
    *   
    
    *   <ext:Panel ID="Panel1" runat="server" Height="300" Title="Title">
    *       <Body>
    *           <ext:ComboBox ID="ComboBox1" runat="server">
    *           </ext:ComboBox>
    *           <ext:Button ID="Button1" runat="server" Text="Submit">
    *           <AjaxEvents>
    *           <Click OnEvent="mmks_fillcombobox">
    *           </Click>
    *           </AjaxEvents>
    *           </ext:Button>
    *          *
    *       </Body>
    *   </ext:Panel>
    *   </form>
    </body>
    </html>
    and simple method to refill combo is*

    
    Partial Class test_combobox
    *   Inherits System.Web.UI.Page
    
    *   Protected Sub mmks_fillcombobox()
    *       Dim lni As Integer
    *       For lni = 1 To 10
    *           Me.ComboBox1.Items.Add(New Coolite.Ext.Web.ListItem("Page " + lni.ToString, lni.ToString))
    *       Next
    *   End Sub
    End Class
    When I try to expand combobox - I'v got a JavaScript error..

    Microsoft JScript runtime error "This.store" is null or not an object*



    I look examples.. and everywhere using Store object.. can I avoid to use store object and fill items with Items.add() in the code?

    Thank you!
  2. #2

    RE: Add Items to combobox dynamically problem



    Hi,

    I understand about - no ways to add during AjaxEvent items to the combobox. I try to use Store as it shows on example.. and I have NO SUCCESS...

    In the example we have a code

    
    *          List<object> data = new List<object>();
    
    *           foreach (XmlNode cityNode in xmlDoc.SelectNodes(string.Concat("countries/country[@code='", this.Countries.SelectedItem.Value, "']/city")))
    *           {
    *               string id = cityNode.SelectSingleNode("id").InnerText;
    *               string name = cityNode.SelectSingleNode("name").InnerText;
    
    *               data.Add(new{Id=id, Name = name});
    *           }
    *

    There we can see - we add one "Anonymous type" to the List collection.. I try to do same using VB.NET and I have not success.. the items are here.. but all empty

    
    *       Dim loObjects As List(Of Object) = New List(Of Object)
    *       loObjects.Add(New With {.id = "1", .name = "test"})
    
    *       Me.Store1.DataSource = loObjects
    *       Me.Store1.DataBind()


    So far as I seen.. in Debuger Watch window - the Anonymous type for C# and VB.NET is not a SAME.. so VB has a middle layer.. with "anonymous type" object.. and C# has properties id and name directy for item.

    So.. I second day try to solve this problem and have not success with Store ..

    Please Help!

  3. #3

    RE: Add Items to combobox dynamically problem

    Hi,

    What problems do you have? Can you show your code?
    You need use store to rebind combo data during AjaxEvent.
    For example add OnRefreshData handler for Store and on client side call

    js code
    Store1.reload();
  4. #4

    RE: Add Items to combobox dynamically problem



    Hi Vlad,

    Thanks for response.. here is a code...*

    The task background was.. When I select record in DataGrid.. then AjaxEvent executed.. then I need update a combobox..

    (I get xml from SQL Database.. by Id and then Fill combobox with items from this xml)*

    Here just an example. what I want to do...*

    Thank you very much for your answers!

    
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test_combobox.aspx.vb" Inherits="test_combobox" %>
    
    <%@ Register assembly="Coolite.Ext.Web" namespace="Coolite.Ext.Web" tagprefix="ext" %>
    
    <!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>Untitled Page</title>
    </head>
    <body>
    *   <form id="form1" runat="server">
    *   
    *       <ext:ScriptManager ID="ScriptManager1" runat="server" />
    *   
    
    *   <asp:XmlDataSource ID="XmlDataSource1" runat="server">
    *       <Data>
    *           <row>
    *               <id>1</id>
    *               <name>Page 1</name>
    *           </row>
    *       </Data>
    *   </asp:XmlDataSource>
    *   <ext:Store runat="server" ID="Store1" AutoLoad="false">
    *       <AjaxEventConfig>
    *           <EventMask ShowMask="false" />
    *       </AjaxEventConfig>
    *       <Reader>
    *           <ext:JsonReader ReaderID="id" >
    *               <Fields>
    *                   <ext:RecordField Name="id" Type="String"  />
    *                   <ext:RecordField Name="name" Type="String" />
    *               </Fields>
    *           </ext:JsonReader>
    *       </Reader>
    *   </ext:Store>  *
    *   <ext:Panel ID="Panel1" runat="server" Height="300" Title="Title">
    *       <Body>
    *           <ext:ComboBox ID="ComboBox1" runat="server" StoreID="Store1"
    *           TypeAhead="true" 
    *           Mode="Local"
    *           ForceSelection="true" 
    *           TriggerAction="All" 
    *           DisplayField="name" 
    *           ValueField="id"
    *           EmptyText="Loading..." 
    *           ValueNotFoundText="Loading...">
    *           </ext:ComboBox>
    *           <ext:Button ID="Button1" runat="server" Text="Submit">
    *           <AjaxEvents>
    *           <Click OnEvent="mmks_fillcombobox">
    *           </Click>
    *           </AjaxEvents>
    *           </ext:Button>
    *          *
    *          *
    *          *
    *       </Body>
    *   </ext:Panel>
    *   </form>
    </body>
    </html>


    
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Data
    
    
    Partial Class test_combobox
    *   Inherits System.Web.UI.Page
    
    *   Protected Sub mmks_fillcombobox()
    *       Dim loObjects As List(Of Object) = New List(Of Object)
    *       loObjects.Add(New With {.id = "1", .name = "test"})
    *       Me.Store1.DataSource = loObjects
    *       Me.Store1.DataBind()
    *   End Sub
    End Class
    *
  5. #5

    RE: Add Items to combobox dynamically problem

    Hi,

    I think you need to define correct handler signature. You need to add two arguments to the AjaxEvent handler

    (sender As Object, e As Coolite.Ext.Web.AjaxEventArgs)
    
    The must have the same signature as delegate
    Delegate Sub AjaxEventHandler(sender As Object, e As Coolite.Ext.Web.AjaxEventArgs)
  6. #6

    RE: Add Items to combobox dynamically problem



    Hi Vlad!

    Thank you for solution.. you are right.. now the combobox filled with items.

    But new problem appears .. Now I can't set the default value for combobox in this method..



    
    *       Dim loObjects As List(Of Object) = New List(Of Object)
    *       loObjects.Add(New With {.id = "1", .name = "test"})
    
    *       Me.Store1.DataSource = loObjects
    *       Me.Store1.DataBind()
    * Me.cboPages.SetValue("1")


    And the value still empty.. why? What do I wrong?

    *
  7. #7

    RE: Add Items to combobox dynamically problem

    Hi,

    SetValue method using during AjaxEvent only. In your case need use*SelectedItem*property
    ComboBox1.SelectedItem.Value = "1";
    *
  8. #8

    RE: Add Items to combobox dynamically problem

    Thank you for the best support!*

Similar Threads

  1. [CLOSED] Problem selecting items in Combobox
    By tlfdesarrollo in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Jan 17, 2012, 6:57 PM
  2. Add items to menu dynamically
    By sunshine in forum 1.x Help
    Replies: 5
    Last Post: Mar 10, 2011, 6:44 PM
  3. Problem about change ComboBox's store dynamically
    By zhangsir199 in forum 1.x Help
    Replies: 0
    Last Post: Aug 04, 2010, 2:46 AM
  4. Replies: 3
    Last Post: Jul 24, 2010, 4:31 PM
  5. Replies: 1
    Last Post: Apr 15, 2010, 8:07 PM

Posting Permissions