Who is online? 97 guests and 0 members
Member login | Become a member
home » blogs » vivek_iit » DropDownList Not Posting Back in ASP.NET
posted 3/6/2008 by vivek_iit
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.
vivek_iit (Member since: 11/27/2008) I am one of the administrators at CodeAsp.Net and I love programming, architecting solutions, code reviews, teaching and writing about ASP.NET.
View vivek_iit 's profile
Comment (No HTML)
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address: