Solved

Datagrid 2.0

Posted on 2009-05-13
8
418 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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
 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

832 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