六十四:GridView批量添加數(shù)據(jù)_第1頁(yè)
六十四:GridView批量添加數(shù)據(jù)_第2頁(yè)
六十四:GridView批量添加數(shù)據(jù)_第3頁(yè)
六十四:GridView批量添加數(shù)據(jù)_第4頁(yè)
六十四:GridView批量添加數(shù)據(jù)_第5頁(yè)
已閱讀5頁(yè),還剩17頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、在ASP.NET 2.0中操作數(shù)據(jù)之六十四:GridView批量添加數(shù)據(jù)作者:heker2007 字體:增加 減小 類(lèi)型:轉(zhuǎn)載 時(shí)間:2016-05-18 我要評(píng)論前面介紹了批量更新,批量刪除數(shù)據(jù),這篇文章主要介紹如何實(shí)現(xiàn)批量添加數(shù)據(jù),當(dāng)然為了保證數(shù)據(jù)的完整性,我們?cè)谧鲞@些批量操作的時(shí)候,都使用了事務(wù)來(lái)實(shí)現(xiàn)。導(dǎo)言:在前面的第62章GridView批量更新數(shù)據(jù)里,我們用GridView控件里定制了一個(gè)批編輯界面,同樣的我們也可以定制一個(gè)批添加界面.假設(shè)有這種情況,我們接受一批從Tokyo(東京)發(fā)過(guò)來(lái)的貨物:6種不同的tea 和 coffee,如果用戶在一個(gè)DetailsVi

2、ew控件里一次輸入一個(gè)產(chǎn)品,他將會(huì)重復(fù)的輸入很多相同的值,比如相同的種類(lèi)(Beverages),相同的供應(yīng)商(Tokyo Traders),相同的discontinued值(False),以及相同的order值(0).重復(fù)性的輸入這些相同的值不僅累,還很容易出錯(cuò).只需額外多做一些工作,我們就可以創(chuàng)建一個(gè)批添加界面。用戶只需一次行的選擇supplier 和category,輸入一系列產(chǎn)品的names 和unit prices,再點(diǎn)擊一個(gè)按鈕就可以將這些新產(chǎn)品添加進(jìn)數(shù)據(jù)庫(kù)(如圖1所示).這些添加的產(chǎn)品的ProductName 和UnitPrice數(shù)據(jù)由界面上方的2個(gè)DropDownList控件指定

3、,Discontinued 和UnitsOnOrder的值由“硬編輯”指定,分別為false和0.圖1:批添加界面本教程,我們將創(chuàng)建一個(gè)如圖1所示的批添加界面。在前面2章的基礎(chǔ)上我們將把添加過(guò)程用事務(wù)封裝以保證原子操作.讓我們開(kāi)始吧!第一步:創(chuàng)建一個(gè)展示界面我們將創(chuàng)建一個(gè)包含2個(gè)區(qū)域的單一頁(yè)面:展示區(qū)域和添加區(qū)域.我們?cè)谶@一步創(chuàng)建的是展示區(qū)域,它包含一個(gè)用于展示產(chǎn)品的GridView控件以及一個(gè)標(biāo)題為“Process Product Shipment”的button按鈕.當(dāng)點(diǎn)擊該按鈕時(shí),展示界面將替換為一個(gè)如圖1所示的添加界面.如果點(diǎn)“Add Products from Shipment” 或

4、 “Cancel”按鈕時(shí)又會(huì)返回展示頁(yè)面.添加界面將在第二步完成.這個(gè)包含2個(gè)界面的頁(yè)面每次只能讓一個(gè)界面可見(jiàn)。我們將用2個(gè)Panel Web控件作為容器包含這2個(gè)界面一個(gè)Panel Web控件包含一個(gè)界面.首先打開(kāi)BatchData文件夾里的BatchInsert.aspx頁(yè)面,在設(shè)計(jì)器模式里從工具箱里拖一個(gè)Panel控件到頁(yè)面(如圖2所示),設(shè)置其ID為DisplayInterface.當(dāng)將Panel控件拖到頁(yè)面時(shí)其Height 和 Width屬性分別為50px 和 125px.在屬性窗口里清除這些屬性.圖2:從工具箱里拖一個(gè)Panel控件到頁(yè)面然后拖一個(gè)Button 和 GridView

