. NET for JavaScript operations

Source: Internet
Author: User
Tags add eval header net return window client javascript eval
. NET for JavaScript operations. The following is a summary of some common JavaScript operations in asp.net:

1. To add a confirmation feature to a button control

To add a client-side event for a server control, you need to use the Attributes property. The Attributes property is a property of all server controls that is used to add custom tags to the resulting HTML. If you have a Save button btnsave on the Web form, and you want to be prompted when the user points to this button to be sure you want to save it (for example, once you save it, and so on), you should add the following code to the Page_Load event:

BTNSAVE.ATTRIBUTES.ADD (

"onclick", "Javascript:return Confirm" (

' Are you sure to save '); \

”)


Note that ' return ', which is not a province, otherwise, even if the user points to cancel, the data will still be saved.


2. Add a JavaScript event for each row in the DataGrid


The child controls in the DataGrid are not accessible directly, and to achieve that effect, we need to use the Onitemdatabound event of the DataGrid. The Onitemdatabound event occurs after each row of data in the DataGrid is bound to a DataGrid (that is, one row fires once). First, add the Onitemdatabound attribute in the declaration of the DataGrid, as follows:


This describes the call to the ItemDataBound method when the Onitemdatabound event occurs, adding the definition of this method in the code-behind file:


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;

Gets the client ID of the control that is available for JavaScript to call

BTNSAVE.ATTRIBUTES.ADD ("onclick", "Javascript:return confirm

(' Are you sure to save? ');

");

}

}


Because the header row and footer row of the DataGrid also fire this event, it is first judged that the row that fired this event is not the header row and the footer row. This assumes that the Btnsave button is in the 3rd column of the DataGrid (the first column is 0).


3. Triggering server-side control events in JavaScript


Let us consider the first instance, we have added a confirmation function for the Save button, only after the user confirmed that the save operation will be performed, the user does not confirm the words will not be executed, if we want the user to press "Cancel", do the other operation how to do? This will require the use of JS back and forth (postback) server-side controls to complete the operation.


There is a DropDownList control ddltest in the current page. button Buttons Btnsave. When the Ddltest is selected to trigger the onchange event, save the current selection value, before saving will let the operator first confirm, the user confirms then save, otherwise turn to Default.aspx page.


Add the following code to the Page_Load event:


String strcmd = Page.getpostbackclienthyperlink (Btnsave, "");

string script = @ "Javascript:confirmupdate" ("Eval_message"); ";

script = script. Replace ("Eval_message", strcmd);

DDLTEST.ATTRIBUTES.ADD ("onchange", script);


The select control that is generated after this code executes is this:


〈select name= "Ddltest" id= "Ddltest"

Onchange= "Javascript:confirmupdate (" Javascript:__dopostback "(' btnsave ', ')");

" 〉


The Confirmupdate function is as follows

〈script language=javascript〉

function Confirmupdate (cmd) {

if (Confirm ("Are you sure to update?") {

eval (cmd);

}

else {

Window.location.href= "Default.aspx"

}

}

〈/script〉


This uses the JavaScript eval function to invoke a command contained in a string. Note that the string containing the command cannot be enclosed in single quotes because the automatically generated script includes single quotes, so here are two double quotes that represent the double quotes of the string itself.


The following are some simple JavaScript operations in asp.net:


1. Open a new window


This simple: Response.Write (@ "< script


language= ' JavaScript ' >window.open (' url ');</script> ");


2. Close the window


Closes the current window and prompts the user to close, yes close, no exit


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


Delay Close the window (the following code indicates 2 seconds to close without confirmation)


Response.Write (@ "< script


language= ' JavaScript ' >settimeout (' self.close () ', Watts);</script> ");


3. Delay Time


This is not much different from the above. The situation I use is that when the user is finished giving a prompt, "n seconds later, the page


Steering "and so on, just remove the 2 self.close ()


Response.Write (@ "< script


language= ' JavaScript ' >settimeout (", Watts);</script>");


4. Pop-up prompts or Warning window


Response.Write (@ < script language= ' JavaScript ' >alert (' Add success, 2 seconds after page


will automatically jump ');</script> ");


5. Refresh other Pages


The use of the situation is still quite a lot. For example, in the face of data updates and modifications on page B, another page A should keep the latest data


To the customer, which is to refresh a when the B operation is complete:


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


href= './default.aspx ' </script> ');


6. Page Jump


Sometimes learning to give a hint in the case of a page jump, can not use Response.Redirect ("url");


For example, when the customer completes the operation, click the button to submit, pop-up prompt box (using 3 and 4 above), if the use of the


Response.Redirect ("url");


Then the page will not give a hint, the page is 3 and 4 no effect on the direct turn.


If you are following the process of operation:


1). Response.Write (@ < script language= ' JavaScript ' >alert (' Add success, 2 seconds after page


Face will automatically jump ');</script> ");


2). Response.Write (@ "< script


language= ' JavaScript ' >settimeout (", Watts);</script>");


3). Page Turn:


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


Conte

nt= ' 0; Url=./default.aspx ' > ');


I don't know how to do it with JavaScript.
The above is the JavaScript operation in the common asp.net.



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.