[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.
Re: [2005] List Box's Count property
Why can't you just use...
Code:
document.Form1.lstGroups.options.add(y);
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.
Re: [2005] List Box's Count property
Oh sorry i misunderstood lol here use this ;)
Edit:
Scratch i misunderstood the question yet again -.-
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!!!!
Re: [2005] List Box's Count property
Re: [2005] List Box's Count property
Let's see how you're trying to count it. C0d plz.
Re: [2005] List Box's Count property
I am fetching the count through code behind and not through javascript.
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!!!
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. :thumb:
Hope this helps,
Pradeep :)
Re: [2005] List Box's Count property
That's a great help Pradeep. I will try this. Thanks for the help
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.