ASP.NET controls to add the common JavaScript operation

1. Add a validation function for the button control

To add a client for the server control events, need to use Attributes property. Attributes property is all the server controls have a property that is used to generate the HTML for the end to add some custom tags. Web Form on the assumption that there is a Save button btnSave, want to prompt the user when the user Click the button really want to save (for example, can not be resumed once saved, etc.), should be in the Page_Load event add the following code:

1btnSave.Attributes.Add ("onclick", "Javascript: return confirm ('Are you sure tosave ?');")
Note that the 'return', this is not the province, or even if the user point of cancellation, the data will still be saved.

2. Datagrid in each row for the add Javascript events

Datagrid in the child control is no way to directly access, and to realize that the above results, we need to use Datagrid in OnItemDataBound event. OnItemDataBound occurred in Datagrid of each row after the data bound to the Datagrid (which inspired a line). First, add a statement in Datagrid OnItemDataBound property, as follows:

Here's OnItemDataBound ItemDataBound method is called when the event occurs, the code behind file, add this method definition:
private void OnItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
if (e.Item.ItemType! = ListItemType.Header & & e.Item.ItemType! =
LinkButton btnSave = (LinkButton) e.Item.Cells [2]. Controls [0];
string strClientID = btnSave.ClientID;
/ / Get the control to the client ID, call for JavaScript
btnSave.Attributes.Add ("onclick", "Javascript: return confirm ('Are you sure to save ?');");

Datagrid row as the title and footnote lines will also stimulate this event, so first of all to judge the line is not excited for this event header row and footer row. It is assumed that btnSave button in Datagrid section 3 (first column is 0).

3. In the Javascript events to trigger server-side control

Let us again consider the first instance, we increased the Save button to confirm function, only the user will confirm the save operation, the user does not confirm if not execution, if we want the user presses the "Cancel", the implementation of other operation of how to do? This would need to use JS to callback (PostBack) server-side control to complete the operation.

The current page has a DropDownList control ddlTest, Button button btnSave. When choosing ddlTest trigger onchange event, save the current selection value in the preservation of the former allow the operator to confirm the first user to confirm the save, or move default.aspx page.

In the Page_Load event add the following code:
1string strCMD = Page.GetPostBackClientHyperlink (btnSave, "");
2string script = @ "Javascript: ConfirmUpdate (" "EVAL_MESSAGE "");";
3script = script.Replace ("EVAL_MESSAGE", strCMD);
4ddlTest.Attributes.Add ("onchange", script);
This code select generated after the implementation of controls will be like this:
1select name = "ddlTest" onchange = "Javascript: ConfirmUpdate (" javascript: __doPostBack ('btnSave','')");">
ConfirmUpdate function as follows

1 <SCRIPT language=javascript>
2 function ConfirmUpdate (cmd) (
3 if (confirm ("Are you sure to update?"))
4 (
5 eval (cmd);
7 else
8 (
9 window.location.href = "default.aspx"
12 </ SCRIPT> here to use the Javascript eval function to call a string containing the command. Note that a string containing the command can not be enclosed in single quotes, because automatically generated script to include single quotes, so here are two double quotes with double quotes the string itself.

This article comes from CSDN blog, reproduced, please indicate the source:

分类:DotNet 时间:2010-03-30 人气:382
blog comments powered by Disqus


  • Change the front control code-behind CSS, javascript events to increase 2010-11-27

    WebControl class has a property: Attributes (acquisition and control of the property does not correspond to any feature (only for rendering) of the collection.) Property value name and value pairs AttributeCollection. This property can be changed thr

  • 解析DHTML,JavaScript,DOM,BOM以及WEB标准的描述 2013-10-28

    本篇文章是对DHTML,JavaScript,DOM,BOM以及WEB标准进行了详细的描述介绍,需要的朋友参考下 DHTML:它不是一门新的语言,而是HTML.CSS和JavaScript这三者相结合的产物,DHTML包含如下3个含义: (1)通过HTML把网页标记为各个元素 (2)利用CSS设计元素之间的排版样式,并控制各个标记的位置 (3)使用js来控制各个标记 JavaScript由三部分组成: (1) ECMAScript是一种由欧洲计算机制造商协会(ECMA)通过ECMA-262标准化

  • JavaScript中遍历对象的property的3种方法介绍 2014-05-15

    这篇文章主要介绍了JavaScript中遍历对象的property的3种方法介绍,本文先是讲解了3种方法并用一张图片加深理解,然后给出代码实例,需要的朋友可以参考下 在JavaScript中,可以用三种方法来遍历对象的property: 1.for/in.可以使用for/in语句遍历对象自身的property(Own Property)及其从原型对象处继承的property,只有enumerable的property才会被遍历到. 2.Object.keys().可以将对象作为参数传入Objec

  • 由JavaScript技术实现的web小游戏(不含网游) 2015-01-23

    伴随Ajax与网页游戏的崛起,曾几何时JavaScript也成了游戏开发时可供选择的技术之一,文本 仅列举数项由JavaScript技术实现的web小游戏(不含网游),聊作参考之用. 1.Mario(游戏地址: ) 传说中的马里奥网页版,一比一实现了红白机时代超级马里奥中所有功能 与关卡,精细程度不逊原版游戏. 依 赖库:无 2 . Bunny Hunt (游戏地址:

  • 有了 Docker,用 JavaScript 框架开发的 Web 站点也能很好地支持网络爬虫的内容抓取 2014-06-18

    Prerender 服务能够为网络爬虫提供预先渲染的动态页面内容,解决了用 JavaScript 框架构建的 Web 站点不支持爬虫抓取的问题.本文详细描述了一种解决方案,尤其是提供了集成 Prerender 服务的 Docker 容器镜像. 如果你正在使用 AngularJS 构建一个面向大众消费者的应用,你肯定希望用户能把它分享到社交媒体上.对于特定的应用而言,丰富的社交分享差不多是最重要的营销渠道.所谓「丰富的社交分享」,是指像下面这样的分享: 可以看到, Facebook, Twitte

  • 9 个基于JavaScript 和 CSS 的 Web 图表框架 2015-01-15

    jQuery, MooTools, Prototype 等优秀的 JavaScript 框架拥有各种强大的功能,包括绘制 Web 图表,使用这些框架以及相应插件,我们可以非常轻松地实现曲线图,圆饼图,柱状图等 Web 图表的绘制,而不必象以往那样通过复杂的 Flash 技术实现.本文介绍了9个优秀的基于 JavaScript 与 CSS 的 Web 图表框架. 1. Flot Flot 是一个纯粹的 jQuery JavaScript 绘图库,可以在客户端即时生成图形,使用非常简单,支持放大缩小

  • Analysis of JavaScript events. And the capture and bubbling 2010-08-18

    Document model in the browser DOM, the event is for some specific interactions occur, the role of the elements to be issued by the then process the response by the browser. Common events: click, onmouseover, onblur, etc.. DOM (Document Object Model)

  • collection of commonly used validation javascript 2010-09-07

    Website often used to do the input text javascript validation, I do network and convenient products, it is on the ip address, mac address authentication uses a little more Normally used to collect some of the verification function. Verify IP: <script

  • Reference javascript events 2010-10-09

    Event source object event.srcElement.tagName event.srcElement.type Capture Release event.srcElement.setCapture (); event.srcElement.releaseCapture (); Events button event.keyCode event.shiftKey event.altKey event.ctrlKey Event return values event.ret

  • Understanding JavaScript events 2010-12-20

    In many language learning, the "event" is a more difficult to understand, but also a very important concept. JavaScript event handler in the same, is because of event handling, Ajax drag effect will appear. JavaScript in this article to discuss

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.034 (s). 13 q(s)