Solved

Datagrid 2.0

Posted on 2009-05-13
8
417 Views
Last Modified: 2012-05-06
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
Comment
Question by:mockweb
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:baiju_nagori
ID: 24378442
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
 

Author Comment

by:mockweb
ID: 24378498
I am using c#
0
 
LVL 4

Expert Comment

by:baiju_nagori
ID: 24378876
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
 

Author Comment

by:mockweb
ID: 24384837
i get the following error message
No overload for 'GridView1_RowDeleted' matches delegate 'System.Web.UI.WebControls.GridViewDeletedEventHandler'
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:baiju_nagori
ID: 24388886
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
 

Author Comment

by:mockweb
ID: 24388990
no i am still getting an error
No overload for 'GridView1_RowDataBound' matches delegate 'System.Web.UI.WebControls.GridViewDeletedEventHandler'
0
 
LVL 4

Accepted Solution

by:
baiju_nagori earned 500 total points
ID: 24389995
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
 

Author Comment

by:mockweb
ID: 24424055
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now