在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"+" \\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;
}
}
}