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脚本代码的更多相关文章

  1. [转]ASP.NET MVC中你必须知道的13个扩展点

    本文转自:http://www.cnblogs.com/ejiyuan/archive/2010/03/09/1681442.html ScottGu在其最新的博文中推荐了Simone Chiaret ...

  2. 【转】在ASP.NET MVC中,使用Bundle来打包压缩js和css

    在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原 ...

  3. ASP.NET MVC中你必须知道的13个扩展点

         ScottGu在其最新的博文中推荐了Simone Chiaretta的文章13 ASP.NET MVC extensibility points you have to know,该文章为我 ...

  4. 在ASP.NET MVC中,使用Bundle来打包压缩js和css(转)

    转自:http://www.cnblogs.com/xwgli/p/3296809.html 在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和c ...

  5. 在ASP.NET MVC中,使用Bundle来打包压缩js和css

    该总结参考博文地址:http://www.cnblogs.com/xwgli/p/3296809.html 1.首先了解Bundle的作用:Bundles用于打包CSS和javascript脚本文件, ...

  6. ASP.NET后台输出js脚本代码

    利用asp.net输出js我们大多数都会直接使用Respone.Write()然后根js格式的代码,再在页面调用时我们直接这样是完全可以实现的,下面我来给大家介绍另一种方法 我是我最初的想法以下是代码 ...

  7. ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误)

    在ASP.NET MVC4中微软引入了bundles特性,这个特性可以将服务器端的多个Javascript或多个css文件捆绑在一起作为一个单一的URL地址供客户端浏览器调用,从而减少了页面上Http ...

  8. 在Asp.net Core中使用中间件来管理websocket

    介绍 ASP.NET Core SignalR是一个有用的库,可以简化Web应用程序中实时通信的管理.但是,我宁愿使用WebSockets,因为我想要更灵活,并且与任何WebSocket客户端兼容. ...

  9. ASP.NET Core 中的那些认证中间件及一些重要知识点

    前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...

随机推荐

  1. 基于FPGA的线阵CCD实时图像采集系统

    基于FPGA的线阵CCD实时图像采集系统 2015年微型机与应用第13期 作者:章金敏,张 菁,陈梦苇2016/2/8 20:52:00 关键词: 实时采集 电荷耦合器件 现场可编程逻辑器件 信号处理 ...

  2. [svc][op]关闭linux centos各种声音

    现在基本都用xshell了,直接xshell禁止即可 shell报警 #vi /etc/inputrc ================================ set bell-style ...

  3. ubuntu图形界面调出命令行

    新安装的ubuntu12.04在左边的快捷方式里默认是没有终端图标的,可以使用如下方法打开终端: 使用ctrl+alt+t.这个组合键适合ubuntu的各种版本.但是,在使用KVM虚拟机时可能会出现问 ...

  4. hdoj2037 今年暑假不AC

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. VC2010下Qt5的中文乱码问题

    要搞清楚这个问题,先要弄明白编码.但是编码问题实在太复杂,这里肯定讲不开. 我先找一个例子,比如:“中文” 的 Unicode 码点/UTF8编码/GBK 分别是多少. 先去这个网站,输入 “中文” ...

  6. fileupload 上传execl文件的一些操作

    OABaseReadExeclDataAction .class 包含创建临时文件目录,基本校验,取属性值,处理乱码,基类 这里在上传文件是execl并且需要读取的话,需要把fileitem对象转换成 ...

  7. sqlserver 用户名创建 及权限配置

    打开SQL Server2008,以windows身份验证模式登陆(其他版本同理) 在"对象资源管理器"中展开"安全性",右击"登录名",在 ...

  8. RTX——第16章 消息邮箱

    以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 前面几个章节主要给大家讲解了任务间的同步和资源共享机制,本章节为大家讲解任务间的通信机制消息邮箱,RTX ...

  9. 翻转数字最后n位

    #include<stdio.h> int turn_n(int ,int); int main(void) { ,b=; printf("%x\n%d\n%x\n", ...

  10. C语言 · 身份证号码升级

    算法提高 身份证号码升级   时间限制:1.0s   内存限制:256.0MB      问题描述 从1999年10月1日开始,公民身份证号码由15位数字增至18位.(18位身份证号码简介).升级方法 ...