PDA

View Full Version : String Comparison Not Working in Lisbox servercontrol


venki.ffcl
06-08-2009, 02:28 PM
Dear friends,

Here i have a problem when am comparing two string values.

1. Am using a ListBox Control with the name "LB"
2. Am getting user input using a textbox control named "txtname"
3. The condition is not satisfied while am comparing the listbox values by textbox values. But both the returned values are same when checked by alert.


Note : I need to insert the user Input if it is not exists in the listbox.

Please anyone guide me


Code Used
----------
function OnGetHelloDetails(result) {
if (!result) {
if ($get('<%=LB.ClientID %>').length > 0) {
for (var i = 0; i < $get('<%=LB.ClientID %>').length; i++) {
if (($get('<%=LB.ClientID %>').options[i].value) == ($get('txtname').value)) {
break;
}
else {
var opt = document.createElement('OPTION');
opt.text = $get('txtname').value;
opt.value = $get('txtname').value;
$get('<%=LB.ClientID %>').options.add(opt);
}
}
}
else {
var opt = document.createElement('OPTION');
opt.text = $get('txtname').value;
opt.value = $get('txtname').value;
$get('<%=LB.ClientID %>').options.add(opt);
}
}
}

A1ien51
06-09-2009, 01:52 PM
It is better if you show use the generated markup and not the serverside code. View page source and show us that.

Have you debugged this:
if (($get('<%=LB.ClientID %>').options[i].value) == ($get('txtname').value)) {

Whitespace? Case? ETC. alerts [ or console.log in Firebug] will be your friend


Also for a performance thing

store $get('txtname').value) into a variable one time before the loop and use it. You are forcing your code to walk the DOM everytime which is slow

Eric