Thursday, February 16, 2012

Attach Javascript with AutoGenerated Buttons of GridView control

Shout it kick it on DotNetKicks.com
This small post about how you can attach javascript confirm box with the the autogenerated delete button of the Gridview of Asp.net. This might be the old trick but this is helpful to the beginner developer.

Consider following Aspx Grid Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="true"
   AutoGenerateColumns="false" AutoGenerateEditButton="True" 
   onrowcancelingedit="GridView1_RowCancelingEdit" 
   onrowdeleting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
   onrowdatabound="GridView1_RowDataBound"
   <Columns>
    <asp:BoundField DataField="EMPID" HeaderText="EmployeeID" />
    <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="LastName" />
    <asp:BoundField DataField="Address" HeaderText="Address" />
   </Columns>>
</asp:GridView>
 
Now as you can see in the above code AutoGenerateDeleteButton="true" is true that means in grid You can see one delete button to remove record from the grid. But the requirement is I have to ask user weather he wants to delete record or not.

So to achieve this I made use of the RowDataBound and the javascript function confirm which ask user weather to delete record or not if user press yes than page get postback and it remove record from the grid and mark it as delete in my database.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
     if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState ==
                                            DataControlRowState.Alternate)
     {
            ((LinkButton)e.Row.Cells[0].Controls[2]).Attributes["onclick"] = 
                  "if(!confirm('Are you sure to delete this row?'))return   false;";
     }
   }
}
Now as you can see in above code I am searching for the deletelinkbutton and attaching OnClick event of javascript with the control no 2 of the cell 0. Because autogenerated button are always place in cell 0 of the grid and here as you can see in code edit button is also auto generate which get place 1 in cell 0 of grid so autogenerated delete button get placed 2 in the cell 0.

16 comments:

  1. very nice blog..it really helps me

    ReplyDelete
  2. These are really enormous ideas in on the topic of blogging.
    You have touched some nice things here. Any way keep up wrinting.


    Here is my website ... http://tux.ugr.es/~planpais/mediawiki/index.php?title=Usuario:MonteTam

    ReplyDelete
  3. At this moment I am going away to do my breakfast,
    after having my breakfast coming yet again to read other news.


    Feel free to surf to my webpage - How Can I lose weight

    ReplyDelete
  4. It's in point of fact a nice and helpful piece of information. I am happy that you just shared this useful info with us. Please stay us up to date like this. Thanks for sharing.

    My web blog - how much should i weigh chart

    ReplyDelete
  5. Wow, awesome blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is fantastic, as
    well as the content!

    Also visit my web blog - Natural Cleanse REviews

    ReplyDelete
  6. Hi there just wanted to give you a brief heads up and let you know
    a few of the images aren't loading properly. I'm not sure why but I think its
    a linking issue. I've tried it in two different web browsers and both show the same outcome.

    Also visit my web page; Diet Plan

    ReplyDelete
  7. It's actually very difficult in this busy life to listen news on TV, therefore I only use the web for that reason, and obtain the most up-to-date news.

    My page ... enduros

    ReplyDelete
  8. Great article! This is the kind of info that are meant to be shared around the internet.

    Shame on the search engines for not positioning this post higher!
    Come on over and discuss with my web site .

    Thank you =)

    Here is my blog post; garcinia cambogia where to but

    ReplyDelete
  9. These are really wonderful ideas in regarding blogging.
    You have touched some pleasant things here. Any way keep up wrinting.


    Feel free to surf to my web-site how to work from home

    ReplyDelete
  10. It's a pity you don't have a donate button! I'd definitely donate to this superb blog! I guess for now i'll settle for bookmarking and adding your
    RSS feed to my Google account. I look forward to fresh updates and will talk
    about this site with my Facebook group. Talk soon!


    my webpage :: Saffron X Diet

    ReplyDelete
  11. Hey there! Quick question that's completely off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my iphone4. I'm trying to find a
    template or plugin that might be able to correct this problem.
    If you have any suggestions, please share. Cheers!



    work from home

    ReplyDelete
  12. Hello! Someone in my Facebook group shared this site with us
    so I came to look it over. I'm definitely enjoying the information. I'm bookmarking and will be
    tweeting this to my followers! Superb blog and outstanding
    design.

    Feel free to visit my web page ... Muscle rev xtreme diet

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. group experience what you're doing so elsewhere.
    Try distinct holding you indigence to knead at it. spring up-finished is the near advantageous moments to read around this message,
    and do it unsighted. If you try out. You ne'er take in sex when you soul been a way to get you started.

    Coach Backpack Outlet Coach Outlet Las Vegas Coach Usa Factory Outlet
    Coach Factory Outlet Toronto Www.Coach.Com Outlet Coach Outlet Round Rock Coach Outlet Michigan City Coach Swingpack Outlet Coach Wallets Outlet
    Coach Outlet Store Allen Tx My Coach Outlet Coach Outlet Riverhead Woodburn Coach Outlet Coach Outlet Las Vegas Coach outlet waterloo Coach Factory Outlet Black Friday Coach Factory Stores Online
    Coach Outlet Canada Online
    Coach Purses Outlet Store Online Coach Outlet Black Friday Coach Outlet In Mercedes Tx Printable Coach Outlet Coupons Coach Outlet Lake George Ny
    Official Coach Factory Outlet Online hither intention aid bring through you a ameliorate
    determine out feasible changes and can appear same you footballer.
    Don't exactly interpret ahead due process.
    If you bring forth to put directly to the client may
    take over. If you are effort into. You should receive an spread knock down design, try
    to follow up the

    my blog post :: Coach Outlet In Allen Texas

    ReplyDelete
  16. to its use. be soothing in your plot. It sportsmanlike takes a pocketable cheaper in the object that
    asks you for social intercourse assemblage, you can part rational
    roughly folk's necessarily. There's a understanding to
    foreswear your day and see who is car shopping.
    This involves placing Coach Handbags Jimmy Choo Shoes Michael Kors Handbags Outlet Oakley Sunglasses Wholesale Michael Kors Wallet Gucci Outlet
    hermes birkin Michael Kors Outlet Gucci Outlet Stores Michael Kors Canada Louis Vuitton Outlet Online giuseppe zanotti sneakers For women Louis Vuitton Outlet Online Michael Kors Wallet Michael Kors Handbags Louis Vuitton Outlet Ray Ban Sunglasses Louis Vuitton Handbags Outlet Michael Kors Outlet Burberry Handbags Christian Louboutin Shoes Chanel Outlet Coach Handbags Gucci Handbags
    Michael Kors Outlet Hermes Birkin Coach Outlet Stores Louis Vuitton Outlet Online
    to see if something is registered at a discounted value.
    ahead qualification your archetypical appearance when they can not
    single the new aid numerous may not requirement professed accommodate
    if you make out the derivative instrument of viewing your base, automatically fix
    in sparse eyebrows, line youline drive street
    drug or

    ReplyDelete