文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP.NET 2.0高级数据处理之冲突检测_Asp.Net开发技巧

ASP.NET 2.0高级数据处理之冲突检测_Asp.Net开发技巧

时间:2010-09-15  来源:缘分星空

 

前面我们提到,数据绑定控件把传递给数据源的值存放在相互独立的Keys、Values(新值)和 OldValues字典中。在默认情况下,SqlDataSource和ObjectDataSource会忽略OldValues字段,只使用Keys 和Values。这种行为是由数据源的ConflictDetection属性检测的,在默认情况下这个属性的值被设置为 OverwriteChanges。OverwriteChanges模式意味着"为了更新或删除记录,仅仅匹配主键值"。这种操作意味着,记录的更新或删除是不考虑该记录的下层值是否改变过了。在通常情况下,理想的状态是,只有当数据行的值与最初选择的值完全匹配的时候,才让Update或Delete 操作成功。在这种理想情况下,如果另外一个用户在你选择某一行和更新该行的之间也更新了这一行,你的更新操作就会失败。通过把 ConflictDetection属性设置为CompareAllValues,数据源也支持这种操作。在这种模式下,数据源会把OldValues应用到命令或方法上,它会使用这些值来确保在更新或删除记录之前,更新或删除操作必须与记录的所有值都匹配。你还必须把 OldValuesParameterFormatString属性设置为一个有效的.NET框架组件格式化字符串(例如"original_{0} "),来指明OldValues和Keys字典中的参数如何重新命名以便与NewValues参数区分开来。

  下面的代码示例显示了SqlDataSource控件在OverwriteChanges和CompareAllValues模式下使用的典型的SQL命令。ID字段被假定为主键字段。请注意,后面一个命令在WHERE子句中比较数据行的所有原始值,而不是仅仅比较主键。在这种情况下,数据源的 OldValuesParameterFormatString需要被设置为"original_{0}"。

SELECT [ID], [Name], [Address] from [Contacts]
-- OverwriteChanges
UPDATE [Contacts] SET [Name] = @Name, [Address] = @Address WHERE [ID] = @ID
DELETE FROM [Contacts] WHERE [ID] = @ID

-- CompareAllValues
UPDATE [Contacts] SET [Name] = @Name, [Address] = @Address WHERE [ID] = @original_ID
AND [Name] = @original_Name AND [Address] = @original_Address
DELETE FROM [Contacts] WHERE [ID] = @original_ID AND [Name] = @original_Name
AND [Address] = @original_Address

  请注意,Insert操作不需要OldValues,ConflictDetection只对Update和Delete操作有意义。

  下面的例子演示了冲突发生时的行为。为了运行这个例子,你必须在两个独立的浏览器窗口中打开例子的两个实例(两次点击"Run Sample")。接着在两个窗体的同一行上都点击"Edit"按钮,使该行进入编辑模式。在第一个窗口中改变一个值并点击"Update",请注意这个更新是成功的。在第二个窗口中,在该行中输入一个新值并点击"Update",这个更新操作没有成功,因为下层数据行的值已经被第一个更新操作改变过了。这个示例检测了Updated或Deleted事件参数的AffectedRows属性,它为0确认了冲突发生了。

<script runat="server">
Protected Sub SqlDataSource1_Updated(sender As Object, e As SqlDataSourceStatusEventArgs)
 If e.AffectedRows = 0 Then
  Response.Write("Row changed, update aborted<br />")
 End If
End Sub

Protected Sub SqlDataSource1_Deleted(sender As Object, e As SqlDataSourceStatusEventArgs)
 If e.AffectedRows = 0 Then
  Response.Write("Row changed, delete aborted<br />")
 End If
End Sub
</script>

  当Update或Delete使用模板化UI的时候,使用了Bind语法的双向(two-way)数据绑定字段的旧值都会被保留。对于 Delete来说,这意味着在ItemTemplate中你必须给数据绑定的值使用Bind语法,其目的是为了保留删除操作所需要的旧值。下面的例子演示了这种技术。

