ASP.NET服務端執行js指令碼(備忘)

來源:互聯網
上載者:User

T_T : [之所以需要用到以下方式來在用戶端執行js指令碼是因為ASP.NET中的OnClick和OnClientClick的機制問題。(Html頁面中的元素(比如button)的onclick不止綁定一個方法,其中綁定的方法有OnClientClick註冊的,也有OnClick註冊的,關鍵是OnClientClick註冊的方法總是被放在OnClick註冊的方法前面,如下例子)]

Confusion:

1 <asp:Button ID="btnSave" OnClick="btnSave_Click" OnClientClick="javascript:RefreshGrid();" runat="server" />
2
3 ###########################################################################################################################################
4 html頁面結果:
5
6 <input type="submit" name="ctl00$MainContent$btnSave" value="儲存"
onclick="javascript:RefreshGrid(); WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btnSave&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))"
id="ctl00_MainContent_btnSave" class="button" />

#############################################################################################################################################

解決方案:

Template:

1 var jsCode= "<script language='javascript'>(function (){ /*你的js代碼*/})();  </script>";
2 if (!this.ClientScript.IsStartupScriptRegistered(this.GetType(), "jsCode"))
3 {
4   this.ClientScript.RegisterStartupScript(GetType(), "jsCode", jsCode);
5 }

Example:

1 var refresh = "<script language='javascript'>(function (){ window.parent.RefreshGrid();})();  </script>";
2 if (!this.ClientScript.IsStartupScriptRegistered(this.GetType(), "refresh"))
3 {
4   this.ClientScript.RegisterStartupScript(GetType(), "refresh", refresh);
5 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.