Results 1 to 11 of 11

Thread: [2005] List Box's Count property

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Arrow [2005] List Box's Count property

    Hi all,

    By mistake I posted my query in VB.Net Forum. So here posting it again

    Hi all,

    I am adding items to my ListBox in parent window from a child window through javascript. Here's the code:


    Code:
    var id=document.getElementById('<%=lstGroups.ClientID%>');
    var y=document.createElement('option');
    y.text=arrGroup[i];
    y.value=arrGroup[i];
    id.options.add(y);
    But on the button click on the parent window, the ListBox's Count property gives me a value of zero.

    I faild to understand why this is happening!!!

    Pls guide.
    Microsoft Techie

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2005] List Box's Count property

    Why can't you just use...

    Code:
    document.Form1.lstGroups.options.add(y);

  3. #3

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [2005] List Box's Count property

    What difference does it make??? Its one and the same. Through my code, I can see the values added in my ListBox. But the count property is giving me the wrong response.
    Microsoft Techie

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2005] List Box's Count property

    Oh sorry i misunderstood lol here use this

    Edit:

    Scratch i misunderstood the question yet again -.-
    Last edited by Paul M; Mar 13th, 2008 at 08:05 AM.

  5. #5

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [2005] List Box's Count property

    No Paul... I am not looking for that

    One more finding about my code - the code is working on the other page having DropDownList. But its not working for ListBox!!!!
    Microsoft Techie

  6. #6

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [2005] List Box's Count property

    bump
    Microsoft Techie

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] List Box's Count property

    Let's see how you're trying to count it. C0d plz.

  8. #8

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [2005] List Box's Count property

    I am fetching the count through code behind and not through javascript.

    vb Code:
    1. lstGroups.Items.Count

    I tried to debug the application. My Save button is a server side control. Even before the code of Save button execute or even before the page's Postback the lstGroups.Items.Count show me zero value. This really amuses me!!!
    Microsoft Techie

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2005] List Box's Count property

    There is nothing wrong with your javascript. The problem is with the asp.net engine.
    ListItems added to listbox, dropdowns etc. are not recoginzed by the server side code. The server side seems to trust the ViewState more than javascript; and there's no way to easily modify the ViewState.
    I hope ms will correct this in the next version, but till then you must live on workarounds only.

    Here is a workaround to your problem that will trick the server to recognize the ListItems added dynamically from clientside javascript:

    Add this javascript function to your page:
    Code:
    //Select all ListItems in the ListBox
    function SelectAllListItems(Ctl){
        var listbox = document.getElementById(Ctl);
        if(listbox == null) return false;
        for (var i=0;i<=listbox.length-1;i++){
            listbox.options[i].selected=true;
        }
        return false;
    }
    Modify your server side code in the following manner:

    Code:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If IsPostBack Then ResyncAllListBoxes()
            Me.Form.Attributes.Add("onsubmit", "SelectAllListItems('MyListBox');")
            '----------------------
    
            '.......Your other Page_Load code goes here.....
    
        End Sub
    
        Private Sub ResyncAllListBoxes()
            MyListBox.Items.Clear()
            If Request("MyListBox") <> "" Then
                Dim MyListItems as String = Request("MyListBox")            
                ' You will get a string with all ListItems seperated by comma in MyListItems
                ' Split the string and fill the ListBox again here.
    
                ' You already know the code, don't you :)
    
            End If
    
            '** Repeat for all such listboxes **
    
        End Sub
    This worked for me, so it must work for you also.

    Hope this helps,

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [2005] List Box's Count property

    That's a great help Pradeep. I will try this. Thanks for the help

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] List Box's Count property

    Javascript modified your DOM, but that change isn't reflected until you yourself persist those changes. This isn't specific to ASP.NET, but is a fundamental feature of all server side technologies - the DOM is irrelevant unless it was rendered by the script in the first place. Hence, you look for workarounds. Pradeep has shown you how to deal with the listbox items, assuming that the items you added via javascript are submitted as part of the request as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width