Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to get the number of items of a ListBox

Status
Not open for further replies.

cesark

Programmer
Dec 20, 2003
621
The asp.net ListBox that I am trying to get the number of items on it, is populated with items on client side, with a javascript function, the items are there with its text and value, and the js function makes the items added in this ListBox selected by default. To count the items on it I don’ t know how I have to do it. Somebody can help me please?

I am trying to do it thus:
Request.Form("ListBox").Split(",").Length


And I receive this error:
Object reference not set to an instance of an object
Line 213: If (i > Request.Form("ListBox").Split(",").Length ) Then


If I try it thus:
ListBox.Items.Count

The (ListBox.Items.Count) value is always ‘0’ because, I suppose, none items are found, but the items are there with its text and value added by the js function.

How can I do it?

Thank you,
Cesar
 
Since you do the listbox update on the client side with jscript the server doesn't know about it.
Have you considered using a hidden field on the client filled by the jscript to hold this count so the server can read it?
 
If you're trying to get the number of items on a postback to do some sort of server-side processing, there are a couple things that you can do.

If you're using an HtmlControl, change it to a WebControl. This will make it easier to deal with in server-side code.

(<input type=listbox> ... or whatever it is... = HtmlControl)
(<asp:ListBox runat=server id=foo/> = WebControl)

Code:
<!-- HTML VIEW -->
<asp:ListBox runat=server id=foo/>

// Code-Behind
int _iLbCount = this.foo.Items.Count;

You will have to make one slight modification to your script code however...

Because of the way the .net webcontrol client-side naming works, the control's id will not be "foo" when it's rendered to the client. Due to this, wherever you reference the listbox in your client script, you will have to change document.foo (or however you're referencing it) to document.<%= this.foo.ClientID %>, which will render the proper ID for client-side processing.

Hope that's what you're looking for...

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
In fact, I am considering to use a hidden field as marinero said, because my items added in my ListBox are not selected (and I don' t want it).
Now my problem is how to fill this hidden field from the js function.

I have added the line in bold to my old function, but it doesn't work:
Code:
function copyOptionsSelected(from,to) {
 
 var options = new Object();
 
 for (var i=0;i<to.options.length;i++) {
  options[to.options[i].text] = true;}
  
 
 
 for (var i=0;i<from.options.length;i++) {
  var o = from.options[i];
  
   if (o.selected) { 
    if (options[o.text] == null || options[o.text] == "undefined") {
	 to.options[to.options.length] = new Option( o.text, o.value, false, false); 
 [B]document.form_newProduct.hidden1.options[document.form_newProduct.hidden1.options.length] = new Option(o.text, o.value, false, false);[/B]


     }
 
	 	 
  }
 }	
	  
   from.selectedIndex = -1; to.selectedIndex = -1;
   
}

The js error message says:
document.form_newProduct.hidden1 isn' t an object or it is null.

But the form' s name is 'form_newProduct' and the hiddenField' s name is 'hidden1'.. This is correct..
 
I would still strongly recommend doing it the way my post has it... It's the "proper" way of dealing with it in asp.net, but.. It's your project, of course :)

Try:

Code:
var _hidden = document.getElementById("hidden1");

// do all operations on _hidden object

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Just thought that I'd post this before anybody else did:

There are many different ways to approach a problem in engineering... Just because one way isn't as "pretty" as another, it doesn't mean that it won't work. Please disregard the comment in my last post about it being the "proper" way of dealing with it in asp.net. I've got the forum posting frustrations right now ;)

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
It does not matter! The different opinions are essential to choose 'my way' ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top