What is the problem? JavaScript - ListBox

Discussion in 'ASP .Net' started by Steven M, Feb 7, 2005.

  1. Steven M

    Steven M Guest

    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();
    }
     
    Steven M, Feb 7, 2005
    #1
    1. Advertising

  2. Steven M

    Karl Seguin Guest

    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

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Steven M" <> wrote in message
    news:...
    > 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();
    > }
     
    Karl Seguin, Feb 7, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. blenderdude
    Replies:
    0
    Views:
    1,105
    blenderdude
    Aug 3, 2003
  2. DC Gringo
    Replies:
    0
    Views:
    413
    DC Gringo
    Apr 6, 2004
  3. kent
    Replies:
    1
    Views:
    894
    Andrew de la Harpe
    May 3, 2004
  4. Simon Prince
    Replies:
    2
    Views:
    3,355
    =?Utf-8?B?TWljaGFlbCBUYXlsb3I=?=
    Oct 19, 2004
  5. mc
    Replies:
    0
    Views:
    244
Loading...

Share This Page