1. Add a confirmation function for the Button control
To add client events to server controls, you need to use Attributes properties. Attributes property is an attribute that all server controls have, which is used to add some custom tags for the final generated HTML. Suppose there is a save button BTnsave on the web form, I hope that the user is not saved when the user clicks this button (such as the saving cannot be recovered), should be added to the Page_Load event:
Btnsave.attributes.add ("onclick", "JavaScript: Return Confirm ('Are you suing to save?');")
It should be noted that 'return', which is inevitably, otherwise the data will still be saved even if the user is canceled.
2. Add JavaScript events for each line of DataGrid
The child control in DataGrid is no way to access directly. To implement the above effect, we need to use the DataGrid onItemDatabase. OnItemDatabase happens after the DataGrid's data is bound to the DataGrid (ie, one line is excited). First add the onItemDatabase attribute in the DataGrid declaration, as follows:
Here, the ItemDatabase method is called when the onItemDatabase event occurs, and the definition of this method is added to the code post 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 strphone; // Get the client ID of the control, available for JavaScript call btnsave.attributes.add ("Onclick", "JavaScript: Return Confirm ('Are you have to save?'); ");}}
Since DataGrid's header and footnotes also inspire this event, first determined that the row that excited this event is not the title line and footnotes. Here, it is assumed that the BTnsave button is located in the third column of DataGrid (first column is 0).
3. Trigger server-side control events in JavaScript
Let's consider the first instance, we add a confirmation function for the save button, only if the user is confirmed, the user does not perform, if we want the user to press "Cancel", What should I do with another operation? This requires the operation of the POSTBACK server-side control.
There is a DROPDOWNLIST control DDLTEST, button button Btnsave. When DDLTEST is selected, save the current select value, let the operator will confirm before saving, the user confirms, saves, otherwise turn to the Default.aspx page.
Add the following code in 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 generated after this code execution will be like this:
The ConfirMUpdate function is as follows