总结ASP.NET C#中经常用到的13个JS脚本代码
1.按钮前后台事件
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"
OnClientClick="alert('客房端验证,阻止向服务器端提交');return false;" />
2.注册相关事件:onblur,onclick,onchange
this.TextBox1.Attributes.Add("onchange",
"alert('数据被改动,现检查输入是否符合规则');");
3.注册相关属性:
this.TextBox1.Attributes.Add("readOnly", "true");
4.引入JS文件
前台HTML页面:
<script type="text/javascript" src="JScript.js" language="javascript"></script>
<script type="text/javascript" language="javascript">
function fn_Name()
{
alert("JS");
}
</script>
后台cs页面:
this.RegisterClientScriptBlock("jsFile",
"<script type='text/javascript' src='JScript.js' language='javascript'></script>");
5.点击按钮时 相关栏位 非空判断
function checkEmpty(txtObj,msgShow)
{
if(txtObj.value == "")
{
alert(msgShow);
return false;
}
}
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"
OnClientClick="return checkEmpty(TextBox1,'TextBox1 不能为空')" />
6.通过ChcekBox的是否点选来控制其相对应的TextBox 是否可输入
function chkTextBox(chkObj,txtObj)
{
if(chkObj.checked==true)
{
txtObj.value = "";
txtObj.readOnly = false;
txtObj.focus();
}
if(chkObj.checked == false)
{
txtObj.value = "";
txtObj.readOnly = true;
}
}
<input id="Checkbox1" type="checkbox" onclick="chkTextBox(Checkbox1,TextBox1)" />
7.传值到模态窗口 并得到传回的值
var EnCodeQueryName = escape(Name);
var strPara = "'dialogWidth: 400px;dialogHeight: 400px;dialogLeft: 300px;dialogTop: 200px;toolbar: no;menubar: no;resizable: yes;location: no;status: no;scrollbars= no'";
var ReturnInfo = window.showModalDialog("QryName.aspx?&Name="+EnCodeQueryName +"&QueryID="+QueryType+"",'',strPara);
if(ReturnInfo !=null)
{
var arrayReturnInfo = ReturnInfo .split("@");
document.all.drpID.value = arrayReturnInfo[1];
document.all.txtName.value= arrayReturnInfo[2];
}
8.弹出JS的确认对话框,并根据确认结果 触发后台相关操作
if(confirm('确认如何吗?'))
{
document.all.hidbtn_Submit.click();
}
else
{
document.all.hidbtn_Cancel.click();
}
HTML页面相关代码:
<input id="hidbtn_Submit" type="button" value="确认修改"
style="display:none;"
onserverclick="hidbtn_Submit_ServerClick"
runat="server" />
9.添加页面对快捷键的响应,如 按F2时 进行新增按钮的操作等
#region 添加页面对快捷键的响应
string strJS_ShortKey = "<script language='javascript' type='text/javascript' > ";
strJS_ShortKey += " document.onkeydown=shortKeyDown; ";
strJS_ShortKey += " function shortKeyDown() ";
strJS_ShortKey += " { ";
// 新增
if (this.ButtonCtl1.ImgBtn_AddFamily.Visible)
{
string btnInsertCID = this.ButtonCtl1.ImgBtn_Insert.ClientID.Trim();
//F2 - 113
strJS_ShortKey += " if(event.keyCode=='113') ";
strJS_ShortKey += " { ";
strJS_ShortKey += " document.all('" + btnInsertCID + "').click();";
strJS_ShortKey += " event.keyCode= 0; ";
strJS_ShortKey += " event.returnValue = false; ";
strJS_ShortKey += " return false; ";
strJS_ShortKey += " } ";
}
// 修改
if (this.ButtonCtl1.ImgBtn_Edit.Visible)
{
string btnEditCID = this.ButtonCtl1.ImgBtn_Edit.ClientID.Trim();
//F3 - 114
strJS_ShortKey += " if(event.keyCode=='114') ";
strJS_ShortKey += " { ";
strJS_ShortKey += " document.all('" + btnEditCID + "').click();";
strJS_ShortKey += " event.keyCode= 0; ";
strJS_ShortKey += " event.returnValue = false; ";
strJS_ShortKey += " return false; ";
strJS_ShortKey += " } ";
}
strJS_ShortKey += " } ";
//注册事件
Page.RegisterStartupScript("shortKey", strJS_ShortKey);
#endregion
10.弹出的提示 分行显示
alert('aaa \r\n bbb \r\n ccc');
如果是在后台.cs文件中注册
string strAlertContent = "aaa"+" \\r\\n ";
strAlertContent += "bbb" +" \\r\\n ";
11.点击GridView上的某一行时,行首列处的RadioButton处于选中状态,同时保存相关值在隐藏栏位
//用查询得的数据集进行绑定
if (dt.Rows.Count > 0)
{
//绑定
this.gv_InfoFromSendModule.DataSource = dt;
this.gv_InfoFromSendModule.DataBind();
//确定按钮显示
this.btn_OK.Visible = true;
this.txthid_RowCount.Text = dt.Rows.Count.ToString();
}
//GridView的RowDataBound
protected void gv_InfoFromSendModule_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex < 0)
return;
e.Row.Attributes.Add("onclick", "radButton('" + e.Row.RowIndex.ToString() + "','" + e.Row.Cells[1].Text.Trim() + "');");
//RadioButton rad = (RadioButton)e.Row.Cells[0].FindControl("rad_Select");
//rad.Attributes.Add("onclick", "radButton('"+e.Row.RowIndex.ToString()+"','"+ e.Row.Cells[1].Text.Trim()+"');");
}
//行上所绑定的JS
function radButton(rowIndex,rowGUID)
{
//gv_InfoFromSendModule$ctl02$rad_Select
var rowCount = parseInt(document.all.txthid_RowCount.value)+2;
for(var i=2;i<rowCount;i++)
{
var tmpName;
if(i<10)
{
tmpName = "gv_InfoFromSendModule$ctl0"+i+"$rad_Select";
}
else
{
tmpName = "gv_InfoFromSendModule$ctl"+i+"$rad_Select";
}
//取得对应的Radio对象
var tmpRadio = document.getElementById(tmpName);
//当前选中 其他取消选中
if((i-2) == rowIndex)
{
tmpRadio.checked = true;
}
else
{
tmpRadio.checked = false;
}
}
document.all.txthid_GUID.value = rowGUID;
}
12.去掉前后空格
function fn_Trim(obj)
{
if(obj==null)
{
return;
}
else
{
var oldStr = obj.value;
var newStr = oldStr.replace(/^\s+|\s+$/g,"");
obj.value = newStr;
}
}
13.TextBox文本内容长度判断 看是否超过长度 超过返回true
function fn_IsTooLong(obj,varLength)
{
if(obj==null)
{
return false;
}
else
{
var valueStr = obj.value;
var len = valueStr.match(/[^ -~]/g) == null ? valueStr.length : valueStr.length + valueStr.match(/[^ -~]/g).length ;
if(len > parseInt(varLength) )
{
return true;
}
else
{
return false;
}
}
}
总结ASP.NET C#中经常用到的13个JS脚本代码的更多相关文章
- [转]ASP.NET MVC中你必须知道的13个扩展点
本文转自:http://www.cnblogs.com/ejiyuan/archive/2010/03/09/1681442.html ScottGu在其最新的博文中推荐了Simone Chiaret ...
- 【转】在ASP.NET MVC中,使用Bundle来打包压缩js和css
在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原 ...
- ASP.NET MVC中你必须知道的13个扩展点
ScottGu在其最新的博文中推荐了Simone Chiaretta的文章13 ASP.NET MVC extensibility points you have to know,该文章为我 ...
- 在ASP.NET MVC中,使用Bundle来打包压缩js和css(转)
转自:http://www.cnblogs.com/xwgli/p/3296809.html 在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和c ...
- 在ASP.NET MVC中,使用Bundle来打包压缩js和css
该总结参考博文地址:http://www.cnblogs.com/xwgli/p/3296809.html 1.首先了解Bundle的作用:Bundles用于打包CSS和javascript脚本文件, ...
- ASP.NET后台输出js脚本代码
利用asp.net输出js我们大多数都会直接使用Respone.Write()然后根js格式的代码,再在页面调用时我们直接这样是完全可以实现的,下面我来给大家介绍另一种方法 我是我最初的想法以下是代码 ...
- ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误)
在ASP.NET MVC4中微软引入了bundles特性,这个特性可以将服务器端的多个Javascript或多个css文件捆绑在一起作为一个单一的URL地址供客户端浏览器调用,从而减少了页面上Http ...
- 在Asp.net Core中使用中间件来管理websocket
介绍 ASP.NET Core SignalR是一个有用的库,可以简化Web应用程序中实时通信的管理.但是,我宁愿使用WebSockets,因为我想要更灵活,并且与任何WebSocket客户端兼容. ...
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
随机推荐
- eclipse中的项目受svn管理
1.我们在启动Eclipse的时候都会有例如以下图提示: 假设我们直接这样输入目录的名字,这个文件会在eclipse安装目录的同一级自己主动生成这样一个名字叫做njgzw的目录.接下来我们每次启动都用 ...
- 使用tesseract-ocr破解网站验证码
首先我得承认,关注tesseract-ocr, 是冲着下面这篇文章的噱头去的,26行groovy代码破解网站验证码 http://www.kellyrob99.com/blog/2010/03/14/ ...
- 临界区&Monitor
监视器(Monitor)的概念 可以在MSDN(http://msdn.microsoft.com/zh-cn/library/ms173179(VS.80).aspx)上找到下面一段话: 与lock ...
- Tomcat的URL中文乱码解决以及传输优化
默认的tomcat容器如果直接使用get方式在url中传中文时,传到后台接收会是乱码. 乱码问题 原因: tomcat默认的在url传输时是用iso8859-1编码. 解决方案一: 在使用get传输参 ...
- java的static块执行时机<转>
一.误区:简单认为JAVA静态代码块在类被加载时就会自动执行.证错如下: class MyClass1 { static {//静态块 System.out.println("static ...
- 关于那些常见的坑爹的小bug(会持续更新)
当我学了矩阵分析的时候我知道什么是麻烦,当我学了傅里叶级数的时候我知道什么是相当麻烦. 然而,当我刚刚接触前端,我才明确什么叫做坑爹的ie6.这个分享对于经验丰富的前端基本都遇过.对于刚入行的新手,也 ...
- MVC出现错误:系统找不到指定文件(异常来自 HRSULT:0x80070002)
vs2013创建Web应用程序MVC出现错误:系统找不到指定文件(异常来自 HRSULT:0x80070002) 查到博客园VS2013新建Web Application时报错Exception fr ...
- innobackupex参数之 --throttle 限速这个值设置多少合理 原创
innobackupex参数之--parallel --throttle--parallel 此参数用于开启多个子进程并发备份多个数据文件(注意,一个数据文件只会有一个进程完成备份).可以加快备份速度 ...
- iOS边练边学--transform的简单介绍并且用transform实现键盘处理
一.transform:形变属性,能完成功能:平移,缩放,旋转 <平移> // 根据给的移动距离平移 self.tempView.transform = CGAffineTransform ...
- js学习笔记32----new
new:用于创建一个对象. 有 new 与 无 new 时的区别,查看下面的示例代码应该会增加感觉: <!DOCTYPE html> <html lang="en" ...