在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="/UploadFiles/2021-04-02/JScript.js">
<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"+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('确认如何吗"codetitle">复制代码 代码如下:
<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;
}
}
}
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!