Problem with listbox at client side

  • Thread starter Thread starter Anup
  • Start date Start date
A

Anup

Hi Group,

In my application there is a very common functionality, where two list
box and present and items are moved between them on button clicks when
selected,

This I am doing client side,
Client side code is like this ->

function
assignInspector(listboxAllFieldEngineers,listBoxAssignedFieldEngineers)
{
i=0;
while(i<listboxAllFieldEngineers.length)
{
if(listboxAllFieldEngineers.options.selected)
{
var optn = document.createElement("OPTION");
optn.text = listboxAllFieldEngineers.options.value;
optn.value = listboxAllFieldEngineers.options.value;
listBoxAssignedFieldEngineers.options.add(optn);
listboxAllFieldEngineers.remove(i);
continue;
}
i++;
}
return false;
}


Here two list boxes are passed from .CS file ->

buttonAddSelected.Attributes.Add("onclick","return
assignInspector(listboxAllFieldEngineers,listBoxAssignedFieldEngineers);");



PROBELM:-

Items move from one listbox to other perfectly but when listboxes are
accessed through .cs file, they show there previous state,

e.g. ->

say LeftListBox is having 4 items and RightListBox is having 2
elements,
now I select and move two items from left and move them ro right and
one of previously present items from right to left on button click,

the client side operation is carried out perfectly,

But when I try to use the Items in RightListBox in .CS
file,RightListBox.Items.Count returns only '2' instead of '3', which
are previously present in the RightListBox.


Please tell me where I am going wrong(Using ASP.NET 1.x).


Thanks and Regards,

Anup Daware
 
You might be loading data on load ...try filterering on IsPostBack
....also please check viewstate of the controls

- hungrymind
 
thank you for quick reply,
But, viewstste is true for listboxes and leftside list box list-box is
populated in if( !PostBack )
please let me know if you can suggest any thing else..

Thanks and Regards,

Anup Daware
 
Hi,

IMO you will have to do all this client side, so use a regular html SELECT
(you can still use a listbox )
This is what I would do:
1- Assign the values in the server side as usual
2- in the client the onclick of the button will call a jscript method that
will delete/insert an item from one listbox to the other.
3- before postback create a string concatenating the values of either both
or one of the listboxes. Usually using a HIDDEN control
4- in the server side use this string to know the content of one or both of
the listboxes


I have done this several times in ASP
 
Hi Ignacio,

This is really a very useful post, but I have some confusion, I am new
to ASP.NET and I have little difficulty in using hidden controls, I
mean how do I store the values at client side and pass them to server
using hidden controls?

Please provide some sample code you used if possible.

Thanks,
Anup
 
Hi,

Anup said:
Hi Ignacio,

This is really a very useful post, but I have some confusion, I am new
to ASP.NET and I have little difficulty in using hidden controls, I
mean how do I store the values at client side and pass them to server
using hidden controls?

you declare it like this in the page:
<input type="hidden" name="MinDate" id="MinDate" value="01/01/2003"
runat=server>

and like this in the code behind:

protected System.Web.UI.HtmlControls.HtmlInputHidden MinDate;


then you can access the value using MinDate.Value

before you submit your form you build the value of the control and it will
be accesible in the code behind.

Sorry I cannot provide you with a working example right now, I will have to
look around to find one, I have use this technique in several instances,
maybe later today I can provide you with one.
 
Back
Top