日期:2014-05-18  浏览次数:20528 次

自定义控件有含有button,自动生成的页面就没有__dopostback函数,为什么?
自定义控件放button,查看画面源代码,没有自动生成的__dopostback函数。
但自定义控件放其他按钮如linkbutton,那就正常了,能自动生成__dopostback函数。
为什么啊?大家试试。
代码:
'''''''''''''''Control''''''''' 
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>
<asp:Button runat ="server" Text ="aaaaa" id="btn" />

'''''''''''''''Aspx''''''''' 
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="ucTitleBar" TagPrefix="uc1" %>
<!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>Untitled Page</title>
  <script type="text/javascript" language="javascript">
  function a(){

  }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <table>
  <tr><td><uc1:ucTitleBar runat ="server" ID ="aa" /></td></tr>
  <tr><td><asp:Button runat ="server" ID="btn1" Text =" TEST " OnClientClick ="return a();"/></td></tr>
  </table>
  </div>
  </form>
</body>
</html>


------解决方案--------------------
这个问题本质在于LinkButton控件在生成时的OnPreRender(EventArgs e)事件里
调用了Page.RegisterPostBackScript()方法,所以才在前台自动生成了__doPostBack,
而一般情况下Button控件没有,除非你把Button的UseSubmitBehavior属性置为false才会有