前台代码
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:Button ID="Button1" runat="server" Text="隐藏" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="显示" OnClick="Button2_Click" /> <div id="container" runat="server"> 内容 </div> </form> </body> </html>
后台代码
// 隐藏 protected void Button1_Click(object sender, EventArgs e) { //// 方式一 RegisterStartupScript("", "<script>document.getElementById('container').style.display='none';</script>"); // 方式二 Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>document.getElementById('container').style.display='none';</script>"); } // 显示 protected void Button2_Click(object sender, EventArgs e) { // 方式一 RegisterStartupScript("", "<script>document.getElementById('container').style.display='';</script>"); // 方式二 Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>document.getElementById('container').style.display='';</script>"); }