Vivek Thakur

Chaotically Complex

  Home  |   Contact  |   Syndication    |   Login
  105 Posts | 1 Stories | 499 Comments | 65 Trackbacks

News



Archives

ASP.NET Ventures

*******************
Note: view the updated entry below:

http://www.codeasp.net/blogs/vivek_iit/microsoft.net/144/dropdownlist-not-posting-back-in-asp.net

*******************

Recently I faced a strange issue while working on a community project. There was a webpage written by a previous developer on which I had to add an additional DropDownList control. The dropdownlist I added on a page was not firing on Postbacks (even when AutoPostBack was enabled). The messed up single ASP and HTML code on that page made it extremely difficult to debug and find out the reason behind it, adding to my frustration.

After trying different things out in vain, I noticed a serious mistake, the ASP:Button control added by the programmer had used “submit” as the ID of the control: 

<asp:Button id="submit" Text = "Submit" runat="server" OnClick="Submit_Click" /> 

“submit” is a keyword used by the browser and the runtime to identify the controls that can submit a form, hence using them as IDs will cause strange behavior aand might stop posting back of other controls which don't submit by default, like DropDownList (which was happening in my case). DropDownList control uses JavaScript (when AutoPostBack is turned On) to submit the form and this JS code looks something like:

<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["frmDefault"];
}
else {
theform = document.frmDefault;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
The DropDownList web control is rendered as:
<select name="ddlCity" onchange="__doPostBack('ddlCity','')" language="javascript" id="ddlCity">
 
Now, if the ID of any other control which submits by default (like a button) is "submit" then this JS will not work and 
you will notice a JS error saying "Invalid object name".
 

I changed its ID to “btnSubmit” and the issue got resolved.

posted on Tuesday, March 13, 2007 9:30 AM