在点击保存时通常会验证输入框是否为空,一般我们会在按钮控件中添加OnClientClick=“return Check();”事件,并通过javascript来处理。

下面是另一种方法,在后台.cs代码中使用Attrbutes为控件添加事件。

1.

<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function Check() {
var name = document.getElementById("txtName");
var number = document.getElementById("txtNumber"); if (name.value.trim() == "") {
alert("请输入姓名!");
return false;
}
if (number.value.trim() == "") {
alert("请输入学号!");
return false;
} return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
学号:<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox> <asp:Button ID="btnSave" runat="server" Text="保存" onclick="btnSave_Click"/>
</form>
</body>
</html>

前台html代码

如果不在后台添加事件,只需改动<asp:Button ID="btnSave" runat="server" Text="保存" OnClientClick="return Check();"/>即可

2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.btnSave.Attributes.Add("onclick", "return Check()");
//或者下面的写法
//this.btnSave.Attributes["onclick"] = "return Check()";
} protected void btnSave_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('保存成功')</script>");
}
}
}

后台.cs代码

3.结果

asp.net中控件的Attributes用法的更多相关文章

  1. ASP.NET中控件命名规则

    控件名 简写 控件名 简写 Web 窗体 Label lbl TextBox tb Button btn LinkButton lb HyperLink hl Repeator rpt ImageBu ...

  2. ASP.NET控件之RadioButtonList

    “RadioButtonList”控件表示一个封装了一组单选按钮控件的列表控件. 可以使用两种类型的 ASP.NET 控件将单选按钮添加到网页上:各个“RadioButton”控件或一个“RadioB ...

  3. duilib中控件拖拽功能的实现方法(附源码)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...

  4. 关于ASP.NET控件方面的学习(恢复版)

    前段时间没有把学习中的遇到的问题和解决方法详细总结,今天整理整理.. 鉴于我们这个研究生论文管理系统是小组形式,所以说虽然我只负责数据库,但是其它部分也多少有些工作方面的涉及,最后感谢各位同学和老师的 ...

  5. 【VS开发】VS2010 MFC中控件、对话框等背景颜色动态修改的方法

    [VS开发]VS2010 MFC中控件.对话框等背景颜色动态修改的方法 标签(空格分隔):[VS开发] 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明: ...

  6. asp.net <asp:Content>控件

    <asp:Content ID="Content2" ContentPlaceHolderID="CPH_MainContent" runat=" ...

  7. FineUI 基于 ExtJS 的专业 ASP.NET 控件库

    FineUI 基于 ExtJS 的专业 ASP.NET 控件库 http://www.fineui.com/

  8. ASP.NET泛型List的各种用法Skip、Take等

    List在.NET里面使用得非常频繁,但有好多人不了解它各种小用法.我就一直记不大住... asp.net中List的简单用法,例如: 1 2 3 4 5 6 7 List<int> li ...

  9. ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系

    ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...

随机推荐

  1. FFmpeg - 音频解码过程

    1. 注册所有解码器 av_register_all(); 2. Codec & CodecContext AVCodec* codec = avcodec_find_decoder(CODE ...

  2. Shell 操作练习2

    #! /bin/sh ############################### # -- # # author jackluo # # net.webjoy@gmail.com # ###### ...

  3. Java 读写图像

    Java中进行图像I/O(即读图片和写图片,不涉及到复杂图像处理)有三个方法:1. Java Image I/O API,支持常见图片,从Java 2 version 1.4.0开始就内置了.主页:h ...

  4. iPhone开发常问的十个问题

    iPhone开发常问的十个问题 前言 今天去stackoverflow.com上看了一下iPhone标签下排名最高的10个问题,将它们整理出来,希望这些常见问题能帮到一些iPhone开发的初学者.本来 ...

  5. PHP5 session 详解【经典】 -- 转帖

    PHP5 session 详解[经典] http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越 ...

  6. JS - 超强大文本动画插件Textillate.js

    http://www.yyyweb.com/demo/textillate/ Textillate.js AsimplepluginforCSS3textanimations.

  7. 在windows下使用gnu的工具

    mingw Minimalist GNU for Windows http://www.mingw.org/ 想要在Windows使用wget,之前使用的是单独的安装包,现在又想使用bash的命令提示 ...

  8. A trip through the Graphics Pipeline 2011_03

    At this point, we’ve sent draw calls down from our app all the way through various driver layers and ...

  9. swfit-pod使用

    一.查询第三方版本号 pod search  SDWebImage 二.项目添加pod 1.在终端打开项目路径 2.输入  pod init 生成Podfile 三.在Podfile输入需要的第三方 ...

  10. Xamarin迁移到 Unified API 注意事项

    参考官方文档: Migrating to Unified API for Components #if __UNIFIED__ ... // Mappings Unified CoreGraphic ...