asp.net页面中动态地添加javascript脚本
最近的项目开发中 遇到一些需要根据具体情况动态添加javaScript脚本,然后执行脚本 于是收集了一下:
1 在控件的绑定事件中添加脚本 如:在gridview控件的rowdatabind事件中可以实现 指针的选中行不同色显示 可添加脚本
protected void gvEngineerRepairState_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='99ccff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
}
}
2 在后置代码类中 还可以给控件注册脚本:
ScriptManager.RegisterStartupScript(btnPrint, this.GetType(), "onclick", "<script language='javascript'>window.open('../PrintTest/PrintBack.aspx?pname=" + txtName.Text + "');</script>", false);
如果控件在updatepanel里 而且需要脚本控制 跳转加弹框的时候
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "warning", "window.alert('您需要跳转');window.location.replace('DoTest.aspx');", true);
3可以在前台放一个<asp:Label runat="server" ID="lblShowScript" Text="" ></asp:Label> 然后在后置代码类中可以写
lblShowScript.Text="<script>alert('你要跳转?');</script>"; 此时 需要注意的是 我们有必要在此后置代码类中的Load事件里写上
lblShowScript.Text=""; 还原取消脚本
4 另外在服务器控件 按钮的前台属性中 还有onClientClick事件指的就是脚本的onclick事件
相关文档:
概述:
本文基于ASP.NET 2.0的源代码,对ASP.NET 2.0运行时进行了简要的分析,希望能帮助你理解ASP.NET 2.0中请求处理过程及页面编译模型。
关键字:
ASP.NET 2.0运行时,原理,请求处理,页面编译,ASP.NET 2.0 HTTP Runtime
主要类:
&nb ......
在做网站的时候,我们经常需要对尺寸超标的图片进行缩放。
由于浏览器的差异,有些代码某些浏览器工作不正常。
经过研究得到了以下的代码,它可以在IE6,IE8,FireFox中完美地按比例缩放图片而不失真。
//改变图片大小
function resizepic(o)
{
var maxwidth=550; //定义最大宽度
var maxheight=800;&nbs ......
JavaScript时间差计算函数
<mce:script language="javascript"><!--
Date.prototype.dateDiff = function(interval,objDate){
//若參數不足或 objDate 不是日期物件則回傳 undefined
if(arguments.length<2||objDate.constructor!=Date) return undefined;
......
在 JS(JavaScript) 操作cookies比较复杂,在 ASP 里面我们只需要知道 cookie 的名称、cookie 的值就行了,而 JS 里面,我们面对的是 cookie 的字符串,你自己编写这个字符串写入客户端,然后自己解析这个字符串。
一、从写 cookie 说起。
var the_date = new Date("December 31, 2020");
var expiresDate ......
<html>
<head>
<title>测试输入字符</title>
<script language=javascript>
String.prototype.lenB =function(){
return this.replace(/[^\x00-\xff]/g,"**").length;
}
function a() ......