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:
Note that the 'return', this is not the province, or even if the user point of cancellation, the data will still be saved.
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 . Controls ;
string strClientID = btnSave.ClientID;
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).
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, "");
3script = script.Replace ("EVAL_MESSAGE", strCMD);
4ddlTest.Attributes.Add ("onchange", script);
This code select generated after the implementation of controls will be like this:
ConfirmUpdate function as follows
2 function ConfirmUpdate (cmd) (
3 if (confirm ("Are you sure to update?"))
5 eval (cmd);
9 window.location.href = "default.aspx"
This article comes from CSDN blog, reproduced, please indicate the source: http://blog.csdn.net/21aspnet/archive/2007/03/20/1535628.aspx