Search code examples
asp.netvb.netdetailsviewupdatemodeleditmode

Placing ASP.Net DetailsView into Edit mode and Update mode


We are working with an ASP.Net DetailsView with a VB.Net code-behind file. We are trying to allow the user to edit and save changes in the DetailsView by allowing the user to click the Edit button and then click the Update button.

Nothing happens when the user clicks the Edit button so we added an OnClick handler for the Edit button. The DetailsView will go into edit mode but only if the user clicks the Edit button twice. (Maybe an ASP.Net bug?)

Once the DetailsView is in Edit mode the Update and Cancel buttons are displayed as expected but nothing happens when the user clicks either of those buttons. We placed an OnClick on the Update button in an attempt to force the DetailsView to Update but the only choices for .ChangeMode(DetailsViewMode. are Edit, Insert, ReadOnly.

I also thought DetailsViews did not need additional OnClicks unless we needed to perform special handling.

Here is the markup for the DetailsView:

<asp:DetailsView 
    ID="DetailsView" 
    runat="server" 
    Height="50px" 
    Width="218px" AutoGenerateRows="False">

    <Fields>

        <asp:TemplateField ShowHeader="False">

            <EditItemTemplate>
                <asp:Button ID="ButtonUpdate" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Update" OnClick="ButtonUpdate_Click"  />
                &nbsp;<asp:Button ID="ButtonCancelUpdate" runat="server" CausesValidation="False" 
                    CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>

            <ItemTemplate>
                <asp:Button ID="ButtonEdit" runat="server" CausesValidation="False" 
                    CommandName="Edit" Text="Edit" OnClick="ButtonEdit_Click"/>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="Forename" HeaderText="First Name:" />
    </Fields>
</asp:DetailsView>

Here is the coding in the code-behind file:

Public Class StudentDetailsMaintenance
    Inherits System.Web.UI.Page

    Dim theTableAdapter As New DataSetSingleStudentTableAdapters.StudentsMaintenanceTableAdapter

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Load the data from the database into the DetailsView.
        '------------------------------------------------------
        DetailsView.DataSource = theTableAdapter.GetDataByStudentID(StudentMaintenance.IntStudentID)
        DetailsView.DataBind()
    End Sub

    Protected Sub ButtonEdit_Click(sender As Object, e As EventArgs)

        ' Place the DetailsView into Edit mode.
        '--------------------------------------
        DetailsView.ChangeMode(DetailsViewMode.Edit)
    End Sub

    Protected Sub ButtonUpdate_Click(sender As Object, e As EventArgs)

        ' Place the DetailsView into Update mode.
        '----------------------------------------
        DetailsView.ChangeMode(DetailsViewMode.)
    End Sub
End Class

The ButtonUpdate_Click routine is incomplete because we don't know how to get the DetailsView to do the update.

Addional Note: This is the first time we are trying to do a DetailsView by not setting up a DataSource in the markup. Instead of doing that we are using the data from a DataSet TableAdapter created in the DataSet designer.

If we did the DetailsView along with the DataSource all in the markup then the Edit and Update buttons work without any problem. We were also doing this in an attempt to eliminate extra coding if possible.


Solution

  • Well, looking real briefly, try wrapping your Page_Load databind in an If Not Page.IsPostback.

    Postback wreaks havoc on databound controls.