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 人气:356
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 through

  • 解析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技术实现的web小游戏(不含网游) 2015-01-23

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

  • 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) struc

  • 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: <scr

  • 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.returnVa

  • 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

  • javascript events 2009-03-04

    onrowsinserted IE5, N the current data source will have to insert a new data record when triggered by this event External events onafterprint IE5, N when the document is printed after the trigger of this incident onbeforeprint IE5, N when the documen ...

  • javascript events Daquan 2008-05-07

    General Event Browser Support Description onClick IE3 | N2 | O mouse click event is used in an object within the control of the mouse click onDblClick IE4 | N4 | O Mouse double-click the event onMouseDown IE4 | N4 | O mouse button is pressed on the onMous

  • Common data validation javascript 2010-03-29 <script language="javascript"> / * The following function return value Description: All "Is" at the beginning of the function, if it is "Is" after the specified s

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.368 (s). 14 q(s)