总结ASP.NET C#中经常用到的13个JS脚本代码
在C#开发过程中,免不了写一些JS,其实做后端开发的,本身不擅长写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>");
[code]
5.点击按钮时 相关栏位 非空判断
[code]
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"+" ";
strAlertContent += "bbb" +" ";
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;
}
}
}