ASP.NET controls to add the common JavaScript operation

sponsored links
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! =
ListItemType.Footer)
(
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);
6)
7 else
8 (
9 window.location.href = "default.aspx"
10)
11)
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: http://blog.csdn.net/21aspnet/archive/2007/03/20/1535628.aspx
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of ASP.NET controls to add the common JavaScript operation

  • Software development sunflower Baodian [reprint]

    Master the ability to reuse code very familiar with the new API's fast. This is because, he once used a lot of the API, have a lot of reusable code. He knows what is available and what is deficient. He has been using Qt, also used by gtk +, also used

  • Detailed JS regular expressions

    Opening, or have to talk about ^ and $ that they are separately used to match the beginning and end of the string, the following examples to illustrate separately "^ The": there must be at the beginning of "The" string; "of despai

  • JavaScript inheritance

    About JavaScript inherited a small example ... <! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 / / EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" conte ...

  • JavaScript String Functions Guinness

    Own JS function 1.Asc (x), Chr (x): convert characters, character code 2. Filter: Search string array of a specific string Format: v = filter (x, s [, include [, compare]]) Examples: Dim x ()={" kjwang "," wangkj "," peter ")

  • EJB ant script to deploy template works

    <? xml version = "1.0" encoding = "UTF-8"?> <! - Name Project name basedir build.xml file directory -> <project name="HelloWorld" basedir="."> <! - Property variables -> <! - The sour ...

  • java read file

    java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa

  • Hibernate pessimistic locking mechanism for locking and optimistic locking

    hibernate lock mechanism 1. Pessimistic lock It refers to the modification of data by outsiders hold a conservative attitude. The assumption that at any time access to data, may also have another client to access the same data, in order to maintain t ...

  • Java technology: Eclipse explain the use of techniques

    Editor settings: Window -> Preferences -> Java-> Editor appearance: Display line number, emphasizing symmetry shown in square brackets, to emphasize that the existing line to show Print Margins its check, Tab width set 4, print made from the fiel

  • can not be represented as java.sql.Timestamp

    Development of procedures for the use of hibernate when, some time there is no need to fill in the fields, but after the hibernate query time reported "Java.sql.SQLException: Value'0000-00-00 'can not be represented as java.sql.Timestamp ...

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries