OnSelectedIndexChanged for a Drop Down List

OnTheAnvil

Regular
Joined
Nov 4, 2003
Messages
92
Location
Columbus, Ohio, USA
I'd like to execute some javascript code when the user changes the value of a drop down list on my web page but I keep getting an error. Any thoughts?

Code on my page:

<script language="javascript">
function ddlMedicalErrorType_OnSelectedIndexChanged()
{
alert("HELLO");
}
</script>

.
.
.

<asp:dropdownlist id="ddlMedicalErrorType" style="Z-INDEX: 138; LEFT: 480px; POSITION: absolute; TOP: 256px" runat="server" Width="128px" OnSelectedIndexChanged="ddlMedicalErrorType_OnSelectedIndexChanged();">


The error that I get is:

Compiler Error Message: BC30456: 'ddlMedicalErrorType_OnSelectedIndexChanged' is not a member of 'ASP.IncidentLog_aspx'.


I'd appreciate anything I can get.

Thanks,
OnTheAnvil
 
I think ddlMedicalErrorType_OnSelectedIndexChanged
should be in your code behind and from there, call the javascript.
 
are u using VB.net or C#? where do you put your code? all in HTML? if so, why?? why not take advantage of ASP.Net's code behind?
 
I'm using VB .NET and I'm not familiar with the code behind concept. Could you describe what you mean by code behind? The only way I've ever added javascript is by directly editing the HTML code on the .aspx page.

Thanks
 
I;d say you need to read up on asp.net..

there are 2 pages: xxx.aspx and xxx.aspx.vb

xxx.aspx.vb holds the code behind..the code written in VB.
xxx.aspx holds the HTML stuff.
 
It looks like we've been talking about the same thing in different terms. So back to my original question. The OnSelectedIndexChange event calls the ddlMedicalErrorType_OnSelectedIndexChanged() javascript function in the code behind. But when it compiles I get the error message. Does that help clarify my question?
 
On The Anvil,

You need to add "javascript:" to the front of your function call to let the framework know that you are intending to call a javascript function. Otherwise, it assumes that you are calling a VB.net or C#.net function, depending on the language you are using.

Like this:
<asp:dropdownlist id="ddlMedicalErrorType" style="Z-INDEX: 138; LEFT: 480px; POSITION: absolute; TOP: 256px" runat="server" Width="128px" OnSelectedIndexChanged="javascript:ddlMedicalErrorType_OnSelectedIndexChanged();">

Heres a little bit on the "Code-Behind" concept (VB.net example):

The "Code-Behind" concept seperates the design from the code. All the HTML elements and javascript are in the *.aspx page while the VB.net code is in the *.aspx.vb page. The *.aspx page will reside on the server as is, while the *.aspx.vb will be compiled into a *.dll file to be placed in the bin directory. Now the VB.net code may be written in the .aspx page instead between <script runat="server"> </script> tags. The code part will still be compiled into a *.dll file to be placed in the bin directory.

Why use the "Code-Behind"?
In many production environments there are separate teams that work on each part of the Web Application. The Design team generally uses WYSWYG tools like Microsoft FrontPage or Macromedia DreamWeaver to design their pages while the programming team works on the code for these pages.

In previous technologies like ASP/JSP the code is mingled throughout the page along with the design, so if the design team modified the design, there was a big possibility that they overwrote some of the programming code too! This makes life difficult for both the design and programming teams and many times leads to locked horns between the two!

In ASP.NET you can have two separate files, one for the design and another for the code. This is the most elegant solution, since the design team can work on the design page (with peace) and the programming team on their code without having to worry about clashes between the two.

Understand that this is a vastly over simplified explanation of the "Code-Behind" concept.

I hope this shed at least a little light on this subject.

Jeff B


Here is some more reading on the subject.
http://www.developer.com/net/csharp/article.php/3087791
 
Back
Top