Getting Javascript code to fire on a selection change on a DropDownList in ASP.Net
-
04-07-2021 - |
Question
I would like to add Javascript code to be fired when the selected index is changed on a dropdownlist in asp.net.
So, here is my DDL:
<asp:DropDownList ID="comboReportTypes" runat="server" />
I would like to add something like this to the above DDL: onSelectionIndexChanged="MyJavascriptFuntion(this)"
So I'd like to end up with something like this:
<asp:DropDownList ID="comboReportTypes" runat="server" onSelectionIndexChanged="MyJavascriptFuntion(this)" />
Is this possible?
Thanks in advance. I'm using ASP.Net 4.0
Solution
add onchange event to it from the code .
Eg: DropDownList dlOption = new DropDownList(); dlOption.Attributes.Add("onchange", "javascript:return functionname(this);");
OTHER TIPS
Using jquery
you can easily achieve what you are trying to do
$('#comboReportTypes').change(function(){
//do whatever you need to.
});
Note: Make sure you are using the correct ID
for the DropDownList
as server
controls tend to change their ID's
on rendering.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow