net中需要前台输出js代码的函数小技巧

////在<form>标签前部输出脚本
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), "alert('添加成功!')", true);

这时是看不到form表单的界面

//在<form>标签后部输出脚本
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('添加失败!"+ex.Message+"')", true);

可以看到表单界面

如果用Response.Write会破坏html结构,从而影响css效果。

另一种方法:在cs中定义一个protected string str = string.Empty;在需要赋js代码地方给str赋值,然后在前台html中通过<%=str%>进行调用;

 

posted @ 2013-03-22 14:40  ~峰~  阅读(137)  评论(0编辑  收藏  举报