Mohan

Mohan

  • 1.7k
  • 27
  • 2.4k

gridview command field image button delete not firing

Dec 3 2020 6:05 PM
Hi, I'm implementing gridview crud operation. I used command field with button type button, all the functions (edit, update,delete) worked properly. But when I changed command field button type to image, delete function is not firing. When I click on Delete icon, Javascript alert is coming, and when I click ok, it is not firing OnRowDeleting function, Below is my code
  1. <asp:GridView ID="grdCenterVisitDate" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"  
  2. DataKeyNames="CenterVisitID" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" PageSize = "5" AllowPaging ="true" OnPageIndexChanging = "OnPaging"  
  3. OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added."  
  4. >  
  5. <Columns>  
  6. <asp:TemplateField HeaderText="Center Visit date">  
  7. <ItemTemplate>  
  8. <asp:Label ID="lblName" runat="server" Text='<%# Eval("CenterVisitDate") %>'></asp:Label>  
  9. </ItemTemplate>  
  10. <EditItemTemplate>  
  11. <asp:TextBox ID="txtCenterVisitDate" runat="server" ValidationGroup="vgManageVisitDate" Text='<%# Eval("CenterVisitDate") %>'></asp:TextBox>  
  12. <asp:RequiredFieldValidator ID="rfvManageVisitDate" runat="server" ControlToValidate="txtCenterVisitDate" ErrorMessage="Date is required" Display="Dynamic" ValidationGroup="vgManageVisitDate"></asp:RequiredFieldValidator>  
  13. <ACT:CalendarExtender ID="cetxtStartDate" runat="server" TargetControlID="txtCenterVisitDate"></ACT:CalendarExtender>  
  14. </EditItemTemplate>  
  15. </asp:TemplateField>  
  16. <asp:TemplateField HeaderText="CenterVisitType">  
  17. <ItemTemplate>  
  18. <asp:Label ID="lblCenterVisitType" runat="server" Text='<%# Eval("CenterVisitType") %>'></asp:Label>  
  19. </ItemTemplate>  
  20. <EditItemTemplate>  
  21. <asp:DropDownList runat="server" ID="ddlCenterVisit" SelectedValue='<%# Bind("CenterVisitTypeID") %>'>  
  22. <asp:ListItem Text="In-Person Class hours" Value="1"></asp:ListItem>  
  23. <asp:ListItem Text="In-Person Consultation" Value="2"></asp:ListItem>  
  24. <asp:ListItem Text="On-Line Consultation" Value="3"></asp:ListItem>  
  25. </asp:DropDownList>  
  26. </EditItemTemplate>  
  27. </asp:TemplateField>  
  28. <asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true" ValidationGroup="vgManageVisitDate" EditImageUrl="~/Images/site/goals/icon-edit-sm.png" DeleteImageUrl="~/Images/site/goals/icon-delete-sm.png" UpdateImageUrl="~/Images/site/goals/icon-update-sm.png" CancelImageUrl="~/Images/site/goals/icon-cancel-sm.png" ControlStyle-CssClass="btn btn-cv-edit"/>  
  29. </Columns>  
  30. </asp:GridView>  
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (Request.QueryString["CenterID"] != null)  
  4. strCenterID = Request.QueryString["CenterID"];  
  5. if (!IsPostBack)  
  6. {  
  7. ////txtDate1.StartDate = DateTime.Now;  
  8. fillCenterVisitDate();  
  9. }  
  10. else  
  11. dashboardID = 2;  
  12. }  
  13. private void fillCenterVisitDate()  
  14. {  
  15. DataSet ds = new DataSet();  
  16. ds = gdh.GetCenterVisitDateByDashboardID(2);  
  17. grdCenterVisitDate.DataSource = ds;  
  18. grdCenterVisitDate.DataBind();  
  19. }  
  20. #region CenterVisitDateGrid  
  21. protected void OnRowEditing(object sender, GridViewEditEventArgs e)  
  22. {  
  23. grdCenterVisitDate.EditIndex = e.NewEditIndex;  
  24. this.fillCenterVisitDate();  
  25. }  
  26. protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)  
  27. {  
  28. GridViewRow row = grdCenterVisitDate.Rows[e.RowIndex];  
  29. int gridId = Convert.ToInt32(grdCenterVisitDate.DataKeys[e.RowIndex].Values[0]);  
  30. DateTime centerVisitDate = Convert.ToDateTime((row.FindControl("txtCenterVisitDate"as TextBox).Text);  
  31. Int16 CenterVisit = Convert.ToInt16((row.FindControl("ddlCenterVisit"as DropDownList).SelectedValue);  
  32. int i = gdh.updateCenterVisitDate(gridId, CenterVisit, centerVisitDate, user.UserID);  
  33. grdCenterVisitDate.EditIndex = -1;  
  34. this.fillCenterVisitDate();  
  35. }  
  36. protected void OnRowCancelingEdit(object sender, EventArgs e)  
  37. {  
  38. grdCenterVisitDate.EditIndex = -1;  
  39. this.fillCenterVisitDate();  
  40. }  
  41. protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)  
  42. {  
  43. int centerVisitId = Convert.ToInt32(grdCenterVisitDate.DataKeys[e.RowIndex].Values[0]);  
  44. int i = gdh.DelCenterVisit(centerVisitId);  
  45. this.fillCenterVisitDate();  
  46. }  
  47. protected void OnRowDataBound(object sender, GridViewRowEventArgs e)  
  48. {  
  49. if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != grdCenterVisitDate.EditIndex)  
  50. {  
  51. (e.Row.Cells[2].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";  
  52. }  
  53. }  
  54. protected void OnPaging(object sender, GridViewPageEventArgs e)  
  55. {  
  56. grdCenterVisitDate.PageIndex = e.NewPageIndex;  
  57. this.fillCenterVisitDate();  
  58. }  
  59. #endregion  

Answers (1)