Solved

How to call DetailsView's New explicitly??

Posted on 2008-10-07
2
370 Views
Last Modified: 2012-05-05
Hi,

DetailsView has these edit,delete,new builtin function.
How can I call this new detailsview explicityly??
As you know detailsview let you handle update, delete and insert.
I want to use that same detailsview to handle edit and insert a new record.
How can I call this new detailsview??
Thanks.
0
Comment
Question by:dkim18
[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
  • 2
2 Comments
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 22675381
If the command buttons are placed inside a Template Field, you can change the DetailsView's mode according to their CommandName.  Set the attribute to Edit, or New as appropriate in the ItemTemplate.  The commands to save the record are Update and Insert, or Cancel to ... not save, obviously.  

This is the usual method.


<asp:TemplateField>
    <EditItemTemplate>
        <asp:Button ID="cmdUpdate" runat="server" CommandName="Update" Text="Save" />
        <asp:Button ID="cmdCancel" runat="server" CommandName="Cancel" Text="Undo" />
    </EditItemTemplate>
    <InsertItemTemplate>
        <asp:Button ID="cmdInsert" runat="server" CommandName="Insert" Text="Save" />
        <asp:Button ID="cmdCancel" runat="server" CommandName="Cancel" Text="Undo" />
    </InsertItemTemplate>
    <ItemTemplate>
        <asp:Button ID="cmdEdit" runat="server" CommandName="Edit" Text="Edit" />
        <asp:Button ID="cmdNew" runat="server" CommandName="New" Text="Add New" />
    </ItemTemplate>
</asp:TemplateField>

Open in new window

0
 
LVL 16

Accepted Solution

by:
GreymanMSC earned 500 total points
ID: 22675423
However, if the command button is located outside the DetailsView, then you need to use server side code in the buttons' OnClick event.  The code can call the ChangeMode method to switch from ReadOnly to Edit or Insert, or back to cancel.  To save the record use the UpdateItem or InsertItem methods, as appropriate
    Protected Sub cmdEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Me.dvItems.ChangeMode(DetailsViewMode.Edit)
        Me.cmdEdit.Visible = False
        Me.cmdNew.Visible = False
        Me.cmdSave.Visible = True
        Me.cmdCancel.Visible = True
    End Sub
 
    Protected Sub cmdNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdNew.Click
        Me.dvItems.ChangeMode(DetailsViewMode.Insert)
        Me.cmdEdit.Visible = False
        Me.cmdNew.Visible = False
        Me.cmdSave.Visible = True
        Me.cmdCancel.Visible = True
    End Sub
 
    Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.dvItems.ChangeMode(DetailsViewMode.ReadOnly)
        Me.cmdEdit.Visible = True
        Me.cmdNew.Visible = True
        Me.cmdSave.Visible = False
        Me.cmdCancel.Visible = False
    End Sub
 
    Protected Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Select Case Me.dvItems.CurrentMode 
            Case DetailsViewMode.Edit
                Me.dvItems.UpdateItem(True)
                If Me.Page.IsValid Then
                    Me.dvItems.ChangeMode(DetailsViewMode.ReadOnly)
                End If
            Case DetailsViewMode.Insert
                Me.dvItems.InsertItem(True)
                If Me.Page.IsValid Then
                    Me.dvItems.ChangeMode(DetailsViewMode.ReadOnly)
                End If
        End Select 
    End Sub

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

635 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