<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %><!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>无标题页</title>
    <style type="text/css">
        .boderBottom
        {
            border-bottom: green thin solid;
            border-right: 0px solid;
            border-top: 0px solid;
            border-left: 0px solid;
        }
    </style>    <script type="text/javascript">
        function changeStyle(id)
        {
            var textBox = document.getElementById(id);
            textBox.className = "boderBottom"
            textBox.disabled = true;
        }
        function helloWorld(){
            alert('Hello World');
        }
    </script></head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <input id="Button1" type="button" value="button" onclick="changeStyle('TextBox1')" runat="server"/>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="changeStyle('TextBox1')"/>
        <asp:Button ID="Button3" runat="server" Text="Button" OnClientClick="helloWorld()"/>
     </div>
    </form>
</body>
</html>Button1可以实现changeStyle的效果 为什么Button2不能?

解决方案 »

  1.   

    <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="changeStyle('TextBox2')"/>
      

  2.   

     onclick
    OnClientClick你去看看这2个的区别。。自然就懂了
      

  3.   

    使用 OnClientClick 属性来指定在引发某个 Button 控件的 Click 事件时所执行的附加客户端脚本。(MSDN)
    OnClentClick会使页面回送  Button1后面有 runat="server" 它不会使页面回送?
      

  4.   

    OnClientClick="changeStyle('TextBox1');return false;"
    执行完js表单就提交了 return false阻止提交