javascript函数,是不是只有html控件可以调用,服务器控件不可以啊?
我下面的2个button,只能html button的onclick事件可以调用javascript函数,服务器button却不可以。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>点点滴滴</title>
    <script type="text/javascript">
        function haha() {
            alert("haha....");
            var str = document.getElementById("textBox1").value;
            alert(str);           
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="haha()" /> 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <input type="button" value="点击我" onclick="haha()" />
    </div>
    </form>
</body>
</html>

解决方案 »

  1.   

    可以啊。<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="haha()" /> 
      

  2.   

    楼上的正解,服务器控件要使用onclientclick="",而不是onclick,onclick是调用服务器后台的事件,而不是客户端事件
      

  3.   


        protected void Page_Load(object sender, EventArgs e)
        {
            Button2.Attributes.Add("onclick", "haha()");
        }
      

  4.   


    //后台调用代码
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", " haha()", true);
      

  5.   

    来晚了.编辑了半天,发现全都回复了.
    <head runat="server">
        <title></title>    <script type="text/javascript">
            function beforeSubmit() {
                return confirm("你确定吗?");
            }        function ShowMessage(msg) {
                alert(msg);
            }
        </script>    <script type="text/C#" runat="server">
            
            protected void Page_Load(object sender, EventArgs e)
            {
                this.btnAddByCode.Attributes.Add("onclick", "ShowMessage('Add by code')");
            }        protected void btnSubmit_Click(object sender, EventArgs e)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", @"<script type='text/javascript'>ShowMessage('You Clicked Me!');<" + "/script>");
            } 
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnSubmit" runat="server" Text="确定" OnClick="btnSubmit_Click" OnClientClick="return beforeSubmit();" />
            <asp:Button ID="btnTest" runat="server" Text="调用js函数" OnClientClick="ShowMessage('Hello')" />
            <asp:Button ID="btnAddByCode" runat="server" Text="通过代码添加js函数" />
        </div>
        </form>
    </body>