5、控件到Panel控件里。設(shè)Button的ID為ProcessShipment ,Text屬性為“Process Product Shipment”.  設(shè)GridView的ID為ProductsGrid,從其智能標(biāo)簽里將其綁定到一個(gè)名為ProductsDataSource的ObjectDataSource.設(shè)置該ObjectDataSource調(diào)用ProductsBLL class類(lèi)的GetProducts方法.由于該GridView控件只用來(lái)展示數(shù)據(jù),從UPDATE, INSERT, DELETE標(biāo)簽里選“(None)”. 點(diǎn)Finish完成設(shè)置圖3:調(diào)用ProductsBLL C

6、lass類(lèi)的GetProducts方法來(lái)顯示數(shù)據(jù)圖4:在UPDATE, INSERT, DELETE標(biāo)簽里選“(None)”完成ObjectDataSource設(shè)置后,Visual Studio會(huì)自動(dòng)地添加一些BoundFields以及一個(gè)CheckBoxField。只保留ProductName, CategoryName, SupplierName, UnitPrice, 以及Discontinued這幾列.你可以再做一些外觀的改進(jìn).我將UnitPrice列定制為貨幣值,重新對(duì)列進(jìn)行了排序,再對(duì)一些列的HeaderText值進(jìn)行了修改.再在GridView的智能標(biāo)簽里啟用了“分頁(yè)”和“排序”

7、功能.完成上述工作后,頁(yè)面的聲明代碼看起來(lái)應(yīng)該和下面的差不多:?12345678910111213141516171819202122232425262728293031<asp:Panel ID="DisplayInterface" runat="server"> <p> <asp:Button ID="ProcessShipment" runat="server"  Text="Process Product Shipment&qu

8、ot; /> </p> <asp:GridView ID="ProductsGrid" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductID" DataSourceID="ProductsDataSource"> <

9、Columns>  <asp:BoundField DataField="ProductName" HeaderText="Product"  SortExpression="ProductName" />  <asp:BoundField DataField="CategoryName" HeaderText="Category"  ReadOnly="True" Sor

10、tExpression="CategoryName" />  <asp:BoundField DataField="SupplierName" HeaderText="Supplier"  ReadOnly="True" SortExpression="SupplierName" />  <asp:BoundField DataField="UnitPrice" DataFormatStrin

11、g="0:c"  HeaderText="Price" HtmlEncode="False"  SortExpression="UnitPrice">  <ItemStyle HorizontalAlign="Right" />  </asp:BoundField>  <asp:CheckBoxField DataField="Discontinued

12、" HeaderText="Discontinued"  SortExpression="Discontinued">  <ItemStyle HorizontalAlign="Center" />  </asp:CheckBoxField> </Columns> </asp:GridView> <asp:ObjectDataSource ID="ProductsDa

13、taSource" runat="server" OldValuesParameterFormatString="original_0" SelectMethod="GetProducts" TypeName="ProductsBLL"> </asp:ObjectDataSource></asp:Panel>我們注意到Button 和 GridView控件的聲明代碼出現(xiàn)在<asp:Panel>標(biāo)簽內(nèi)部,因?yàn)檫@些控件置于名為Displa

14、yInterface的Panel控件里面,我們可以將Panel控件的Visible 屬性設(shè)置為false來(lái)隱藏這些控件.我們將在第三步看到,當(dāng)點(diǎn)擊一個(gè)按鈕時(shí),通過(guò)編程的方式改變Panel控件的Visible屬性以顯示添加界面.花點(diǎn)時(shí)間在瀏覽器里登錄該頁(yè)面.如圖5所示,你將看到一個(gè)顯示為“Process Product Shipment”的button按鈕,其下的GridView控件每次列出10個(gè)產(chǎn)品.圖5:GridView列出了產(chǎn)品并啟用排序和分頁(yè)功能第二步:創(chuàng)建添加界面創(chuàng)建完展示界面后,我們將創(chuàng)建添加界面。在本教程,我們的添加界面允許用戶同時(shí)添加5個(gè)產(chǎn)品,且這5個(gè)產(chǎn)品的category 和

