Why does this code NOT execute ?

Discussion in 'Javascript' started by Alex, Jan 29, 2008.

  1. Alex

    Alex Guest

    Hello, all.

    Big question (for my very limited knowledge) - What's wrong with
    this ? No error, but nothing happens:

    HTML......................
    <head runat="server">
    <title>Test Page</title>
    <script>
    function radioButtonList2Click()
    {
    document.form1.radioButtonList1.SelectedIndex = -1;
    }
    </script>
    <script>
    function radioButtonList1Click()
    {
    document.form1.radioButtonList2.SelectedIndex = -1;
    }
    </script>
    </head>


    and

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    if (radioButtonList1.Attributes["OnSelectedIndexChanged"] == null)
    {
    radioButtonList1.Attributes.Add("OnSelectedIndexChanged",
    "javascript:radioButtonList1Click()");
    }

    if (radioButtonList2.Attributes["OnSelectedIndexChanged"] == null)
    {
    radioButtonList2.Attributes.Add("OnSelectedIndexChanged",
    "javascript:radioButtonList2Click()");
    }
    }
    }

    Thanks a lot
    Alex
    Alex, Jan 29, 2008
    #1
    1. Advertising

  2. Alex

    VK Guest

    On Jan 29, 11:20 pm, Alex <> wrote:
    > Hello, all.
    >
    > Big question (for my very limited knowledge) - What's wrong with
    > this ? No error, but nothing happens:
    >
    > HTML......................
    > <head runat="server">
    > <title>Test Page</title>
    > <script>
    > function radioButtonList2Click()
    > {
    > document.form1.radioButtonList1.SelectedIndex = -1;
    > }
    > </script>
    > <script>
    > function radioButtonList1Click()
    > {
    > document.form1.radioButtonList2.SelectedIndex = -1;
    > }
    > </script>
    > </head>
    >
    > and
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!Page.IsPostBack)
    > {
    > if (radioButtonList1.Attributes["OnSelectedIndexChanged"] == null)
    > {
    > radioButtonList1.Attributes.Add("OnSelectedIndexChanged",
    > "javascript:radioButtonList1Click()");
    > }
    >
    > if (radioButtonList2.Attributes["OnSelectedIndexChanged"] == null)
    > {
    > radioButtonList2.Attributes.Add("OnSelectedIndexChanged",
    > "javascript:radioButtonList2Click()");
    > }
    > }
    > }
    >
    > Thanks a lot
    > Alex


    What language is that? It is not Javascript for sure. Looks a bit like
    Microsoft JScript 7.0/8.0 with non-existing DOM methods. If it's one
    of Microsoft ASP jargons, you may ask at
    microsoft.public.scripting.jscript
    If it is intended to be an universal Javascript code then please
    explain the intended behavior and let's start to clean up.
    VK, Jan 29, 2008
    #2
    1. Advertising

  3. Alex wrote:
    > Big question (for my very limited knowledge) - What's wrong with
    > this ? No error, but nothing happens:
    >
    > HTML......................
    > <head runat="server">


    This may be one reason why nothing happens. Try removing the `runat'
    attribute so that the scripts in the `head' element are executed client-side.

    > <title>Test Page</title>


    http://www.w3.org/QA/Tips/good-titles

    > <script>


    <script type="text/javascript">

    See http://validator.w3.org/

    > function radioButtonList2Click()
    > {
    > document.form1.radioButtonList1.SelectedIndex = -1;


    This is probably another reason why nothing happens. Client-side properties
    are more often case-sensitive (in ASP, server-side properties more often are
    not, for compatibility to VBScript):

    ... .selectedIndex = -1;

    If you use `.SelectedIndex' instead, the control object is added a
    user-defined property that is not part of the DOM and therefore not
    evaluated by the layout engine.

    > [...]
    > function radioButtonList1Click()
    > {
    > document.form1.radioButtonList2.SelectedIndex = -1;
    > }


    Same here.

    > [...]
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!Page.IsPostBack)
    > {
    > if (radioButtonList1.Attributes["OnSelectedIndexChanged"] == null)
    > {
    > radioButtonList1.Attributes.Add("OnSelectedIndexChanged",
    > "javascript:radioButtonList1Click()");
    > }
    >
    > if (radioButtonList2.Attributes["OnSelectedIndexChanged"] == null)
    > {
    > radioButtonList2.Attributes.Add("OnSelectedIndexChanged",
    > "javascript:radioButtonList2Click()");
    > }
    > }
    > }


    This is C# or maybe J# code in ASP .NET that generates elements which
    have event listeners attached to execute the above JScript code.
    `OnSelectedIndexChanged' is a server-side event handler attribute of
    Microsoft Web Controls.

    http://msdn2.microsoft.com/en-us/library/system.web.ui.page(VS.80).aspx

    Probably `javascript:' should be omitted here, see the FAQ.


    HTH

    PointedEars
    Thomas 'PointedEars' Lahn, Jan 30, 2008
    #3
  4. Randy Webb <> writes:

    > Thomas 'PointedEars' Lahn said the following on 1/30/2008 3:47 PM:
    >> Alex wrote:
    >>> Big question (for my very limited knowledge) - What's wrong with
    >>> this ? No error, but nothing happens:
    >>>
    >>> HTML......................
    >>> <head runat="server">

    >>
    >> This may be one reason why nothing happens. Try removing the `runat'
    >> attribute so that the scripts in the `head' element are executed client-side.

    >
    > Even you should know that if that made it to the client, then the
    > client ignores any attribute it doesn't understand and runat is not a
    > valid attribute for the head element so it gets ignored. Has nothing
    > to do with whether code gets executed on the client or not.


    except that runat="server" is a server directive (for MS ISS I think),
    which means that it could possibly prevent the code to reach the client.

    I'm not sure what that code is supposed to do or even if it's
    supposed to be JavaScript, though.

    Joost.
    Joost Diepenmaat, Jan 30, 2008
    #4
    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. Brian Miller
    Replies:
    6
    Views:
    2,924
    Brian Miller
    Nov 27, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    455
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    874
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,803
    Smokey Grindel
    Dec 2, 2006
  5. Troy

    User control code behind does not execute

    Troy, Oct 22, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    127
    Victor Garcia Aprea [MVP]
    Oct 23, 2003
Loading...

Share This Page