• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

Datagrid 2.0

I have a datagrid in C# and I would like my first column to have allow a user to delete the row. However, I would also like to have a javascript alert pop up asking the user if they are sure they would like to delete this message when the delete is clicked. Can this be done  and if so how do I go about doing this.


<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" O
            DataKeyNames="ID" AutoGenerateColumns="False" OnRowDeleted="GridView1_RowDeleted"
            DataSourceID="SqlDataSource1" CellPadding="3" CellSpacing="2" GridLines="None"
            PageSize="50">
            <RowStyle BackColor="#E6E8FC" BorderStyle="None" Font-Names="Verdana" 
                Font-Size="10pt" />
            <Columns>
                
                <%--<asp:CommandField DeleteText="Delete" ShowDeleteButton="True" 
                    ButtonType="Image" DeleteImageUrl="~/images/delete.jpg" />--%>
                
                <asp:BoundField DataField="datecreated" HeaderText="Date " 
                    SortExpression="datecreated" />
                <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
                <asp:BoundField DataField="checked_catalogue" HeaderText="Checked Catalogue" 
                    SortExpression="checked_catalogue" />
                <asp:BoundField DataField="a_lastname" HeaderText="Author Last Name" 
                    SortExpression="a_lastname" />
                <asp:BoundField DataField="a_firstname" HeaderText="Author First Name" 
                    SortExpression="a_firstname" />
                <asp:BoundField DataField="publisher" HeaderText="Publisher" 
                    SortExpression="publisher" />
                <asp:BoundField DataField="publication_date" HeaderText="Publication Date" 
                    SortExpression="publication_date" />
                <asp:BoundField DataField="ISBN" HeaderText="ISBN" SortExpression="ISBN" />
                <asp:BoundField DataField="how_you_hear" HeaderText="How U Hear" 
                    SortExpression="how_you_hear" />
                <asp:BoundField DataField="audience" HeaderText="Audience" 
                    SortExpression="audience" />
                <asp:BoundField DataField="additional_info" HeaderText="Additional_Info" 
                    SortExpression="additional_info" />
                <asp:BoundField DataField="p_name" HeaderText="Patron Name" 
                    SortExpression="p_name" />
                <asp:BoundField DataField="library_card_no" HeaderText="Library Card No" 
                    SortExpression="library_card_no" />
                <asp:BoundField DataField="place_hold" HeaderText="Place Hold" 
                    SortExpression="place_hold" />
                <asp:BoundField DataField="pick_up_location" HeaderText="P.U. Location" 
                    SortExpression="pick_up_location" />
            </Columns>
            <PagerStyle BackColor="#252D6C" Font-Names="Verdana" Font-Size="8pt" 
                ForeColor="White" />
            <HeaderStyle BackColor="#252D6C" Font-Names="Verdana" Font-Size="8pt" 
                ForeColor="White" />
            <AlternatingRowStyle BackColor="White" Font-Names="Verdana" 
                Font-Overline="False" Font-Size="10pt" />
        </asp:GridView>

Open in new window

0
mockweb
Asked:
mockweb
  • 4
  • 4
1 Solution
 
baiju_nagoriCommented:
Hi

On code behind add following code


Protected Sub GridView1_RowDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _ Handles GridView1.RowDataBound 
If e.Row.RowType = DataControlRowType.DataRow Then ' reference the Delete LinkButton 
Dim db As ImageButton = CType(e.Row.Cells(0).Controls(0), ImageButton) ' Get information about the product bound to the row 
 
db.OnClientClick = "return confirm('Are you certain you want to delete the product?');"
 
 End If
 
 End Sub

Open in new window

0
 
mockwebAuthor Commented:
I am using c#
0
 
baiju_nagoriCommented:
Bookmark this link http://www.developerfusion.com/tools/convert/vb-to-csharp/

Here is the converted code.



protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        // reference the Delete ImageButton 
        ImageButton db = (ImageButton)e.Row.Cells(0).Controls(0);
        db.OnClientClick = "return confirm('Are you certain you want to delete the product?');";
    }
}

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mockwebAuthor Commented:
i get the following error message
No overload for 'GridView1_RowDeleted' matches delegate 'System.Web.UI.WebControls.GridViewDeletedEventHandler'
0
 
baiju_nagoriCommented:
I am not sure how you are getting error for GridView1_RowDeleted when my solution uses GridView1_RowDataBound.

Try removing GridView1_RowDeleted and see if what I proposed works or not.


0
 
mockwebAuthor Commented:
no i am still getting an error
No overload for 'GridView1_RowDataBound' matches delegate 'System.Web.UI.WebControls.GridViewDeletedEventHandler'
0
 
baiju_nagoriCommented:
I think there is a problem on your HTML code.

Can you check for this?
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
 <Columns>
  <asp:CommandField ButtonType="Button" HeaderText="Del" ShowDeleteButton="True" ShowHeader="True" />
 </Columns>
</asp:GridView>
 
    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
if (e.Row.RowType == DataControlRowType.DataRow) {
        // reference the Delete ImageButton 
        ImageButton db = (ImageButton)e.Row.Cells(0).Controls(0);
        db.OnClientClick = "return confirm('Are you certain you want to delete the product?');";
    }
 
    }

Open in new window

0
 
mockwebAuthor Commented:
now i get this error:
 System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.DataControlButton' to type 'System.Web.UI.WebControls.ImageButton'.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now