What is the problem? JavaScript - ListBox

S

Steven M

Hello!!!

I dont know what is the problem!!! I am desperate!!! I want to put
enabled = false a button when in ListBox has selected more than a
item.

this is the javascript.
it dosen't Work!!!!

<script language =javascript>
function JavaListBox()
{
var Contador = 0;
for (var i=0;i< document.all('ListBox1').options.length;i++)
{
if (document.all('ListBox1').options.selected == true)
{
Contador++;
}
}
if (Contador > 1)
{
document.all('btnMio').disabled = true;
}
else
{
document.all('btnMio').disabled = false;
}
}
</script>


here is the real code (codebehind)


private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterStartupScript("JListBox",JavaScriptListBox());
}

private void ListBox1_PreRender(object sender, System.EventArgs e)
{
ListBox1.Attributes.Add("OnClick", "JavaListBox()");
}


private string JavaScriptListBox()
{
StringBuilder strMetodo = new StringBuilder();
strMetodo.Append("<script language=\"javascript\">");
strMetodo.Append("function JavaListBox(){");
strMetodo.Append("var Contador = 0;");
strMetodo.Append("for (var i=0;i<
document.all('ListBox1').options.length;i++)");
strMetodo.Append("{");
strMetodo.Append("alert('Entro For');");
strMetodo.Append("if (document.all('ListBox1').options.selected ==
true)");
strMetodo.Append("{");
strMetodo.Append("Contador++;");
strMetodo.Append("}");
strMetodo.Append("}");
strMetodo.Append("if (Contador > 1){ document.all('btnMio').disabled =
true}");
strMetodo.Append("else {document.all('btnMio').disabled = false}");
strMetodo.Append("}");
strMetodo.Append("</script>");
return strMetodo.ToString();
}
 
K

Karl Seguin

You probably want to use the onChange event instead of onClick. It'll work
with OnClick but oddly (only when the select is clicked). That's assuming
you have all the right id's

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top