15、supplier是一樣的,而names 和 nit price值不同.在ID為DisplayInterface的Panel控件下面,從工具箱里拖一個(gè)Panel控件到頁(yè)面,設(shè)置其ID為InsertingInterface,Visible屬性為false,并清除其Height 和 Width屬性值。我們將在第三步添加代碼將其Visible屬性改為true.接下來(lái)我們將創(chuàng)建如圖1所示的添加界面。該界面本來(lái)可以通過(guò)一些HTML技術(shù)來(lái)創(chuàng)建的,不過(guò)在這里我們將使用一個(gè)很簡(jiǎn)單的4列7行的table表.注意:雖然在設(shè)計(jì)器模式里可以使用工具箱的工具來(lái)添加<table> elements元素,不過(guò)那樣

16、會(huì)自動(dòng)添加一些我們不需要的style屬性設(shè)置.因此,我更偏向于在源視圖模式里添加HTML <table> elements元素. 當(dāng)寫(xiě)好類(lèi)<table>聲明代碼后,我喜歡立即切換到設(shè)計(jì)器模式,再添加Web控件并設(shè)置其屬性。當(dāng)心中有數(shù),已經(jīng)想好了要?jiǎng)?chuàng)建幾行幾列的時(shí)候,我傾向于使用靜態(tài)HTML(static HTML)而不是Table Web控件,原因是如果使用Table Web控件的話,我們必須通過(guò)FindControl("controlID")的方式來(lái)訪問(wèn)放置在里面的的Web控件.不過(guò)話又說(shuō)回來(lái),如果是要?jiǎng)?chuàng)建一個(gè)動(dòng)態(tài)變化的表(dynamically-

17、sized tables)的話比如該表的行和列都綁定到數(shù)據(jù)庫(kù)或者基于用戶自定義的標(biāo)準(zhǔn)格式,我還是要使用Table Web控件,原因很簡(jiǎn)單,我們可以通過(guò)編程來(lái)創(chuàng)建該Table Web控件.在ID為InsertingInterface的Panel控件的<asp:Panel>標(biāo)簽里輸入如下的聲明代碼:?123456789101112131415161718192021222324252627282930313233343536373839404142<table class="DataWebControlStyle" cellspacing="0&qu

18、ot;> <tr class="BatchInsertHeaderRow"> <td class="BatchInsertLabel">Supplier:</td> <td></td> <td class="BatchInsertLabel">Category:</td> <td></td> </tr> <tr class="

19、BatchInsertRow"> <td class="BatchInsertLabel">Product:</td> <td></td> <td class="BatchInsertLabel">Price:</td> <td></td> </tr> <tr class="BatchInsertAlternatingRow"> &

20、lt;td class="BatchInsertLabel">Product:</td> <td></td> <td class="BatchInsertLabel">Price:</td> <td></td> </tr> <tr class="BatchInsertRow"> <td class="BatchInsertLabel"&g

21、t;Product:</td> <td></td> <td class="BatchInsertLabel">Price:</td> <td></td> </tr> <tr class="BatchInsertAlternatingRow"> <td class="BatchInsertLabel">Product:</td> <

22、td></td> <td class="BatchInsertLabel">Price:</td> <td></td> </tr> <tr class="BatchInsertRow"> <td class="BatchInsertLabel">Product:</td> <td></td> <td class="

23、BatchInsertLabel">Price:</td> <td></td> </tr> <tr class="BatchInsertFooterRow"> <td colspan="4"> </td> </tr></table>該<table>聲明代碼里暫時(shí)還未包含任何的Web控件。我們注意到每一個(gè)<tr> element元素都有明確的CSS c

24、lass設(shè)置:放置名為supplier 和category的DropDownLists控件的“頭頂行”對(duì)應(yīng)的是BatchInsertHeaderRow;放置“Add Products from Shipment” 和“Cancel”按鈕的“底部行”對(duì)應(yīng)的是BatchInsertFooterRow;那些包含product和unit price的TextBox控件的行交替的運(yùn)用BatchInsertRow和BatchInsertAlternatingRow.我已經(jīng)在Styles.css文件里創(chuàng)建里相應(yīng)的CSS classes,代碼如下:?123456789101112131415161718192

