Javascript for Gridview Commandfield
-
One column on my gridview is an asp commandfield. When a user clicks on a row in this column how can I execute a client side java script function instead of the code behind function?
Convert your commandfield column as templatefield then replace the item template control by regular html link tag.
-
One column on my gridview is an asp commandfield. When a user clicks on a row in this column how can I execute a client side java script function instead of the code behind function?
GridView Data Manipulation function ConfirmOnDelete(item) { if (confirm("Are you sure to delete: " + item + "?")==true) return true; else return false; } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowState != DataControlRowState.Edit) // check for RowState { if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType { string id = e.Row.Cells[0].Text; // Get the id to be deleted LinkButton lb = (LinkButton)e.Row.Cells[6].Controls[2]; //cast the ShowDeleteButton link to linkbutton if (lb != null) { lb.Attributes.Add("onclick", "return ConfirmOnDelete('" + id + "');"); //attach the JavaScript function with the ID as the paramter } } } }