代码如下:
js代码和页面(就只有一个按钮)
- JScript code
<title>无标题页</title> <script type="text/javascript" language="javascript"> function CCC() { alert("XXXXX"); } </script></head><body onload="CCC()"> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> </form></body></html>
后台cs文件中的页面按钮功能
- C# code
public partial class Default16 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } prfile:///E:\本地CVS工作目录\页面咨询\Default16.aspxotected void Button1_Click(object sender, EventArgs e) { Response.Write("!!!"); }}
现在的结果是,我一点按钮除了会执行Response.Write("!!!");之外还会执行unload事件的CCC()函数,每次点按钮都会跳出alert("XXXX")
请问诸位高手我怎么才能让alert("XXXX")只在页面载入的时候才运行,在postback刷新的时候不去运行
------解决方案--------------------------------------------------------
太Easy 了吧,你直接在
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "c", "<script language = 'javaScript'>alert('XXXX');</script>“)
}
}
前台body onload事件不要给
------解决方案--------------------------------------------------------
将页面中body改一下:
<body id="pageBody" runat="server">
代码:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
pageBody.Attributes["onload"] = "CCC()";
}
}
------解决方案--------------------------------------------------------
<script type="text/javascript">
function CCC()
{
if(window.name=="") alert("XXXXX");
}
</script>
------解决方案--------------------------------------------------------
你不就是想弹出个对话框要说句话吗?根本用不着在前台写JS方法去说一句话,直接在窗体率加裁时候写客户端脚本!
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
你的要求挺奇怪的.
- HTML code
<title>无标题页</title> <script type="text/javascript" language="javascript"> function CCC() { var ispostback = "<%=IsPostBack.ToString() %>"; if(!ispostback){ alert("XXXXX"); } } </script></head><body onload="CCC()"> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> </form></body></html>