25、02122232425262728/* Styles for /BatchData/BatchInsert.aspx tutorial */.BatchInsertLabel font-weight: bold; text-align: right; .BatchInsertHeaderRow td color: White; background-color: #900; padding: 11px; .BatchInsertFooterRow td text-align: center; paddin

26、g-top: 5px; .BatchInsertRow .BatchInsertAlternatingRow background-color: #fcc;輸入這些代碼后,切換到設(shè)計(jì)視圖,該<table>看起來(lái)是一個(gè)4列7行的表,如圖6所示:圖6:添加界面為一個(gè)4列7行的表現(xiàn)在我們?cè)谔砑咏缑胬锾砑覹eb控件.從工具箱拖2個(gè)DropDownList到表的相應(yīng)方格里一個(gè)用來(lái)顯示supplier另一個(gè)用來(lái)顯示category.將用來(lái)顯示supplier的那個(gè)DropDownList的ID設(shè)為Suppliers,并將其綁定到一個(gè)名為SuppliersDataS

27、ource的ObjectDataSource.設(shè)置該ObjectDataSource調(diào)用SuppliersBLL class類(lèi)的GetSuppliers方法.并在UPDATE標(biāo)簽里選“(None)”,點(diǎn)擊Finish完成設(shè)置向?qū)?圖7:設(shè)置ObjectDataSource調(diào)用SuppliersBLL Class類(lèi)的GetSuppliers方法設(shè)置該DropDownList顯示CompanyName列,而傳遞的值為SupplierID列.圖8:顯示CompanyName列,傳遞的是SupplierID列的值將第2個(gè)DropDownList的ID設(shè)為Categories,并將其綁定到一個(gè)名為Cat

28、egoriesDataSource的ObjectDataSource.該ObjectDataSource調(diào)用CategoriesBLL class類(lèi)的GetCategories方法. 在UPDATE標(biāo)簽里選“(None)”,再點(diǎn)Finish完成設(shè)置. 最后設(shè)置該DropDownList控件顯示CategoryName列,傳遞CategoryID列的值.當(dāng)添加這2個(gè)DropDownList控件并綁定到相應(yīng)的ObjectDataSources后,你的屏幕看起來(lái)應(yīng)該和圖9差不多:圖9:“頭部行”包含顯示Suppliers和Categories的DropDownList控件我們現(xiàn)在需要?jiǎng)?chuàng)建收集每個(gè)產(chǎn)品

29、的name和price信息的TextBox控件。在下面的每行對(duì)應(yīng)的name和price方格里各拖放一個(gè)TextBox控件. 分別設(shè)置它們的ID為ProductName1, UnitPrice1,ProductName2, UnitPrice2,依次類(lèi)推.對(duì)每個(gè)price TextBoxes添加一個(gè)CompareValidator控件,設(shè)置其ControlToValidate屬性為相應(yīng)控件的ID值.同時(shí)將Operator屬性設(shè)置為GreaterThanEqual,ValueToCompare 屬性設(shè)置為“0”, Type屬性設(shè)置為Currency.這樣一來(lái)可以保證輸入的價(jià)格為有效的大于或等于0的

30、貨幣值.同時(shí)將Text屬性設(shè)置為“*”;ErrorMessage屬性為“The price must be greater than or equal to zero. Also, please omit any currency symbols.”。注意:我們并沒(méi)有在添加界面里包含任何的RequiredFieldValidator控件,即便是數(shù)據(jù)庫(kù)表Products的ProductName不允許為NULL值.舉個(gè)例子,如果用戶只想在前3行輸入產(chǎn)品的name和unit price,而最后2行為空,我們就僅僅向數(shù)據(jù)庫(kù)添加了3個(gè)產(chǎn)品。由于ProductName是必需的,當(dāng)輸入了name值后,我們只

31、需要通過(guò)編程檢查用戶是否輸入了該產(chǎn)品的unit price值.我們將在第四步進(jìn)行該檢查.當(dāng)用戶輸入了數(shù)據(jù),但如果輸入值包含貨幣符號(hào)的話,CompareValidator控件將報(bào)告無(wú)效數(shù)據(jù).在每個(gè)unit price TextBoxe控件前添加一個(gè)“$”符合,提示用戶輸入數(shù)據(jù)的時(shí)候忽略貨幣符號(hào).最后,在InsertingInterface Panel控件里添加一個(gè)ValidationSummary控件,設(shè)置其ShowMessageBox屬性為true,ShowSummary屬性為false.有了這些設(shè)置后,當(dāng)用戶輸入一個(gè)無(wú)效的unit price值后,在TextBox控件旁邊將會(huì)出現(xiàn)一個(gè)星號(hào),且

