ASP.Net 2.0 DetailView Insert User Confirmation Message Box?

Posted on 2006-05-25
Last Modified: 2012-05-05

  I'm coding a Web Form using VS 2005 and ASP.Net 2.0. On the form, I have a GridView which shows existing records from a table and a DetailView always set to "Insert" mode to allow users to add new records.

So, the default mode for the DetailView control is set to Insert, and I'm just using the DetailView's  "New, Insert, Cancel" Control field.   That works fine in that the records get inserted correctly.  However, what I really want to do is display a message box that says.

"Okay to insert this record?" with the standard "Yes/No" or "OK/Cancel" message box buttons.

The "No" or "Cancel" button needs to stop the insert from taking place.  The "OK" or "Yes" needs to allow the insert to continue.

I had a similar situation with the GridView and the "Delete" button.  In that case, I was able to turn the Delete Button into a Template Field, and add an Client Onclick event that's coded:

  return confirm ('Okay to delete this record?')

That works fine because it's one regular link button and the commandName is 'Delete', which tells how to handle the event.

However, I'm not sure how to turn this sort of double "Insert, Cancel" dataview command into a template field, and even if I could, I'm not sure if it needs to be two buttons or what kind of CommandName(s) to use.

So, it's basically really simple . . .

I want the user to confirm that they really want to insert the record they just said they wanted to insert before I go ahead and actually add it to the database.

How do I do that using a DetailView control?

I'm already coding some stuff into the "ItemInserting" subroutine to make sure there are no blank values and a couple of other things, and I'm using a neat little "Alert" javascript routine I had help with here in Expert's Exchange:

That works really well.  However, I can't figure out how to turn that into a "Yes/No" type of confirmation box, or how to get the results of the user's choice (Yes or No) back into my server side subroutine to set "e.Cancel = true" and exit or to continue on, or even if I need to do that.

Any suggestions?

Please let me know.

Question by:klgrube
    LVL 27

    Expert Comment

    you have 2 option here

    1- Download this control and try to use it or even rebuild it adding any new features you may need.

    2- Build a popup control from a Div and use it as your confirm box with all server side code



    Author Comment


       Thank you, as always!   But I don't speak C#, so I can't translate what I see there.  Also, there's some additional code later in the comments  where the javascript is partly converted to VB Script that helps with browser compatability  But, that doesn't look complete.  I can't quite interpret it.  And, there also seem to be some issues with ASP.Net 2.0 compatability.  So, this isn't going to work well for me.

       I'd love to try your second suggestion, however, if you could please point me to a small example where a simple standard little popup window is used as a substitute for a message / confirmation box.  I haven't been able to find one that works well with ASP.Net 2.0.

       In the meantime, although it may not have been clear, one major part of the question I'm asking is how to code an 'onclick' event into the 'insert' button of the DetailsView control.  I can't figure out how to access that part of the control (the DetailsView command field called "New, Insert, Cancel." )  I can't seem to be able to turn it into a template field, so I can't figure out how to add an on-click event.  That might be the best alternative, insead of writing my own popup control - or borrowing someone else's!  

       Any suggestions?  If that's simply not possible, please let me know.

    LVL 27

    Expert Comment

    I have used the same code in C# with 2.0 and it worked fine for me.
    My idea of using custom control was about warpping a div control in a custom control "dll file" using Css classes to display and hide the control with a little bit of javascript to control the postback. unfortunately I have not seen any such control thats why I thought of creating it for my next project.
    take a look here to see how to insert a record using the detailsview control

    Good luck


    Author Comment


       Thanks again!  

       I'm okay with knowing how to insert a row using a DetailsView.  What I still can't figure out, though, is how to get to an 'onclick' event in the combined  Insert/cancel control field used for inserts in the DetailsView, or how to turn it into a template field so I can get to an onclick event.

       Any suggestions?

    LVL 27

    Accepted Solution

    If you want to customize this row then instead of using the CommandField use a TemplateField where you define any or all of the ItemTemplate, EditItemTemplate or  InsertItemTemplate as required.

    In each template you can add Buttons with the desired CommandNames for
    each template.  For example, within the ItemTemplate, you would add:

    <asp:Button Text="Edit" runat="server" ID="Edit"
    <asp:Button Text="Insert" runat="server" ID="Insert"

    In the EditItemTemplate, you would add:

    <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
    <asp:Button ID="btnCancel" CausesValidation="false" runat="server"
    CommandName="Cancel" Text="Cancel"></asp:Button>

    and in the InsertItemTemplate you would add:

    <asp:Button ID="btnInsert" runat="server" CommandName="Insert"
    <asp:Button ID="btnCancel" runat="server" CausesValidation="false"
    CommandName="Cancel" Text="Cancel"></asp:Button>

    This would mimic the action performed by the CommandField (which renders all
    those buttons for you based on which property you set to true.
    Once you are in the edit template in the desiner you can double click on any button to get the click event
    Note: I have not tried this myself but I dont see any reason why it shouldnt work

    Good luck

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 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

    18 Experts available now in Live!

    Get 1:1 Help Now