Answered by:
Customize Delete button alert in gridview

Question
-
User-1767698477 posted
Rather than just put this alert into the .aspx page in the control which does work, I don't know how to position this on the page which I want done, and also I can't customize.
So in my rowdatabound I have the following and I'm getting a null reference:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Why is it choking on the last line?delete.OnClientClick = "return confirm('Are you sure you want to delete this liability? " & " from" & e.Row.Cells(4).Text & "?"
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound Dim showothercol As Boolean = False If e.Row.RowType = DataControlRowType.DataRow Then Dim delete As ImageButton = CType(e.Row.FindControl("deletebtn"), ImageButton) delete.OnClientClick = "return confirm('Are you sure you want to delete this liability? " & " from" & e.Row.Cells(4).Text & "?"
<div style="overflow-x: auto; height: 150px; width: 700px"> <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CellPadding="3" HorizontalAlign="Center" OnRowDataBound="Gridview1_rowdatabound" DataKeyNames="LiabID" RowStyle-Wrap="False" HeaderStyle-Wrap="False"> <PagerStyle Wrap="True" /> <RowStyle HorizontalAlign="Center" /> <Columns> <asp:TemplateField ItemStyle-Width="20px" HeaderText=""> <ItemTemplate> <asp:ImageButton ID="editbtn" ImageUrl="~/templates/images/small-pencil.jpg" runat="server" Width="25" Height="25" OnClick="editbtn_Click" CommandArgument='<%# Eval("LiabID") %>'/> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Bind("LiabID") %>' /> </ItemTemplate> <HeaderTemplate> <asp:ImageButton ID="insertbtn" ImageUrl="~/templates/images/add-icon.png" runat="server" Width="20" Height="20" OnClick="insertbtn_Click" /> </HeaderTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-Width="30px" HeaderText=""> <ItemTemplate> <asp:ImageButton ID="deletebtn" ImageUrl="~/templates/images/delete-icon.png" runat="server" Width="20" Height="20" /> </ItemTemplate> </asp:TemplateField>
Monday, May 10, 2021 1:56 AM
Answers
-
User-939850651 posted
Hi sking,
According to your description, I created a simple demo used the code you provided, and modified part of the code to make it work.
Something like this:
<body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" OnRowDeleting="GridView1_RowDeleting"> <Columns> <asp:TemplateField HeaderText="Column 1"> <ItemTemplate> <asp:Label ID="col1" Text='<%# Eval("Column1") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Column 2"> <ItemTemplate> <asp:Label ID="col2" Text='<%# Eval("Column2") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Column 3"> <ItemTemplate> <asp:Label ID="Description" Text='<%# Eval("Description") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:ImageButton ID="deletebtn" ImageUrl="~/img/delete.png" runat="server" Width="20" Height="20" CommandName="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:Label Text="" ID="result" runat="server" /> </div> </form> </body>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim dt As New DataTable dt.Columns.Add("Column1") dt.Columns.Add("Column2") dt.Columns.Add("Description") dt.Rows.Add("content11", "content21", "Item1") dt.Rows.Add("content12", "content22", "Item2") dt.Rows.Add("content13", "content23", "Item3") GridView1.DataSource = dt GridView1.DataBind() End If End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound 'Dim showothercol As Boolean = False If e.Row.RowType = DataControlRowType.DataRow Then Dim delete As ImageButton = CType(e.Row.FindControl("deletebtn"), ImageButton) Dim lab As Label = e.Row.FindControl("Description") delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & lab.Text & "?')" 'Dim text As String = "Are you sure you want to delete this liability " & " from" & e.Row.Cells(2).Text & "?" End If End Sub Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) result.Text = "this row has been deleted!" End Sub
Result:
delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & lab.Text & "?')"
It seems that there are some problems with the script code, and the confirm function is not completely closed. And your main question is Null Exception, imagebutton is null? Have you tried using the VS debugger to find the cause of the empty exception?
If possible, please provide more details.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 10, 2021 6:41 AM
All replies
-
User-939850651 posted
Hi sking,
According to your description, I created a simple demo used the code you provided, and modified part of the code to make it work.
Something like this:
<body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" OnRowDeleting="GridView1_RowDeleting"> <Columns> <asp:TemplateField HeaderText="Column 1"> <ItemTemplate> <asp:Label ID="col1" Text='<%# Eval("Column1") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Column 2"> <ItemTemplate> <asp:Label ID="col2" Text='<%# Eval("Column2") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Column 3"> <ItemTemplate> <asp:Label ID="Description" Text='<%# Eval("Description") %>' runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:ImageButton ID="deletebtn" ImageUrl="~/img/delete.png" runat="server" Width="20" Height="20" CommandName="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <asp:Label Text="" ID="result" runat="server" /> </div> </form> </body>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim dt As New DataTable dt.Columns.Add("Column1") dt.Columns.Add("Column2") dt.Columns.Add("Description") dt.Rows.Add("content11", "content21", "Item1") dt.Rows.Add("content12", "content22", "Item2") dt.Rows.Add("content13", "content23", "Item3") GridView1.DataSource = dt GridView1.DataBind() End If End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound 'Dim showothercol As Boolean = False If e.Row.RowType = DataControlRowType.DataRow Then Dim delete As ImageButton = CType(e.Row.FindControl("deletebtn"), ImageButton) Dim lab As Label = e.Row.FindControl("Description") delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & lab.Text & "?')" 'Dim text As String = "Are you sure you want to delete this liability " & " from" & e.Row.Cells(2).Text & "?" End If End Sub Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) result.Text = "this row has been deleted!" End Sub
Result:
delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & lab.Text & "?')"
It seems that there are some problems with the script code, and the confirm function is not completely closed. And your main question is Null Exception, imagebutton is null? Have you tried using the VS debugger to find the cause of the empty exception?
If possible, please provide more details.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 10, 2021 6:41 AM -
User-1767698477 posted
Hi,
I used your code on a fresh page and yes it worked. You are placing a label control into the template field. I don't understand why my line doesn't work substituting e.Row.Cells(3).Text & "?')" for your lab variable holding the text of the label. I have hovered my mouse over delete and it says Nothing so it's not seeing the delete button.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
'Dim showothercol As Boolean = FalseIf e.Row.RowType = DataControlRowType.DataRow Then
Dim delete As ImageButton = CType(e.Row.FindControl("deletebtn"), ImageButton)
Dim lab As Label = e.Row.FindControl("Description")
delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & lab.Text & "?')"
'delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & e.Row.Cells(3).Text & "?')"
End If
End SubI substituted a templatefield for the aspboundfield in my gridview.
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label ID="lblCompany" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>End If
Dim delete As ImageButton = CType(e.Row.FindControl("deletebtn"), ImageButton)
Dim lab As Label = e.Row.FindControl("lblCompany")
'Dim Company As String = e.Row.Cells(4).Text
delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & lab.Text & "?')"System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=App_Web_fn3jtzc0
StackTrace:
at users_app_Mortgage_application_7.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\mortgageloanapply\mortgageloanapply\users\app\Mortgage_application_7.aspx.vb:line 163Tuesday, May 11, 2021 1:49 AM -
User-939850651 posted
Hi sking,
No matter how I try, I can’t reproduce your problem. Even if I don’t use a template to display the column, I can get the corresponding data correctly, and I can get the control through FindControl(). Here is my test:
<body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="GridView1" ShowHeader="true" AutoGenerateColumns="false"
OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting"> <PagerStyle Wrap="True" /> <Columns> <asp:BoundField DataField="Column1" HeaderText="Column1" /> <asp:BoundField DataField="Column2" HeaderText="Column2" /> <asp:BoundField DataField="Description" HeaderText="Description" /> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:ImageButton ID="deletebtn" ImageUrl="~/img/delete.png"
runat="server" Width="20" Height="20" CommandName="Delete" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> <br /> <asp:Label Text="" ID="result" runat="server" /> </form> </body>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim dt As New DataTable dt.Columns.Add("Column1") dt.Columns.Add("Column2") dt.Columns.Add("Description") dt.Rows.Add("content11", "content21", "Item1") dt.Rows.Add("content12", "content22", "Item2") dt.Rows.Add("content13", "content23", "Item3") dt.Rows.Add("content14", "content24", "Item4") dt.Rows.Add("content15", "content25", "Item5") GridView1.DataSource = dt GridView1.DataBind() End If End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound 'Dim showothercol As Boolean = False If e.Row.RowType = DataControlRowType.DataRow Then Dim delete As ImageButton = CType(e.Row.FindControl("deletebtn"), ImageButton) 'Dim lab As Label = e.Row.FindControl("Description") delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & e.Row.Cells(2).Text & "?')" 'Dim text As String = "Are you sure you want to delete this liability " & " from" & e.Row.Cells(2).Text & "?" End If End Sub Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) result.Text = "this row has been deleted!" End Sub
Can you find ImageButton control in e.Row, as shown in the figure above? Is there any capitalization error in the control name?
Best regards,
Xudong Peng
Tuesday, May 11, 2021 9:56 AM -
User-1767698477 posted
I got it figured out. The reason it was not seeing it because I had placed the code outside of the if statement !
If e.Row.RowType = DataControlRowType.DataRow Then
Dim delete As ImageButton = CType(e.Row.FindControl("deletebtn"), ImageButton)
delete.OnClientClick = "return confirm('Are you sure you want to delete this liability from " & e.Row.Cells(2).Text & "?')"
End IfI had it down here...Isee it does work also by referencing the column and it doesn't have to use a label in a template field.
Tuesday, May 11, 2021 11:13 PM