32、ValidationSummary控件將顯示一個(gè)客戶端的消息框,顯示相應(yīng)的錯(cuò)誤消息.此時(shí),你的屏幕看起來(lái)和圖10差不多.圖10:添加界面現(xiàn)在包含顯示產(chǎn)品的Names和Prices的TextBox控件接下來(lái)我們要在底部行添加“Add Products from Shipment” 和 “Cancel”按鈕.從工具箱拖2個(gè)Button控件到界面的底部,分別設(shè)置其ID為AddProducts和CancelButton;同時(shí)分別設(shè)其Text屬性為“Add Products from Shipment”和“Cancel”.此外,將 CancelButton按鈕的CausesValidation屬性設(shè)置為

33、false.最后,我們需要添加一個(gè)Label Web控件來(lái)顯示有關(guān)這2個(gè)界面的狀態(tài)信息.比如:當(dāng)用戶成功地添加了一批產(chǎn)品時(shí)我們希望切換到展示界面并顯示確認(rèn)消息.當(dāng)然,如果用戶輸入產(chǎn)品信息時(shí)只提供了price而沒(méi)有提供name信息,我們就需要顯示一個(gè)警告信息,提示用戶ProductName是必需的.由于我們需要顯示與這2個(gè)界面有關(guān)的信息,將該控件放在這2個(gè)Panel控件的上方.從工具箱里拖一個(gè)Label Web控件到頁(yè)面頂部,設(shè)其ID為StatusLabel,清除其Text屬性,設(shè)其Visible屬性和EnableViewState屬性為false. 我們?cè)谝郧暗慕坛汤锾接戇^(guò),將EnableVi

34、ewState屬性設(shè)為false的話,我們可以通過(guò)編程的方式改變Label控件的屬性值,而當(dāng)發(fā)生頁(yè)面回傳時(shí)其又回到默認(rèn)狀態(tài).當(dāng)發(fā)生某些用戶行為(user action)時(shí),會(huì)顯示狀態(tài)信息,而當(dāng)頁(yè)面回傳時(shí),狀態(tài)信息又消失了.最后設(shè)置StatusLabel的CssClass屬性為“Warning”,這是我們?cè)赟tyles.css文件里定義的CSS class名稱(chēng).下圖顯示的是添加并設(shè)置Label控件后的樣子圖11:在Panel控件上面放置id為StatusLabel的Label控件第三步:在展示界面和添加界面之間切換到此,我們已經(jīng)完成了展示和添加界面,不過(guò)我們?nèi)匀挥?個(gè)任務(wù)要做:1.在展示界面和添

35、加界面之間切換2.將產(chǎn)品添加到數(shù)據(jù)庫(kù)當(dāng)前,展示界面是可見(jiàn)的而添加界面是隱藏的.這是因?yàn)镈isplayInterface Panel控件的Visible屬性為true(默認(rèn)值), 而InsertingInterface Panel控件的Visible屬性為false.當(dāng)點(diǎn)擊“Process Product Shipment”按鈕時(shí),我們向從展示界面切換到添加界面。為此,創(chuàng)建該按鈕的Click事件處理器,包含以下代碼:?12345protected void ProcessShipment_Click(object sender, EventArgs e) DisplayInterfac

36、e.Visible = false; InsertingInterface.Visible = true;該代碼僅僅隱藏DisplayInterface Panel而顯示InsertingInterface Panel.接下來(lái),我們?yōu)樘砑咏缑胬锏摹癆dd Products from Shipment”和“Cancel” 按鈕創(chuàng)建事件處理器.當(dāng)任何一個(gè)按鈕點(diǎn)擊時(shí),我們需要切換到展示界面.創(chuàng)建這2個(gè)按鈕的Click事件處理器以調(diào)用ReturnToDisplayInterface方法我們馬上就要?jiǎng)?chuàng)建該方法.該方法除了隱藏InsertingInterface Panel并顯示DisplayI

