Hopes

          Start Here..

           

          onclick,onclientclick和onserverclick的區別

          onclick,onclientclick和onserverclick的區別  

          下面以 HTML的按鈕( system.web.ui.htmlcontrols ) 和 ASP.NET服務端按鈕 (

          system.web.ui.webcontrols ) 為例:
          1. HTML控件,如IMG的輸入按鈕等 的 onclick ( 客戶端 )和 onserverclick( 服務端 )事件,

          注意 runat="server"。。。
          <input type="button" id="btn" name="btn" value="button" runat="server" onserverclick="ServerSideEvent" onclick="SideScript" />

          此處的onclick先于onserverclick執行。
          2.WEB控件,如<asp:button 按鈕等 的 onclientclick( 客戶端 ) 和 onclick ( 服務端 )事件。。。

          <asp:Button ID="btn" text="button" runat="server"  OnClick="SideScript" OnClientClick="ClientSideScript" />

          此處的onclientclick先于onclick執行。
          onclientclick  事件來自2.0, 它用來取代下列代碼。。。

          btn.Attributes.Add("onclick","SideScript");

          總之,客戶端點擊事件先于服務器端點擊事件執行。也就是說,先執行客戶端的Javascript,然后

          到服務器端執行服務器的相關代碼。

           

          <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"Inherits="CaiPiao.WebForm2" %>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head runat="server">
              
          <title></title>
              
          <script type="text/javascript">
                  
          function fnIsSearch() {
                      
          return true;
                  }
              
          </script>
          </head>

          <body>
              
          <form id="form1" runat="server">
              
          <div>
              
          <input id="btnImport" type="submit" runat="server" value="導出" onserverclick="btnImport_Click"/>
              
          </div>
              
          </form>
          </body>
          </html>
          后臺代碼
          [code=C#]
          using System;
          using System.Collections.Generic;
          using System.Web;
          using System.Web.UI;
          using System.Web.UI.WebControls;

          namespace CaiPiao
          {
              public partial class WebForm2 : System.Web.UI.Page
              {
                  protected void Page_Load(object sender, EventArgs e)
                  {
                      this.btnImport.Attributes.Add("onclick", "return fnIsSearch();");
                  }
                  public void btnImport_Click(object sender, EventArgs e)
                  {
                      Response.Write("測試");
                  }
              }
          }

          posted on 2012-07-17 14:30 ** 閱讀(1027) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          收藏夾

          C#學習

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 军事| 华蓥市| 新昌县| 聂拉木县| 景德镇市| 武陟县| 大荔县| 泰来县| 洛隆县| 滦平县| 濮阳市| 米泉市| 温宿县| 通州市| 内乡县| 陇南市| 南开区| 文安县| 宝山区| 洪泽县| 静宁县| 奉节县| 资中县| 万盛区| 齐河县| 土默特左旗| 佛坪县| 彝良县| 周宁县| 历史| 南岸区| 扎鲁特旗| 酉阳| 吉安县| 高陵县| 阿勒泰市| 嫩江县| 临泉县| 黄冈市| 什邡市| 贵阳市|