vb.net 将 ASP.Net DetailsView 置于编辑模式和更新模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13648377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Placing ASP.Net DetailsView into Edit mode and Update mode
提问by Emad-ud-deen
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.
我们正在使用带有 VB.Net 代码隐藏文件的 ASP.Net DetailsView。我们试图通过允许用户单击“编辑”按钮然后单击“更新”按钮来允许用户在 DetailsView 中编辑和保存更改。
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?)
当用户单击“编辑”按钮时没有任何反应,因此我们为“编辑”按钮添加了一个 OnClick 处理程序。DetailsView 将进入编辑模式,但前提是用户单击“编辑”按钮两次。(也许是 ASP.Net 错误?)
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.
一旦 DetailsView 处于编辑模式,更新和取消按钮就会按预期显示,但当用户单击其中任何一个按钮时,没有任何反应。我们在更新按钮上放置了一个 OnClick 以试图强制 DetailsView 更新,但 .ChangeMode(DetailsViewMode.) 的唯一选择是编辑、插入、只读。
I also thought DetailsViews did not need additional OnClicks unless we needed to perform special handling.
我还认为 DetailsViews 不需要额外的 OnClicks,除非我们需要执行特殊处理。
Here is the markup for the DetailsView:
这是 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" />
<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.
ButtonUpdate_Click 例程不完整,因为我们不知道如何让 DetailsView 进行更新。
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.
附加说明:这是我们第一次尝试通过不在标记中设置数据源来执行 DetailsView。我们没有这样做,而是使用在 DataSet 设计器中创建的 DataSet TableAdapter 中的数据。
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.
如果我们在标记中将 DetailsView 和 DataSource 都放在一起,那么 Edit 和 Update 按钮就可以正常工作了。我们也这样做是为了尽可能消除额外的编码。
采纳答案by MTAdmin
Well, looking real briefly, try wrapping your Page_Load databind in an If Not Page.IsPostback.
好吧,简单地看一下,尝试将 Page_Load 数据绑定包装在 If Not Page.IsPostback 中。
Postback wreaks havoc on databound controls.
回发对数据绑定控件造成了严重破坏。
回答by rweiler
If you want the DetailsView's automatic behaviour for Edit, you need to use a CommandField to show the Edit button:
如果您想要 DetailsView 的自动编辑行为,您需要使用 CommandField 来显示编辑按钮:
<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.
如上所述,您需要包含 DataKeyNames 属性来指定数据源的主键,以便 ASP.NET 知道要更新数据源中的哪条记录。同样如上所述,您需要确保 Bind("columnName") 语句使用与数据存储中使用的字段名称相同的字段名称。
Also, make sure that you provide an UpdateCommand in your SqlDataProvider (or equivalent for your data store) so that the update can take place.
此外,请确保在 SqlDataProvider(或数据存储的等效项)中提供 UpdateCommand,以便进行更新。
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.
现在,当您将 DetailsView 置于编辑模式时,更新和取消按钮将自动显示在该位置。如果您需要在数据存储中更新数据之前对数据进行一些处理,则在后面的代码中处理 DetailView 的 ItemUpdating 事件。

