Solved

Datagrid 2.0

Posted on 2009-05-13
8
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…

691 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