<asp:GridView ……> <Columns> <asp:CommandField ShowDeleteButton= "True" ShowEditButton="True" /> <asp:TemplateField HeaderText= "ContactID" InsertVisible="False" SortExpression="ContactID"> <ItemTemplate> <asp:Label ID= "Label1" runat="server" Text=''<%# Bind("ContactID") %>''></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID= "Label3" runat="server" Text=''<%# Eval("ContactID") %>''></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText= "ContactName" SortExpression="ContactName"> <ItemTemplate> <asp:Label ID= "Label2" runat="server" Text=''<%# Bind("ContactName") %>''></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID= "TextBox1" runat="server" Text=''<%# Bind("ContactName") %>''></asp:TextBox> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> |
你可以温和地处理冲突检测错误,可以通过提示用户下层数据被改变了,向用户显示改变过的值, 让用户选择提交或放弃自己的操作。下面的例子演示处理冲突检测的一种可行方法。请注意,DetailsView的RowUpdated事件参数传递了可用 于检测用户输入的值的字典。你还可以设置这个事件参数的KeepInEditMode属性,使用户在决定如何处理冲突期间,DetailsView处于编 辑模式。这个例子所试验方法与上面一个例子类似,同时打开两个窗口来创建冲突更新。
Protected Sub DetailsView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) If e.AffectedRows = 0 Then '' 使DetailsView处于编辑模式并与数据库同步 e.KeepInEditMode = True DetailsView1.DataBind() '' 用用户输入的值重新填充DetailsView Dim t As TextBox t = DetailsView1.Rows(1).Cells(1).Controls(0) t.Text = e.NewValues( "OrderDate") t = DetailsView1.Rows(2).Cells(1).Controls(0) t.Text = e.NewValues( "ShipCountry") ErrorPanel.Visible = True Else ErrorPanel.Visible = False End If End Sub Protected Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs) If e.CancelingEdit = True AndAlso ErrorPanel.Visible = True Then ErrorPanel.Visible = False End If End Sub |