[CLOSED] MultiSelect indexof listitem always null

  1. #1

    [CLOSED] MultiSelect indexof listitem always null

    Hi,
    When button click, to find the index of multiselect
    It's alway return null(-1).
    How to resolve it??

    <%@ Page Language="C#" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protected void getitem(object sender, DirectEventArgs e)
        {
            Ext.Net.ListItem subitem = new Ext.Net.ListItem();
            subitem.Text = "item1";
            if (MultiSelect1.Items.IndexOf(subitem) > -1)
                Label1.Text = " indexif:" + MultiSelect1.Items.IndexOf(subitem).ToString();
            else
                Label1.Text = " indexif:-1";
              
        }
    </script>
    
    
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <ext:ResourceManager ID="ResourceManager1" runat="server">
            </ext:ResourceManager>
            <ext:MultiSelect ID="MultiSelect1" runat="server">
               <Items>
                  <ext:ListItem Text="item1">
                  </ext:ListItem>
                  <ext:ListItem Text="item2">
                  </ext:ListItem>
                  <ext:ListItem Text="item3">
                  </ext:ListItem>
                  <ext:ListItem Text="item4">
                  </ext:ListItem>
               </Items>
            </ext:MultiSelect>
            <ext:Button ID="Button1" runat="server" Text="get item">
               <DirectEvents>
                  <Click OnEvent="getitem"></Click>
               </DirectEvents>
            </ext:Button>
            <ext:Label ID="Label1" runat="server">
            </ext:Label>
        </div>
        </form>
    </body>
    </html>
    Last edited by Daniil; Jul 13, 2012 at 7:26 PM. Reason: [CLOSED]
  2. #2
    Hi,

    To get it working the Comparer should be overrode.

    We would suggest you to use lambda expressions to achieve the requirement.
  3. #3
    Please provide examples,thanks again.

    Quote Originally Posted by Daniil View Post
    Hi,

    To get it working the Comparer should be overrode.

    We would suggest you to use lambda expressions to achieve the requirement.
  4. #4
    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void GetItem(object sender, DirectEventArgs e)
        {
            Ext.Net.ListItem subItem = this.MultiSelect1.Items.First<Ext.Net.ListItem>(item => item.Text == "item1");
    
            if (subItem != null)
            {
                this.Label1.Text = "found: " + subItem.Text;
            }
            else
            {
                this.Label1.Text = "not found";
            }
        }
    </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 v2 Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
            <ext:MultiSelect ID="MultiSelect1" runat="server">
                <Items>
                    <ext:ListItem Text="item1" />
                    <ext:ListItem Text="item2" />
                </Items>
            </ext:MultiSelect>
    
            <ext:Button runat="server" Text="Get item" OnDirectClick="GetItem" />
    
            <ext:Label ID="Label1" runat="server" />
        </form>
    </body>
    </html>
  5. #5

    MultiSelect add listitem codebehind, but MultiSelect.Items.Count alway 0...

    Hi

    I add listitem in MultiSelect codebehind,The multiselect show listitem work well.
    but when click other button to get Items.count,the multiselect has listitems,but count return 0?????

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
       protected void getcount(object sender, DirectEventArgs e)
        {
    
            for (int i = 1; i < 4; i++)
            {
                Ext.Net.ListItem subitem = new Ext.Net.ListItem();
                subitem.Text = "item"+i.ToString();
                MultiSelect1.Items.Add(subitem);
            }
            MultiSelect1.Render();
            
        }
        protected void getcount1(object sender, DirectEventArgs e)
        {
    
            Label1.Text = "count:" + MultiSelect1.Items.Count.ToString();
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <ext:ResourceManager ID="ResourceManager1" runat="server">
            </ext:ResourceManager>
            <ext:MultiSelect ID="MultiSelect1" runat="server" FieldLabel="" DragGroup="grp2"
                DropGroup="grp2">
                <Items>
                </Items>
            </ext:MultiSelect>
            <ext:Button ID="Button1" runat="server" Text="1.Click  to set MutltiSelect">
                <DirectEvents>
                    <Click OnEvent="getcount">
                    </Click>
                </DirectEvents>
            </ext:Button>
            <ext:Label ID="Label1" runat="server">
            </ext:Label>
            <ext:Button ID="Button2" runat="server" Text="2.Click to get MultiSelect counts">
                <DirectEvents>
                    <Click OnEvent="getcount1">
                    </Click>
                </DirectEvents>
            </ext:Button>
        </div>
        </form>
    </body>
    </html>
  6. #6
    If you create something within the DirectEvent/DirectMethod, it won't be recreated automatically during another request.

    If you need to use Items during another request, you should recreate them manually.

    If you could clarify the initial requirement, we would try to suggest something better/easier.
  7. #7
    I finally understand why every time I add listitems must use the render to refresh screen.
    I don't know how to recreate them manually.
    My requirment is provide three checkbox to users, when user checked, checkbox value add to multiselect
    User can change sequence from multiselect
    I have to save multiselect item to database. I use for loop to get multiselect item.
    for loop can not work becasue items.count always zero.

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        protected void setMultiSelect(object sender, DirectEventArgs e)
        {
            if (Ck1.Checked == true)
            {
                Ext.Net.ListItem subitem = new Ext.Net.ListItem();
                subitem.Text = "item1" ;
                MultiSelect1.Items.Add(subitem);          
            }
    
            if (Ck2.Checked == true)
            {
                Ext.Net.ListItem subitem = new Ext.Net.ListItem();
                subitem.Text = "item2" ;
                MultiSelect1.Items.Add(subitem);
            }
    
            if (Ck3.Checked == true)
            {
                Ext.Net.ListItem subitem = new Ext.Net.ListItem();
                subitem.Text = "item3" ;
                MultiSelect1.Items.Add(subitem);
            }
            
            MultiSelect1.Render();
            //I add checkbox to multiselect,user can change sequence in multiselect 
            
            
        }
        protected void SaveData(object sender, DirectEventArgs e)
        {
            //I have to get the multiSelect items, but items.counts always 0
            for (int i = 0; i < MultiSelect1.Items.Count; i++)
            {
               // save data to db
                
            }
            
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <ext:ResourceManager ID="ResourceManager1" runat="server">
            </ext:ResourceManager>
            <ext:Checkbox ID="Ck1" runat="server" FieldLabel="ITEM1">
               <DirectEvents>
                  <Change OnEvent="setMultiSelect"></Change>
               </DirectEvents>
            </ext:Checkbox>
            <ext:Checkbox ID="Ck2" runat="server" FieldLabel="ITEM2">
                <DirectEvents>
                    <Change OnEvent="setMultiSelect">
                    </Change>
                </DirectEvents>
            </ext:Checkbox>
            <ext:Checkbox ID="Ck3" runat="server" FieldLabel="ITEM3">
                <DirectEvents>
                    <Change OnEvent="setMultiSelect">
                    </Change>
                </DirectEvents>
            </ext:Checkbox>
         
            <ext:MultiSelect ID="MultiSelect1" runat="server" FieldLabel="" DragGroup="grp2" DropGroup="grp2">
                <Items>
                </Items>
            </ext:MultiSelect>
            <ext:Button ID="Button1" runat="server" Text="Save Data">
                <DirectEvents>
                    <Click OnEvent="SaveData">
                    </Click>
                </DirectEvents>
            </ext:Button>
        </div>
        </form>
    </body>
    </html>
  8. #8
    Thanks for the clarification. But the answer is still the same:

    Quote Originally Posted by Daniil View Post
    If you create something within the DirectEvent/DirectMethod, it won't be recreated automatically during another request.

    If you need to use Items during another request, you should recreate them manually.
    You should recreate these Items or send them from client side as a extra parameter of the DirectEvent.

    Here is the example how to submit MultiSelect values.
    https://examples2.ext.net/#/Form/Mul...t/Submit_Data/
  9. #9
    I actually did not think to use ExtraParams.
    that's a good idea for me.
    thanks again,again,again


    Quote Originally Posted by Daniil View Post
    Thanks for the clarification. But the answer is still the same:



    You should recreate these Items or send them from client side as a extra parameter of the DirectEvent.

    Here is the example how to submit MultiSelect values.
    https://examples2.ext.net/#/Form/Mul...t/Submit_Data/

Similar Threads

  1. [CLOSED] ComboBox empty ListItem
    By methode in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Nov 18, 2013, 4:01 AM
  2. [CLOSED] Add listitem to multiselect use javascript.
    By gs_user in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Jul 13, 2012, 7:49 AM
  3. Replies: 4
    Last Post: May 09, 2012, 9:24 PM
  4. [CLOSED] Displaying <, >, [, ] in ListItem
    By pj_martins in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Dec 16, 2011, 5:19 PM
  5. Replies: 1
    Last Post: Jun 08, 2010, 11:38 AM

Posting Permissions