A good user interface reminds the user to confirm whether to continue the selected operation before performing data operations, especially data modification and deletion operations, to prevent user errors, for winform applicationsProgramIt is very simple, but the webform application is relatively difficult.
The principle is also very simple, that is, to confirm the events that cause PostBack to the server on the page. This can be done through the client's JavaScript. You can use window. the confirm method is used for user confirmation, and then window is used. event. returnvalue = false; to cancel the current event (that is, to cancel the onclick method, so you will not post back to the server, so you will not execute your update, delete the logic ).
1. First, add a JavaScript function to the page.
<Script language = "JavaScript">
Function confirm_click ()
{
VaR result = Window. Confirm ("are you sure you want to continue? ");
If (result = false)
{
Window. event. returnvalue = false;
}
}
</SCRIPT>
2. Add the onclick event processor (VB. NET) to the control in the page_load event on the server side)
Btnsearch. Attributes. Add ("onclick", "confirm_click ();")
Or write JavaScript directly.Code
Btnsearch. Attributes. Add ("onclick", "Return confirm (" "Are you sure you want to delete? "");")
The preceding settings are for a single control. script code can be directly added, which is relatively simple. However, the various button columns in the DataGrid are complicated, but there are many such applications, such as editing columns in the DataGrid. The following describes my implementation methods.
In fact, just like adding an event processor to a single control, find all the button controls in the column of the DataGrid and add the event processor one by one. Here we need to use the itemcreated event of the DataGrid, in this event, traverse all controls contained in a (OR) cell in the datagriditem and add the event processor to the corresponding control. The sample code is as follows:
Private sub dgresultsgrid_itemcreated (byval sender as object, byval e as system. Web. UI. webcontrols. datagriditemeventargs) handles dgresultsgrid. itemcreated
Dim DGI as datagriditem = E. Item
If (DGI. itemtype = listitemtype. Item orelse DGI. itemtype = listitemtype. alternatingitem) then
'In this example, only the row item and alternatingitem of the DataGrid are checked.
Dim C as control
For each C in DGI. cells (2). Controls 'only check the linkbutton control contained in column 3rd
If typeof C is linkbutton then
Dim LBC as linkbutton = C
LBC. Attributes. Add ("onclick", "confirm_click ();") 'add an event processor to the control
End if
Next
End if
End sub
In this way, the running result is that the validation code is added to the linkbutton in column 3rd of all rows in the DataGrid.