总结ASP.NET C#中经常用到的13个JS脚本代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了总结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页面:
1 <script type="text/javascript" src="JScript.js" language="javascript"></script> 2 <script type="text/javascript" language="javascript"> 3 function fn_Name() 4 { 5 alert("JS"); 6 } 7 </script>
后台cs页面:
this.RegisterClientScriptBlock("jsFile","<script type=‘text/javascript‘ src=‘JScript.js‘ language=‘javascript‘></script>");
5.点击按钮时 相关栏位 非空判断
1 function checkEmpty(txtObj,msgShow) 2 { 3 if(txtObj.value == "") 4 { 5 alert(msgShow); 6 return false; 7 } 8 } 9 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="return checkEmpty(TextBox1,‘TextBox1 不能为空‘)" />
6.通过ChcekBox的是否点选来控制其相对应的TextBox 是否可输入
1 function chkTextBox(chkObj,txtObj) 2 { 3 if(chkObj.checked==true) 4 { 5 txtObj.value = ""; 6 txtObj.readOnly = false; 7 txtObj.focus(); 8 } 9 if(chkObj.checked == false) 10 { 11 txtObj.value = ""; 12 txtObj.readOnly = true; 13 } 14 } 15 <input id="Checkbox1" type="checkbox" onclick="chkTextBox(Checkbox1,TextBox1)" />
7.传值到模态窗口 并得到传回的值
1 var EnCodeQueryName = escape(Name); 2 var strPara = "‘dialogWidth: 400px;dialogHeight: 400px;dialogLeft: 300px;dialogTop: 200px;toolbar: no;menubar: no;resizable: yes;location: no;status: no;scrollbars= no‘"; 3 var ReturnInfo = window.showModalDialog("QryName.aspx?&Name="+EnCodeQueryName +"&QueryID="+QueryType+"",‘‘,strPara); 4 if(ReturnInfo !=null) 5 { 6 var arrayReturnInfo = ReturnInfo .split("@"); 7 document.all.drpID.value = arrayReturnInfo[1]; 8 document.all.txtName.value= arrayReturnInfo[2]; 9 }
8.弹出JS的确认对话框,并根据确认结果 触发后台相关操作
1 if(confirm(‘确认如何吗?‘)) 2 { 3 document.all.hidbtn_Submit.click(); 4 } 5 else 6 { 7 document.all.hidbtn_Cancel.click(); 8 }
HTML页面相关代码:
1 <input id="hidbtn_Submit" type="button" value="确认修改" style="display:none;" onserverclick="hidbtn_Submit_ServerClick" runat="server" />
9.添加页面对快捷键的响应,如 按F2时 进行新增按钮的操作等
1 #region 添加页面对快捷键的响应 2 string strJS_ShortKey = "<script language=‘javascript‘ type=‘text/javascript‘ > "; 3 strJS_ShortKey += " document.onkeydown=shortKeyDown; "; 4 strJS_ShortKey += " function shortKeyDown() "; 5 strJS_ShortKey += " { "; 6 // 新增 7 if (this.ButtonCtl1.ImgBtn_AddFamily.Visible) 8 { 9 string btnInsertCID = this.ButtonCtl1.ImgBtn_Insert.ClientID.Trim(); 10 //F2 - 113 11 strJS_ShortKey += " if(event.keyCode==‘113‘) "; 12 strJS_ShortKey += " { "; 13 strJS_ShortKey += " document.all(‘" + btnInsertCID + "‘).click();"; 14 strJS_ShortKey += " event.keyCode= 0; "; 15 strJS_ShortKey += " event.returnValue = false; "; 16 strJS_ShortKey += " return false; "; 17 strJS_ShortKey += " } "; 18 } 19 // 修改 20 if (this.ButtonCtl1.ImgBtn_Edit.Visible) 21 { 22 string btnEditCID = this.ButtonCtl1.ImgBtn_Edit.ClientID.Trim(); 23 //F3 - 114 24 strJS_ShortKey += " if(event.keyCode==‘114‘) "; 25 strJS_ShortKey += " { "; 26 strJS_ShortKey += " document.all(‘" + btnEditCID + "‘).click();"; 27 strJS_ShortKey += " event.keyCode= 0; "; 28 strJS_ShortKey += " event.returnValue = false; "; 29 strJS_ShortKey += " return false; "; 30 strJS_ShortKey += " } "; 31 } 32 strJS_ShortKey += " } "; 33 //注册事件 34 Page.RegisterStartupScript("shortKey", strJS_ShortKey); 35 #endregion
10.弹出的提示 分行显示
1 alert(‘aaa \r\n bbb \r\n ccc‘);
如果是在后台.cs文件中注册
1 string strAlertContent = "aaa"+" \\r\\n "; 2 strAlertContent += "bbb" +" \\r\\n ";
11.点击GridView上的某一行时,行首列处的RadioButton处于选中状态,同时保存相关值在隐藏栏位
1 //用查询得的数据集进行绑定 2 if (dt.Rows.Count > 0) 3 { 4 //绑定 5 this.gv_InfoFromSendModule.DataSource = dt; 6 this.gv_InfoFromSendModule.DataBind(); 7 //确定按钮显示 8 this.btn_OK.Visible = true; 9 this.txthid_RowCount.Text = dt.Rows.Count.ToString(); 10 } 11 //GridView的RowDataBound 12 protected void gv_InfoFromSendModule_RowDataBound(object sender, GridViewRowEventArgs e) 13 { 14 if (e.Row.RowIndex < 0) 15 return; 16 e.Row.Attributes.Add("onclick", "radButton(‘" + e.Row.RowIndex.ToString() + "‘,‘" + e.Row.Cells[1].Text.Trim() + "‘);"); 17 //RadioButton rad = (RadioButton)e.Row.Cells[0].FindControl("rad_Select"); 18 //rad.Attributes.Add("onclick", "radButton(‘"+e.Row.RowIndex.ToString()+"‘,‘"+ e.Row.Cells[1].Text.Trim()+"‘);"); 19 } 20 //行上所绑定的JS 21 function radButton(rowIndex,rowGUID) 22 { 23 //gv_InfoFromSendModule$ctl02$rad_Select 24 var rowCount = parseInt(document.all.txthid_RowCount.value)+2; 25 for(var i=2;i<rowCount;i++) 26 { 27 var tmpName; 28 if(i<10) 29 { 30 tmpName = "gv_InfoFromSendModule$ctl0"+i+"$rad_Select"; 31 } 32 else 33 { 34 tmpName = "gv_InfoFromSendModule$ctl"+i+"$rad_Select"; 35 } 36 //取得对应的Radio对象 37 var tmpRadio = document.getElementById(tmpName); 38 //当前选中 其他取消选中 39 if((i-2) == rowIndex) 40 { 41 tmpRadio.checked = true; 42 } 43 else 44 { 45 tmpRadio.checked = false; 46 } 47 } 48 document.all.txthid_GUID.value = rowGUID; 49 }
12.去掉前后空格
1 function fn_Trim(obj) 2 { 3 if(obj==null) 4 { 5 return; 6 } 7 else 8 { 9 var oldStr = obj.value; 10 var newStr = oldStr.replace(/^\s+|\s+$/g,""); 11 obj.value = newStr; 12 } 13 }
13.TextBox文本内容长度判断 看是否超过长度 超过返回true
1 function fn_IsTooLong(obj,varLength) 2 { 3 if(obj==null) 4 { 5 return false; 6 } 7 else 8 { 9 var valueStr = obj.value; 10 var len = valueStr.match(/[^ -~]/g) == null ? valueStr.length : valueStr.length + valueStr.match(/[^ -~]/g).length ; 11 if(len > parseInt(varLength) ) 12 { 13 return true; 14 } 15 else 16 { 17 return false; 18 } 19 } 20 }
以上是关于总结ASP.NET C#中经常用到的13个JS脚本代码的主要内容,如果未能解决你的问题,请参考以下文章