37、nterface Panel外,還需要將Web控件返回到其預(yù)編輯狀態(tài)(pre-editing state).即把DropDownList控件的屬性SelectedIndex設(shè)置為0,清除TextBox控件的Text屬性.注意:仔細(xì)思考一下,如果在返回展示界面以前,我們沒(méi)有將這些控件返回到預(yù)編輯狀態(tài)的話將會(huì)發(fā)生什么事情呢?比如用戶點(diǎn)擊“Process Products from Shipment”按鈕,然后輸入產(chǎn)品信息,再點(diǎn)“Add Products from Shipment”按鈕,這樣將添加產(chǎn)品并返回展示頁(yè)面。如果用戶又想添加另一批產(chǎn)品,一旦點(diǎn)擊“Process Product Shipme

38、nt”按鈕時(shí)將切換到添加界面,但是DropDownList控件的值和TextBox控件的值依然是以前的值.?12345678910111213141516171819202122232425262728293031323334protected void AddProducts_Click(object sender, EventArgs e) / TODO: Save the products  / Revert to the display interface ReturnToDisplayInterface(); protected vo

39、id CancelButton_Click(object sender, EventArgs e) / Revert to the display interface ReturnToDisplayInterface(); const int firstControlID = 1;const int lastControlID = 5; private void ReturnToDisplayInterface() / Reset the control values in the inserting interface Suppli

40、ers.SelectedIndex = 0; Categories.SelectedIndex = 0;  for (int i = firstControlID; i <= lastControlID; i+)  (TextBox)InsertingInterface.FindControl("ProductName" + i.ToString().Text =  string.Empty; (TextBox)InsertingInterface.FindControl("Un

41、itPrice" + i.ToString().Text =  string.Empty;   DisplayInterface.Visible = true; InsertingInterface.Visible = false;上述2個(gè)Click事件處理器都僅僅簡(jiǎn)單的調(diào)用ReturnToDisplayInterface方法,不過(guò)我們將在第四步完善“Add Products from Shipment”的Click事件,添加代碼以保存產(chǎn)品.ReturnToDisplayInterface方法一開(kāi)始就把Suppliers和C

42、ategories的DropDownList控件返回其第一項(xiàng);常量firstControlID和lastControlID分別用來(lái)設(shè)置添加界面里的標(biāo)明產(chǎn)品名稱(chēng)和單價(jià)的TextBoxe控件的開(kāi)始和結(jié)束索引值. 在一個(gè)for循環(huán)里設(shè)置這些控件的Text屬性為空字符串.最后重新設(shè)置Panels控件的Visible屬性,以顯示展示界面而隱藏添加界面.花點(diǎn)時(shí)間在瀏覽器里測(cè)試該頁(yè)面,當(dāng)首次登錄時(shí)你將看到如圖5所示的畫(huà)面,點(diǎn)“Process Product Shipment”按鈕,頁(yè)面將回傳并切換到如圖12所示的添加界面,不管點(diǎn)“Add Products from Shipment”還是“Cancel”按鈕

43、都將返回展示界面.注意:當(dāng)瀏覽添加界面時(shí),測(cè)試一下與unit price TextBox對(duì)應(yīng)的驗(yàn)證控件。如果你輸入的不是貨幣值或價(jià)格比0小,當(dāng)點(diǎn)擊“Add Products from Shipment”按鈕時(shí),就會(huì)彈出一個(gè)客戶端的警告消息.圖12:點(diǎn)擊Process Product Shipment” 按鈕后,將切換到添加界面.第四步:添加產(chǎn)品剩下要做的事情是在“Add Products from Shipment”按鈕的Click事件處理器里將產(chǎn)品添加到數(shù)據(jù)庫(kù).為此,我們可以創(chuàng)建一個(gè)ProductsDataTable,并為要插入的產(chǎn)品添加一個(gè)ProductsRow instance實(shí)例。一旦

44、添加完P(guān)roductsRows后,我們就調(diào)用并把ProductsDataTable傳遞給ProductsBLL class類(lèi)的UpdateWithTransaction方法.記得我們是在第61章在事務(wù)里對(duì)數(shù)據(jù)庫(kù)修改進(jìn)行封裝里創(chuàng)建的UpdateWithTransaction方法,該方法將ProductsDataTable傳遞給ProductsTableAdapter的UpdateWithTransaction方法.于是啟動(dòng)一個(gè)ADO.NET事務(wù),TableAdatper針對(duì)添加的每一個(gè)ProductsRow向數(shù)據(jù)庫(kù)發(fā)出一個(gè)INSERT命令.如果所有的添加無(wú)誤則提交事務(wù),否則對(duì)事務(wù)回滾.對(duì)“Add

45、 Products from Shipment”按鈕的Click處理器進(jìn)行編碼時(shí),應(yīng)該執(zhí)行一些誤差校驗(yàn),因?yàn)槲覀儧](méi)有在添加界面里使用RequiredFieldValidators控件,某個(gè)用戶可能輸入了產(chǎn)品的price而忽視里其name。由于產(chǎn)品的name是必須的,如果出現(xiàn)這種情況的話,我們需要提醒用戶并中斷inserts操作.完整的代碼如下:?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676

