Why does this code NOT execute ?

A

Alex

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
 
V

VK

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.
 
T

Thomas 'PointedEars' Lahn

Alex said:
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
 
J

Joost Diepenmaat

Randy Webb said:
Thomas 'PointedEars' Lahn said the following on 1/30/2008 3:47 PM:

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.
 

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,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top