Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 145701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:28:13+00:00 2026-05-11T08:28:13+00:00

I’ve been doing C# for years but ASP.NET for not so long and this

  • 0

I’ve been doing C# for years but ASP.NET for not so long and this has me stumped.

In my troubleshooting example I have a dropdownlist in an ASP.NET page, it has four items in it, I have a serverside event that fires on selectedindexchanged, it all works great in this scenario.

However, if (as i have in my evolved code) I set a Javascript handler for the ‘onchange’ event, (which launches an alert) the alert works fine, but the server side event no longer fires… I’m sure I’m missing something obvious.

Important notes: Autopostback is set to true on the dropdownlist control, viewstate is enabled, the Javascript event handler returns true

Anyone?

It’s not that I have to manually wire up the __doPostBack is it?

Any help greatly appreciated.

Edit

ok here is the code first the codebehind

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text;  namespace TestEvents { public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {      }      protected override void OnPreRender(EventArgs e)     {          ClientScriptManager csm = Page.ClientScript;         if (!csm.IsClientScriptBlockRegistered('NotesChangeScript'))         {             StringBuilder sb = new StringBuilder();             sb.Append('\r\n <script type=\'text/javascript\'> \r\n');             sb.Append(' //<![CDATA[ \r\n');             sb.Append(' var changesCount = 0; \r\n');              sb.Append(' function selection_handler()\r\n');             sb.Append('{ \r\n');             sb.Append(' changesCount += 1;\r\n');             sb.Append(' alert('i changed on the client!'); \r\n');             sb.Append(' return true; \r\n');             sb.Append(' } \r\n');              sb.Append(' function SetUpNotesHandler() \r\n');             sb.Append('{ \r\n');             sb.Append('          var ctrls = document.getElementsByTagName(\'SELECT\');\r\n');             sb.Append('          for(i=0;i<ctrls.length;i++)\r\n');             sb.Append('                  {\r\n');             sb.Append('                     ctrls[i].onchange = selection_handler; \r\n');             sb.Append('                 }\r\n');             sb.Append('}\r\n');               sb.Append(' //]]> \r\n');             sb.Append('</script>');              csm.RegisterClientScriptBlock(this.GetType(), 'NotesChangeScript', sb.ToString(), false);              StringBuilder initScript = new StringBuilder();             initScript.Append('<script type=\'text/javascript\' >\r\n');             initScript.Append(' //<![CDATA[\r\n');             initScript.Append('  SetUpNotesHandler(); \r\n');             initScript.Append(' //]]> \r\n');             initScript.Append('</script> \r\n');             csm.RegisterStartupScript(this.GetType(), 'StartUpKey', initScript.ToString(),false);             base.OnPreRender(e);         }      }     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)     {         Response.Write('i hit the server event');     } } 

}

and here is the markup for the page

<%@ Page Language='C#' AutoEventWireup='true'     CodeBehind='Default.aspx.cs'    Inherits='TestEvents._Default' %>  <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>  <html xmlns='http://www.w3.org/1999/xhtml' > <head runat='server'> <title>problem</title> </head> <body> <form id='form1' runat='server'> <div>     <asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='True' OnSelectedIndexChanged='DropDownList1_SelectedIndexChanged'>         <asp:ListItem>Numero Uno</asp:ListItem>         <asp:ListItem>Numer Deux</asp:ListItem>         <asp:ListItem>Number Three</asp:ListItem>         <asp:ListItem>Nomina Quatros</asp:ListItem>     </asp:DropDownList>     <asp:HiddenField ID='hdnFieldChange' runat='server' /> </div> </form> </body> </html> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:28:14+00:00Added an answer on May 11, 2026 at 8:28 am

    ok, firstly, thanks to ScarletGarden for making it clear what the problem was, i had assumed as much but its nice when someone confirms your suspicions.

    Secondly, apologies to those who see this as some sort of n00b question and find the whole premise of this question stupid, i really couldnt find any articles clarifying this specific problem…

    Thirdly, i have now worked out how to do this in Javascript (although scarlet gardens solution is a lot simpler in pretty much every case) although the attributes.add approach (as scarletGarden suggested) may suffer if you had multiple handlers and needed them to fire in some sort of sequence.

    Having had a little dig in reflector, Attributes.Add adds event references by semi colon separating them, so this clearly works as it preserves any handlers that are already declared and semi-colon separates the one(s) you add.

    In javascript this kind of chaining (multicasting of a sort) requires that DOM level 2 functionality is used (the javascript bible was useful for identifying this), it should be no surprise that Mozilla and IE have a different way of achieving the same thing and so, in order to add a js handler and preserve the event handling in codebehind, the line in my example above that says

    ctrls[i].onchange = selection_handler; \r\n 

    needs to be replaced by the following (before people get precious about this example i do have a big caveat for this, further down)

    sb.Append('                 if (ctrls[i].addEventListener) ctrls[i].addEventListener(\'change\',selection_handler,false);'); sb.Append('                 else if (ctrls[i].attachEvent) ctrls[i].attachEvent(\'on\' + \'change\', selection_handler, false);'); sb.Append('                 else return false;'); 

    attachEvent works in IE, addEventListener in Firefox (note the different names for the events, Mozilla preferring to drop the ‘on’

    Adding events like this works in a LIFO (last in First Out) manner, there are many alternatives to sequencing events in this manner using more elaborate javascript, covered in an excellent, if slightly confusing thread (because of the way it starts) here http://codingforums.com/showthread.php?t=154673

    I hope this stuff enlightens others as much as me, this is the thread i was looking for when i first hit the problem and now i feel a bit stupid, but hey ho

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.