46、86970717273747576protected void AddProducts_Click(object sender, EventArgs e) / Make sure that the UnitPrice CompareValidators report valid data. if (!Page.IsValid) return;  / Add new ProductsRows to a ProductsDataTable. Northwind.ProductsDataTable products = new Northw

47、ind.ProductsDataTable(); for (int i = firstControlID; i <= lastControlID; i+)  / Read in the values for the product name and unit price string productName = (TextBox)InsertingInterface.FindControl  ("ProductName" + i.ToString().Text.Trim(); string unit

48、Price = (TextBox)InsertingInterface.FindControl  ("UnitPrice" + i.ToString().Text.Trim();  / Ensure that if unitPrice has a value, so does productName if (unitPrice.Length > 0 && productName.Length = 0)   / Display a warning and exit this

49、event handler  StatusLabel.Text = "If you provide a unit price you must also " +  "include the name of the product."  StatusLabel.Visible = true;  return;   / Only add the product if a product name value is provided if (

50、productName.Length > 0)   / Add a new ProductsRow to the ProductsDataTable  Northwind.ProductsRow newProduct = products.NewProductsRow();   / Assign the values from the web page  newProduct.ProductName = productName;  newProduct.Supplier

51、ID = Convert.ToInt32(Suppliers.SelectedValue);  newProduct.CategoryID = Convert.ToInt32(Categories.SelectedValue);  if (unitPrice.Length > 0)  newProduct.UnitPrice = Convert.ToDecimal(unitPrice);   / Add any "default" values  newPro

52、duct.Discontinued = false;  newProduct.UnitsOnOrder = 0;   products.AddProductsRow(newProduct);    / If we reach here, see if there were any products added if (products.Count > 0)  / Add the new products to the database using a transacti

53、on ProductsBLL productsAPI = new ProductsBLL(); productsAPI.UpdateWithTransaction(products);  / Rebind the data to the grid so that the producst just added are displayed ProductsGrid.DataBind();  / Display a confirmation (don't use the Warning CSS class, though

54、) StatusLabel.CssClass = string.Empty; StatusLabel.Text = string.Format(  "0 products from supplier 1 have been added and filed under " +  "category 2.", products.Count, Suppliers.SelectedItem.Text,  Categories.SelectedItem.Text); Stat

55、usLabel.Visible = true;  / Revert to the display interface ReturnToDisplayInterface();  else  / No products supplied! StatusLabel.Text = "No products were added. Please enter the product " +  "names and unit prices in the textboxes.&qu

56、ot; StatusLabel.Visible = true; 該事件處理器開(kāi)始時(shí)檢查Page.IsValid屬性返回的值是否為true。如果返回的為false,那就意味著至少有一個(gè)CompareValidators控件發(fā)現(xiàn)了無(wú)效的數(shù)據(jù).此時(shí),我們要么停止添加產(chǎn)品;要么將用戶鍵入的unit price值向ProductsRow的UnitPrice屬性賦值時(shí),以拋出一個(gè)異常告終.接下來(lái)創(chuàng)建一個(gè)新的ProductsDataTable instance實(shí)例(也就是products),在一個(gè)for循環(huán)里遍歷有關(guān)name和unit price的TextBox控件,并將其Text屬性讀取

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論