C#中常用到的JS

來源:互聯網
上載者:User

新年新開始
如題所示 在後續的日子裡
會將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>");

5.點擊按鈕時 相關欄位 非空判斷
    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;
        }
    }      
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.