Personal notes on software development.
For Java technologies check my dedicated site

Pages

Adding Client Side delete confirmation [1][2]:

The JavaScript confirm(string) function displays its string input parameter as the text inside a modal dialog box that comes equipped with two buttons - OK and Cancel (see Figure 1). The confirm(string) function returns a Boolean value depending on what button is clicked (true, if the user clicks OK, and false if they click Cancel).
The JavaScript confirm(string) Method Displays a Modal, Client-Side Messagebox
During a form submission, if a value of false is returned from a client-side event handler then the form submission is cancelled.
Adding client-side script to a Button, LinkButton, or ImageButton client-side onclick event can be accomplished through the use of the OnClientClick property.

<asp:LinkButton ID="DeleteButton" runat="server"
   CausesValidation="False" CommandName="Delete" Text="Delete"
   OnClientClick="return confirm('Are you sure you want to delete this product?');">
 </asp:LinkButton>
In this example, Clicking the Delete button brings up the confirm dialog box. If the user clicks Cancel, the postback is cancelled and the product is not deleted. If, however, the user clicks OK, the postback continues and the ObjectDataSource Delete() method is invoked, culminating in the database record being deleted.

When working with a Button, LinkButton, or ImageButton directly in a template, a confirmation dialog box can be associated with it by simply configuring its OnClientClick property to return the results of the JavaScript confirm(string) function. However, the CommandField - which adds a field of Delete buttons to a GridView or DetailsView - does not have an OnClientClick property that can be set declaratively. Instead, we must programmatically reference the Delete button in the GridView or DetailsView s appropriate DataBound event handler, and then set its OnClientClick property there.
Althought it is possible to configure the OnClientClick  property for buttons inside a CommandField this will hardcode some index values to access the button inside the ComandField and a better approach is to convert the CommandField into a TemplateField and then use the OnClientClick directly on the button (has we saw above).

These buttons OnClientClick properties can be assigned declaratively, as we saw above, or can be programmatically accessed in the appropriate DataBound event handler (when using a GridView for example) using the following pattern:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
    ButtonType obj = (ButtonType) e.Row.FindControl("controlID");
}

Related articles:
[1] - Adding Client-Side Confirmation When Deleting
[2] - Using JavaScript's confirm() Method to Control Form Submission

Todo:

 Checking All CheckBoxes in a GridView Using Client-Side Script and a Check All CheckBox

No comments:

Post a Comment