Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  38] [ 4]  / answers: 1 / hits: 36101  / 12 Years ago, fri, january 18, 2013, 12:00:00

I have return one Javascript function which asks the confirm message to the user.
The JavaScript functions is



    function ConfirmOnDelete() {
if (confirm(Are you sure to delete?))
return true;
else
return false;


and GridView is like below:



    <asp:CommandField HeaderText=Delete ShowDeleteButton=True />


Here I want to call the JavaScript function when user clicking the Delete in the GridView Command Field.
How to call this?


More From » c#

 Answers
11

Assuming you want to keep using your CommandField, you could do this programmatically using your GridView's OnRowDataBound event.



Specify the event handler for the RowDataBound event in your GridView declaration:



<asp:GridView ID=gv runat=server OnRowDataBound=gv_RowDataBound....


Then in your event handler (code-behind) find your button (here I'm assuming ImageButton, though this depends on your ButtonType property in your CommandField) and add JavaScript to its OnClientClick property.



protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((ImageButton)e.Row.Cells[cell].Controls[ctrl]).OnClientClick = return confirm('Are you sure you want to delete?');; // add any JS you want here
}
}


In the above example, cell refers to the column index of your CommandField and ctrl refers to the control index (Delete button) within the cell you're referencing.


[#80781] Thursday, January 17, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinarnoldp

Total Points: 10
Total Questions: 122
Total Answers: 109

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;