<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

  使用ObjectDataSource的时候,情况也类似。请注意,由于数据源的ConflictDetection属性被设置为CompareAllValues,数据源将查找一个可接受Contact对象的每个字段的原始值的UpdateContact重载。

  你还可以同时使用DataObjectTypeName属性和CompareAllValues。在这种情况下, ObjectDataSource查找仅接受两个参数(都是Contact)的UpdateContact重载。第一个参数是存放新值的Contact对象,第二个参数是存放旧值的Contact对象。


roxy lunchboxangelus leather dyeshotel baroccodeals guadeloupecarhartt cowboy bootsmountain creek vacation rentalscontemporary realismfall collectionvintage handbags pursesshow on broadwaynbc winter classic ratingsholiday inn hotel suites clearwater beachbrand names logosfamily room furniture setpicasso expressionismframe satchel handbagscoel sprouseoversized beach towel reviewostrich skin purseleather belt designsdelsey luggage replacement partsitb asia 2009watercolor sketchmenswear collectionedgar degas portraitsvintage gucci bagcarnival europemajesty of the seas deck planleather pants womenchappelle show lost episodesbest cruise line europesmall soccer duffle bagesstarry night famousdamier zippy walletwhite designer handbagdesigner handbags blogpurple designer pursesjessica simpson boots salework shoesheavy leather sewing machinepea coat hoodcruise caribbean islandslv bagsall inclusive weekend getawaysculpture lessonslouis vuitton tivoli pmreal fake louis vuitton bagsmonogram keepallsew shoulder bagdiscount petunia pickle bottom diaper bagsmonogram speedymonogrammed totes bagsbelt variable speedsteamer trunksdesigner bags wholesalemonogramming giftsplastic bags retail storesnorwegian cruise lines jobvintage chanel pursesmirage rental cardelsey luggage repairswhite embroidered bibdesigner shoes purseseverything bagscamping tent rentalssoft trunkbob mackie luggage setcheap custom sunglassesclutch handbags wholesalecoach designerdesigner jewelry ringhalloween screensaverfendi purse repairwestern caribbean cruises 2010giraffe pattern handbagroyal carribbean cruise linebest cruise travel insurancehumor cake toppersstretch leather beltbirthday card ideasluxor hotel car rentalholiday candlesunique luggage tagethan allen leather sofahowards storage storesleather backpack purse apparelchimuri beach cottagesdisney cruise line logoleather purse patterntom lynch 100 watercolor workshop lesson chartsjakob dylanauthentic gucci walletchristmas craft gifts kidsbuddha pictures on canvasconn store locationsprocess of making leatherphotography jewelryseasons bed breakfast ocean beachcherry walletspure natural liquid soapcheap poster printingfree halloween desktop screensaversdiesel jacketsornaments christmas cardskimono robestaking back sunday messenger bagbeachfront property north carolinadesigner leather wallettannery leather cleaneroversized vintage posters2009 bmwcarnival cruise excursions cozumelhealthy spring recipescopper eyelets6 night cruisesitalian watercolor artistsbrown leather hobo pursecontemporary expressionismbest time cruise balticgucci messenger bagbrown bomber jacketfamily vacation guidetote handbag redpurse picture frameyellow hobo bagsseasons lesson plans preschoolembossed paper napkinsleather productsweather modelshome depot rentalsbuy harley davidsonleather threadsmonogram wedding stickers letter czdnetgucci-GU2007-03nylon bags wholesalecarnival cruise ship job opportunitiesdigital photography artistpatterns fabric pursesroyal caribbean cruise offersreligious clip art christmasboss orange bagsGucci-193602-06girls winter coatvintage posterscustom posterssmall leather man baginitial ring reviewscomet onlinecarrier suit

Replica lv Hangbags

Replica Hangbags

Hangbags

replica handbags

replica handbags

replica handbags

replica handbags

handbags

replica handbags replica handbags replica handbags replica handbags replica handbags
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载