請參考HOW TO:以動態方式將用戶端指令碼加入至 ASP.NET Web 網頁
WebForm1.aspx
1: <html xmlns="http://www.w3.org/1999/xhtml">2: <head runat="server">3: <title></title>4: <script type="text/javascript" src="script/check.js"></script>5: </head>6: <body>7: <form id="form1" runat="server">8: <div>9: <input type="submit" />10: </div>11: </form>12: </body>13: </html>
check.js
1: function check1() {2: return confirm('submit this page?');3: }
WebForm1.aspx.cs
1: protected void Page_Load(object sender, EventArgs e)2: {3: Page.ClientScript.RegisterOnSubmitStatement(this.GetType(),4: "SubmitState1",5: "check1();");6: }7:
Html Source Code
1: <html xmlns="http://www.w3.org/1999/xhtml">2: <head><title>3:4: </title>5: <script type="text/javascript" src="script/check.js"></script>6: </head>7: <body>8: <form method="post" action="WebForm1.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">9:10: <script type="text/javascript">11: //<![CDATA[12: function WebForm_OnSubmit() {13: check1();14: return true;15: }16: //]]>17: </script>18:19: <div>20: <input type="submit" />21: </div>22: </form>23: </body>24: </html>25:
除了以JS檔引入之外,也可以在.aspx.cs中直接使用JS Code。
WebForm1.aspx.cs
1: protected void Page_Load(object sender, EventArgs e)2: {3: string confirmScript = @"return confirm('submit this page?');";4:5: Page.ClientScript.RegisterOnSubmitStatement(this.GetType(),6: "SubmitState1",7: confirmScript);8: }
沒有留言:
張貼留言