سؤال

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.

هل كانت مفيدة؟

المحلول 2

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

Postback wreaks havoc on databound controls.

نصائح أخرى

If you want the DetailsView's automatic behaviour for Edit, you need to use a CommandField to show the Edit button:

<asp:DetailsView id="dvDetailsView" runat="server" DataSourceId="sqlDS" DataKeyNames="primaryKey">
  <Fields>
    <asp:CommandField ButtonType="Button" ShowEditButton="true" />
  </Fields>
</asp:DetailsView>

As noted above you need to include the DataKeyNames property to specify the primary key for the datasource so that ASP.NET knows which record in the data source to update. Also as mentioned above, you need to make sure that the Bind("columnName") statements use the same field names as used in your data store.

Also, make sure that you provide an UpdateCommand in your SqlDataProvider (or equivalent for your data store) so that the update can take place.

Now when you place the DetailsView into Edit mode, the Update and Cancel buttons will automatically be displayed where the is. If you need to do some work with the data before the data is updated in the data store, then handle the DetailView's ItemUpdating event in the code behind.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top