我想做一个层的显示和隐藏。默认为隐藏。点击按钮后显示该层。
但是点击后页面自动刷新,层闪了一下又消失了,如何做到页面不刷新。

解决方案 »

  1.   

    这是默认隐藏<script type="text/javascript">
        function ShowDiv(divId) {
            document.getElementById(divId).style.display = 'block';
        }
    </script>
    这是按钮<table>
                    <tr>
                        <td><asp:Button ID="btn_Add" runat="server" Text="新增" 
                                OnClientClick="ShowDiv('div_Add')" onclick="btn_Add_Click" /></td>
                    </tr>
                    <tr>
                        <td><asp:Button ID="btn_Edit" runat="server" Text="修改"   
                                onmousedown="ShowDiv('div_Edit')" onclick="btn_Edit_Click"/></td>
                    </tr>
                    <tr>
                        <td><asp:Button ID="btn_Del" runat="server" Text="删除" onclick="btn_Del_Click" /></td>
                    </tr>
                </table>
      

  2.   


    <!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>
        <title></title>    <!--<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>-->    <script type="text/javascript">
            function change() {
                document.getElementById("showdiv").style.display = "block";
            }
        </script></head>
    <body>
        <div id="showdiv" style="background: #ccc; height: 200px; width: 200px; display: none;">
        </div>
        <input type="button" onclick="change()" value="按钮" />
    </body>
    </html>
    不要用服务器的按钮   asp:Button ID="btn_Add" runat="server" Text="新增" 
                                OnClientClick="ShowDiv('div_Add')" onclick="btn_Add_Click" />
      

  3.   

    3楼方法不行。
    我用服务器按钮时因为 点击按钮后加载gridview数据并且显示div层。
    2个onclick事件。
    所以没办法。
      

  4.   

    你页面都刷新了 JS 还有什么用啊 JS又重新加载了 你可以用PANL 控件啊
      

  5.   


     protected void Button1_Click(object sender, EventArgs e)
            {
                //绑定数据
                ScriptManager.RegisterStartupScript(this, e.GetType(), "", "change()", true);
            }<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ST.GeneralWeb.Web.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>
         <script type="text/javascript">
            function change() {
                document.getElementById("showdiv").style.display = "block";
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="showdiv" style="background: #ccc; height: 200px; width: 200px; display: none;">
        </div>
        <asp:Button ID="Button1" runat="server" Text="按钮" onclick="Button1_Click" />
        </form>
    </body>
    </html>
      

  6.   

    搞定了,谢谢zhongweng了
     
      

  7.   

    用隐藏域保存值首先给隐藏域设置一个默认值为'none',当点击按钮时改变隐藏域的值
    <input type="hidden" id="newdiv" value="none"/> <script type="text/javascript">
       document.getElementById("showdiv").style.display=document.getElementById("newdiv").value;
            function change() {
    document.getElementById("newdiv").value=""block;                    }
        </script>