Javascript operations in ASP. NET

Source: Internet
Author: User
Tags javascript eval

The following summarizes some common javascript operations in ASP. NET:

1. Add confirmation for the button control

To add client events to server controls, you must use the Attributes attribute. The Attributes attribute is an attribute of all server controls. It is used to add custom tags to the final HTML. Assume that there is a Save button btnSave on the Web Form, and you want to prompt the user whether to save the button (for example, once saved, it cannot be restored) When you click this button ), the following code should be added to the Page_Load event:

 
 
  1. btnSave.Attributes.Add(“onclick”,“javascript:return confirm(‘Are you sure to save?’);”) 

Note that 'return' is not economical. Otherwise, even if the user clicks cancel, the data will still be saved.

2. Add Javascript events for each row in the Datagrid

The child control in the Datagrid cannot be directly accessed. To achieve the above effect, we need to use the OnItemDataBound event of the Datagrid. The OnItemDataBound event occurs when each row of data in the Datagrid is bound to the Datagrid, that is, a row is triggered once ). First, add the OnItemDataBound attribute in the Datagrid declaration, as shown below:

The ItemDataBound method is called when the OnItemDataBound event occurs. Add the definition of this method to the Code POST file:

 
 
  1. Private VoidOnItemDataBound (ObjectSender, System. Web. UI. WebControls. DataGridItemEventArgs e)
  2. {
  3. If(E. Item. ItemType! = ListItemType. Header & e. Item. ItemType! = ListItemType. Footer)
  4. {
  5. LinkButton btnSave = (LinkButton) e. Item. Cells [2]. Controls [0];
  6. StringStrClientID = btnSave. ClientID;// Obtain the Client ID of the control, which can be called by JavaScript. 
  7. BtnSave. Attributes. Add ("Onclick","Javascript: return confirm ('Are you sure to save? ');");
  8. }
  9. }
  10.  

Because the row of the title and footer of the Datagrid will also trigger this event, you must first determine that the row that inspires this event is not the row of the title and footer. Here we assume that the btnSave button is located in the first column of column 3rd of the Datagrid is 0 ).

3. Trigger server-side control events in Javascript

Let's take another look at the first instance. We have added the confirmation function for the Save button. The save operation will only be executed after the user confirms it. If the user does not confirm it, it will not be executed, what if we want another operation to be performed when the user presses "cancel? This requires the use of JS back-and-forth (PostBack) server-side controls to complete the operation.

The current page has a DropDownList control ddlTest, Button btnSave. when ddlTest is selected, the onchange event is triggered to save the current selected value. Before saving the event, the operator is asked to confirm the event and the user confirms the event. Otherwise, the event is switched to default. aspx page.

Add the following code to the Page_Load event:

 
 
  1. string strCMD = Page.GetPostBackClientHyperlink( btnSave, "" );  
  2.      string script = @"javascript:ConfirmUpdate(""EVAL_MESSAGE"");";  
  3.      script = script.Replace( "EVAL_MESSAGE" , strCMD );  
  4.  
  5.      ddlTest.Attributes.Add("onchange",script);  
  6.  

The select Control generated after this code execution is as follows:

 
 
  1. <Select name ="DdlTest"Id ="DdlTest"Onchange ="Javascript: ConfirmUpdate ("Javascript :__ doPostBack ('Btnsave',')");"> 〉
  2.  
  3. The ConfirmUpdate function is as follows:
  4.  
  5. <SCRIPT language = javascript> 〉
  6. Function ConfirmUpdate (cmd ){
  7. If(Confirm ("Are you sure to update? "))
  8. {
  9. Eval (cmd );
  10. }
  11. Else 
  12. {
  13. Window. location. href ="Default. aspx" 
  14. }
  15. }
  16. </SCRIPT> 〉
  17.  

The Javascript eval function is used to call the commands contained in a string. Note that strings containing commands cannot be enclosed by single quotation marks. Because the automatically generated script contains single quotation marks, two double quotation marks are used to represent the double quotation marks of the string.

Below are some simple javascript operations in ASP. NET:

1. open a new window

This is simple: Response. Write (@ "<script

Language = 'javascript '> window. open ('url'); </script> ");

2. Close the window

// Close the current window and prompt the user to close it. yes: close. no: exit.

Response. Write (@ "<script language = 'javascript '> window. close (); </script> ");

// Delayed closing window (the following code indicates closing in 2 seconds without confirmation)

Response. Write (@ "<script

Language = 'javascript '> setTimeout ('self. close ()', 2000); </script> ");

3. Delay Time

This is not much different from the above. I used the case that, after the user operation is complete, a prompt is displayed, "n seconds later, the page

You only need to remove the double Self. close () to the switch.

Response. Write (@ "<script

Language = 'javascript '> setTimeout ('', 2000); </script> ");

4. A prompt or warning window is displayed.

Response. Write (@ "<script language = 'javascript '> alert ('added successfully, page 2 seconds later

Will automatically Jump '); </script> ");

5. Refresh other pages

There are still many situations in this use. For example, data is updated and modified on page B, and A must keep the latest data on the other page.

To the customer, we need to refresh A after the operations on B are completed:

Response. Write (@ "<script language = 'javascript '> window. opener. location.

Href = './default. aspx' </script> ");

6. page Jump

Sometimes you can jump to a page when you are learning to give a prompt. You cannot use Response. Redirect ("url ");

For example, after the customer completes the operation, click the button to submit. A prompt box is displayed (Use 3 and 4 above ).

Response. Redirect ("url ");

The page will not be prompted, and the pages 3 and 4 will go directly if they do not work.

If you are using the following procedure:

1). Response. Write (@ "<script language = 'javascript '> alert (' added successfully, page 2 seconds later

The plane automatically jumps '); </script> ");

2). Response. Write (@ "<script

Language = 'javascript '> setTimeout ('', 2000); </script> ");

3) page orientation:

Response. Write ("<meta http-equiv = 'refresh'

Content = '0; URL =./default. aspx '> ");

// I don't know how to implement it using javascript.

These are common javascript operations in ASP. NET.

  1. Implementation of ASP. NET form Authentication
  2. An error occurred while submitting the asp.net Form using JQuery Form Ajax.
  3. Summary of ASP. NET installation and deployment problems
  4. Experience in running ASP. NET on APACHE
  5. ASP. NET Online Learning Resource Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.