user3442192 user3442192 - 1 year ago 70
ASP.NET (C#) Question

Bug with controlid name

Is there any naming convention or restrictions for a button to be named as "submit"? I have a dropdown list with autopostback= true and a button. It should auto generate textbox with the data from the database

<asp:DropDownList ID="d1" runat="server" DataSourceID="SqlDataSource1" DataTextField="scode" DataValueField="scode" OnSelectedIndexChanged="d1_SelectedIndexChanged" AppendDataBoundItems="true" AutoPostBack="true">
<asp:ListItem Selected="True">0</asp:ListItem>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\database.mdf;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT [scode] FROM [warehouse]"></asp:SqlDataSource>
<asp:TextBox ID="t1" runat="server"></asp:TextBox>
<asp:Button ID="submit" runat="server" Text="Button" />

protected void d1_SelectedIndexChanged(object sender, EventArgs e)
t1.Text = "abcd";

my problem is when i set the controlid of my button as "submit" the postbacks and event handling from the dropdownlist are not triggered but pending. The postbacks and event handling are only triggered when i click on the "submit" button. Pretty sure the problem lies within controlid of the button as I took few hours of trial and error to find a workaround. By renaming the controlid of the button as "submit1" and everything worked perfectly. In short, the textbox are autogenerated with my data whenever I select an item when the controlid of the button is named as submit1". Meanwhile if the controlid of the button is "submit", the data are only generated when i click on the button. This is a small issue as i managed to find the workaround but i would like to know why is this happening.

Answer Source

Using the following minimal code I was able to reproduce your issue on a new web application project with no master page.

<asp:DropDownList runat="server" OnSelectedIndexChanged="d1_SelectedIndexChanged" AutoPostBack="true">
<asp:TextBox ID="ResultLabel" runat="server" />
<asp:Button ID="submit" runat="server" Text="Button" />

Code behind

protected void d1_SelectedIndexChanged(object sender, EventArgs e)
    ResultLabel.Text = "Postback occurred";

If you watch the JavaScript console, you'll see that it results in an error when you change the dropdownlist selection.

Uncaught TypeError: theForm.submit is not a function

This error comes from the bit of code that Web Forms inserts into your page to make postbacks work properly.

That error prevents the postback from completing. The solution is to not have any controls with an ID of submit. You might consider this a bug, but changing the framework to get around this issue would make the framework more complicated than it needs to be.