asp.net - 将 ASP.Net DetailsView 置于编辑模式和更新模式

标签 asp.net vb.net detailsview updatemodel editmode

我们正在使用带有 VB.Net 代码隐藏文件的 ASP.Net DetailsView。我们试图通过允许用户单击“编辑”按钮,然后单击“更新”按钮来允许用户在“详细信息 View ”中编辑和保存更改。

当用户单击“编辑”按钮时没有任何反应,因此我们为“编辑”按钮添加了一个 OnClick 处理程序。只有当用户单击“编辑”按钮两次时,DetailsView 才会进入编辑模式。 (也许是 ASP.Net 错误?)

一旦DetailsView 处于编辑模式,更新和取消按钮就会按预期显示,但当用户单击这两个按钮中的任何一个时,什么也不会发生。我们在“更新”按钮上放置了一个“OnClick”,试图强制“DetailsView”更新,但 .ChangeMode(DetailsViewMode. 的唯一选择是“编辑”、“插入”、“只读”。

我还认为DetailsViews不需要额外的OnClicks,除非我们需要执行特殊处理。

这是详细信息 View 的标记:

<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>

这是代码隐藏文件中的编码:

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

ButtonUpdate_Click 例程不完整,因为我们不知道如何让 DetailsView 进行更新。

附加说明: 这是我们第一次尝试通过不在标记中设置数据源来创建详细信息 View 。我们没有这样做,而是使用在数据集设计器中创建的数据集 TableAdapter 中的数据。

如果我们在标记中添加了详细信息 View 和数据源,那么“编辑”和“更新”按钮就可以正常工作。我们这样做也是为了尽可能消除额外的编码。

最佳答案

如果您希望 DetailsView 的自动编辑行为,您需要使用 CommandField 来显示编辑按钮:

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

如上所述,您需要包含 DataKeyNames 属性来指定数据源的主键,以便 ASP.NET 知道要更新数据源中的哪条记录。同样如上所述,您需要确保 Bind("columnName") 语句使用与数据存储中使用的相同的字段名称。

此外,请确保在 SqlDataProvider(或数据存储的等效项)中提供 UpdateCommand,以便可以进行更新。

现在,当您将“详细信息 View ”置于“编辑”模式时,“更新”和“取消”按钮将自动显示在原来的位置。如果您需要在数据存储中更新数据之前对数据进行一些处理,请在后面的代码中处理 DetailView 的 ItemUpdating 事件。

关于asp.net - 将 ASP.Net DetailsView 置于编辑模式和更新模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648377/

相关文章:

asp.net - 我可以在 Visual Studio 2010 中将任务列表项添加到 csHTML 中吗?

asp.net - swfupload 不允许我从一台服务器上传到另一台服务器

vb.net - .NET 应用程序崩溃,没有调试信息

c# - ASP.NET DetailsView 在插入后返回空白

c# - 什么是 DetailsView.EnableModelValidation?

c# - Entity Framework 选择查询

asp.net - IIS 重写输入类型

c# - 如何设置 Winform 文本框字段焦点以便用户可以通过单击 Tab 按钮浏览它们?

vb.net - 将 AsyncCallback 与 BeginInvoke 结合使用时,为什么我会在回调方法中收到 "Cross-thread operation not valid exception"?

ios - 数据未在详细 View 中加载