总结ASP.NET C#中经常用到的13个JS脚本代码

时间:2022-05-18 06:27:06

this.TextBox1.Attributes.Add("onchange",
"alert(‘数据被改动,,现检查输入是否符合规则‘);");

3.注册相关属性:

this.TextBox1.Attributes.Add("readOnly", "true");

4.引入JS文件

前台HTML页面:

<script type="text/javascript" src="http://www.mamicode.com/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=http://www.mamicode.com/‘JScript.js‘ language=‘javascript‘></script>");

5.点击按钮时 相关栏位 非空判断

function checkEmpty(txtObj,msgShow)
{
    if(txtObj.value == "")
    {
        alert(msgShow);
        return false;
    }
}
<asp:Button runat="server" 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 type="checkbox" />

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 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处于选中状态,同时保存相关值在隐藏栏位