• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    總結ASP.NET C#中經常用到的13個JS腳本代碼

    來源:懂視網 責編:小采 時間:2020-11-27 22:37:49
    文檔

    總結ASP.NET C#中經常用到的13個JS腳本代碼

    總結ASP.NET C#中經常用到的13個JS腳本代碼:在C#開發過程中,免不了寫一些JS,其實做后端開發的,本身不擅長寫JS,干脆總結一下,方便自己也方便別人,分享給大家。呵呵~~ 1.按鈕前后臺事件 代碼如下: <asp:Button ID=Button1 runat=server OnClick=Button1_Click
    推薦度:
    導讀總結ASP.NET C#中經常用到的13個JS腳本代碼:在C#開發過程中,免不了寫一些JS,其實做后端開發的,本身不擅長寫JS,干脆總結一下,方便自己也方便別人,分享給大家。呵呵~~ 1.按鈕前后臺事件 代碼如下: <asp:Button ID=Button1 runat=server OnClick=Button1_Click

    在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;
            }
        }     
    }

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    總結ASP.NET C#中經常用到的13個JS腳本代碼

    總結ASP.NET C#中經常用到的13個JS腳本代碼:在C#開發過程中,免不了寫一些JS,其實做后端開發的,本身不擅長寫JS,干脆總結一下,方便自己也方便別人,分享給大家。呵呵~~ 1.按鈕前后臺事件 代碼如下: <asp:Button ID=Button1 runat=server OnClick=Button1_Click
    推薦度:
    標簽: js 中使用 總結
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产成人精品久久亚洲| 国产成人精品福利网站在线观看| 国产精品一国产精品| 亚洲精品成人片在线播放| 国产精品无码一区二区在线观一| 国产精品视频一区二区三区四 | 人妻少妇精品中文字幕AV| 亚洲精品性视频| 2021国产精品成人免费视频| 在线精品无码字幕无码AV| 精品国产午夜福利在线观看| 精品国产欧美一区二区| 国产精品VA在线观看无码不卡| 亚洲中文字幕久久精品无码APP| 精品国产爽爽AV| 国产成人久久精品麻豆一区| 久久99国产精品久久99| 999国内精品永久免费视频| 久久影院综合精品| 亚洲精品你懂的在线观看 | 99久久免费国产精精品| 国产三级精品三级在线专区1 | 国产精品天干天干在线综合| 亚洲综合一区二区精品导航| 欧美巨大黑人精品videos| 国产午夜精品视频| 国内精品久久久久| 青青青青久久精品国产| 中国精品videossex中国高清| 久久成人影院精品777| 久久99国产精品久久99果冻传媒| 久久精品国产亚洲综合色| 久久精品中文字幕久久| 欧美精品天天操| 91亚洲精品麻豆| 国产精品成人观看视频| 国内精品久久久久久久涩爱 | 国产乱码精品一区二区三区四川人| 国产cosplay精品视频| 久久综合九色综合精品| 亚洲欧美日韩精品|