在C#中如果是asp控件的button有两个click的调用,一个是OnClick,一个是OnClientClick。那么这两者有什么区别呢,下面就来说说区别。

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Page_Load" OnClientClick="one()" /> 

首先你会看到这个button前面有asp:,表示是一个asp控件。

runat="server",这是表示这个控件能请求服务端,也就是后端,如果加了这句,那么后端就能直接通过ID来操作这个控件,如:赋值;后端代码就可以写成this.Button1.Text = "";

说会正题 onClick是执行服务器端的操作,而onClientClick是执行前端的方法操作,那么谁先执行呢,onClientClick先执行,若onClientClick中的方法返回true才会执行onClick中的后端方法。

如果让这两个方法同时起作用,则要注意OnClientClick=“return 方法名称(); 若不加return,那么不管OnClientClick的返回结构是true或false,OnClick事件都会执行。

前端代码:

所有服务器控件必须出现在 <form> 标签内,同时 <form> 标签必须包含 runat="server" 属性。

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Page_Load" OnClientClick="return one()" />
</form>
</body>
</html>
<script type="text/javascript">
function one() {
document.getElementById("Button1").innerText = "你好";
return false;
}
</script>

  后端:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication2
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ Button1.Text = "www";
}
}
}

  

最新文章

  1. NYOJ 205
  2. C++学习笔记(2)
  3. 关于int,integer初始值问题
  4. iOS 4种开发者身份的官方说明
  5. 使用subclass UIActivity的方案来分享图片
  6. [Docker] docker 基础学习笔记6(共6篇)
  7. 阿里云ecs云服务器安装wdcp控制面板教程
  8. ASP.NET使用Razor语法无法正确识别.cshtml文件
  9. 传说中的WCF(3):多个协定
  10. 解决:Unable to connect to repository https://dl-ssl.google.com/android/eclipse/site.xml
  11. javascript禁用与启用select标签(实用版)
  12. Visual Studio2013使用Microsoft Office Document Imaging(MODI)的方法
  13. java中关于移位运算符的demo与总结
  14. HDOJ/HDU 2568 前进(简单题)
  15. 用Jpush极光推送实现抓取特定某个用户Log到七牛云服务器
  16. linux相关小工具的使用(一)————代码相关工具
  17. CSS学习笔记一:css 画平面图形
  18. Springboot中使用Scheduled做定时任务
  19. shell文本左右对齐排版【转】
  20. oracle 11g禁用和强制direct path read

热门文章

  1. Adobe Photoshop CS6下载安装
  2. 121. Best Time to Buy and Sell Stock——Leetcode
  3. element el-tooltip 内容换行的方法
  4. 13、SpringBoot------整合shiro
  5. Linux tmpwatch命令
  6. java基础IO流 复制键盘录入的目录,复制其中的.java文件到指定目录,指定目录中有重名,则改名 对加密文件计算字母个数
  7. java对一个int数组进行排序、去重
  8. 洛谷题解:P1209 【[USACO1.3]修理牛棚 Barn Repair】
  9. lintcode_93_平衡二叉树
  10. ABAP调用WebService时日期类型问题