一个非常隐蔽的错误

<form id="Form1" method="post" runat="server">
    
<FONT face="宋体">
        
<asp:button id="submit" runat="server" />
        
<asp:dropdownlist id="ddl" runat="server" autopostback="true">
            
<asp:ListItem>aaa</asp:ListItem>
            
<asp:ListItem>bbb</asp:ListItem>
            
<asp:ListItem>ccc</asp:ListItem>
        
</asp:DropDownList>
    
</FONT>
</form>

当选择DropDownList中的一项时,在PostBack的过程中会发生javascript错误,它会指出“Microsoft JScript 运行时错误: 对象不支持此

属性或方法”。
为什么呢?因为在PostBack后,ASP.NET会生成一个dopostback的javascript方法:

function __doPostBack(eventTarget, eventArgument) { 
var theform = document.Form1; 
theform.__EVENTTARGET.value 
= eventTarget; 
theform.__EVENTARGUMENT.value 
= eventArgument; 
theform.submit(); 

而当我们使用submit的id在form中提交的时候,它会以为theform.submit是指你所定义的id为submit的控件,而不是一个方法,所以就会出

现上述错误了。
看来在定义一些控件的ID的时候还是要尽量避免一些涉及到javascript方法的一些名称。

原文地址:https://www.cnblogs.com/xiaodi/p/145578.html