aspnettutorial20CustomizingDataModUIcsWord下载.docx
- 文档编号:6724084
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:22
- 大小:33.13KB
aspnettutorial20CustomizingDataModUIcsWord下载.docx
《aspnettutorial20CustomizingDataModUIcsWord下载.docx》由会员分享,可在线阅读,更多相关《aspnettutorial20CustomizingDataModUIcsWord下载.docx(22页珍藏版)》请在冰点文库上搜索。
Active和Discontinued。
如图1:
IntheprecedingtutorialwesawhowtocustomizethedatamodificationinterfacesbyaddingvalidationWebcontrols.Inthistutorialwe’lllookathowtocustomizetheactualdatacollectionWebcontrols,replacingtheBoundFieldandCheckBoxField’sstandardTextBoxandCheckBoxcontrolswithalternativeinputWebcontrols.Inparticular,we’llbuildaneditableGridViewthatallowsaproduct’sname,category,supplier,anddiscontinuedstatustobeupdated.Wheneditingaparticularrow,thecategoryandsupplierfieldswillrenderasDropDownLists,containingthesetofavailablecategoriesandsupplierstochoosefrom.Furthermore,we’llreplacetheCheckBoxField’sdefaultCheckBoxwithaRadioButtonListcontrolthatofferstwooptions:
“Active”and“Discontinued”.
图1:
在GridView的编辑界面使用DropDownList和RadioButton控件
Figure1:
TheGridView’sEditingInterfaceIncludesDropDownListsandRadioButtons
一、重载UpdateProduct方法
Step1:
CreatingtheAppropriateUpdateProductOverload
本节教程我们将创建一个可编辑的GridView并允许编辑更新产品的名字、类别、提供商和废弃状态等。
因此,我们要重载UpdateProduct方法,并接受5个输入参数:
4个产品参数值加上一个产品ID。
像以前那样,本重载将:
InthistutorialwewillbuildaneditableGridViewthatpermitseditingofaproduct’sname,category,supplier,anddiscontinuedstatus.Therefore,weneedanUpdateProductoverloadthatacceptsfiveinputparameters–thesefourproductvaluesplustheProductID.Likeinourpreviousoverloads,thisonewill:
1.根据指定的ProductID从数据库中获取产品信息;
2.更新ProductName,categoryID,supplierID和Discontinued字段;
3.通过TableAdapter的Update()方法向数据访问层DAL发出更新请求。
4.RetrievetheproductinformationfromthedatabaseforthespecifiedProductID,
5.UpdatetheProductName,CategoryID,SupplierID,andDiscontinuedfields,and
6.SendtheupdaterequesttotheDALthroughtheTableAdapter’sUpdate()method.
简单起见,这个重载方法省略了一个重要的业务逻辑――检查并确保一个将会标记为discontinued的产品不是它的提供商提供的唯一产品。
你愿意的话也可以加进来,或者做的更完善一些,将这个逻辑写到一个独立的方法中。
Forbrevity,forthisparticularoverloadI’veomittedthebusinessrulecheckthatensuresaproductbeingmarkedasdiscontinuedisn’ttheonlyproductofferedbyitssupplier.Feelfreetoadditinifyouprefer,or,ideally,refactoroutthelogictoaseparatemethod.
下面的代码是我们在ProductsBLL类中新增的UpdateProduct重载方法:
ThefollowingcodeshowsthenewUpdateProductoverloadintheProductsBLLclass:
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update,false)]
publicboolUpdateProduct(stringproductName,int?
categoryID,int?
supplierID,booldiscontinued,intproductID)
{
Northwind.ProductsDataTableproducts=Adapter.GetProductByProductID(productID);
if(products.Count==0)
//如果没有匹配记录,返回false
//nomatchingrecordfound,returnfalse
returnfalse;
Northwind.ProductsRowproduct=products[0];
product.ProductName=productName;
if(supplierID==null)product.SetSupplierIDNull();
elseproduct.SupplierID=supplierID.Value;
if(categoryID==null)product.SetCategoryIDNull();
elseproduct.CategoryID=categoryID.Value;
product.Discontinued=discontinued;
//更新产品记录
//Updatetheproductrecord
introwsAffected=Adapter.Update(product);
//成功更新后返回true,否则返回false
//Returntrueifpreciselyonerowwasupdated,otherwisefalse
returnrowsAffected==1;
}
二、手工处理可编辑的GridView
Step2:
CraftingtheEditableGridView
编写完UpdateProduct重载方法,下面要做的是创建可编辑的GridView:
在设计器窗口中打开EditInsertDelete文件夹中的CustomizedUI.aspx页,为其增加一个GridView控件;
接着通过GridView的智能标记创建一个新的ObjectDataSource,配置这个ObjectDataSource使用ProductBLL类的GetProducts()方法来获取产品信息,并让其使用上面创建的UpdateProduct重载方法来进行产品的更新。
在新增和删除标签上,从下拉列表中选择(None)。
WiththeUpdateProductoverloadadded,we’rereadytocreateoureditableGridView.OpentheCustomizedUI.aspxpageintheEditInsertDeletefolderandaddaGridViewcontroltotheDesigner.Next,createanewObjectDataSourcefromtheGridView’ssmarttag.ConfiguretheObjectDataSourcetoretrieveproductinformationviatheProductBLLclass’sGetProducts()methodandtoupdateproductdatausingtheUpdateProductoverloadwejustcreated.FromtheINSERTandDELETEtabs,select(None)fromthedrop-downlists.
图2:
配置ObjectDataSource使用上面创建的UpdateProduct重载方法
Figure2:
ConfiguretheObjectDataSourcetoUsetheUpdateProductOverloadJustCreated
像《datamodification》教程中那样,VisualStudio创建了ObjectDataSource的元素标记并指定OldValuesParameterFormatString属性为original_{0}。
由于我们编写的方法不支持传入的原始的ProductID值,所以业务逻辑层不会生效。
因此,像上节教程中那样,我们需要从元素标记中移除这些属性,或者设置这些属性。
Aswe’veseenthroughoutthedatamodificationtutorials,thedeclarativesyntaxfortheObjectDataSourcecreatedbyVisualStudioassignstheOldValuesParameterFormatStringpropertytooriginal_{0}.This,ofcourse,won’tworkwithourBusinessLogicLayersinceourmethodsdon’texpecttheoriginalProductIDvaluetobepassedin.Therefore,aswe’vedoneinprevioustutorials,takeamomenttoremovethispropertyassignmentfromthedeclarativesyntaxor,instead,setthisproperty’svalueto{0}.
改动后的ObjectDataSource元素标记将如下所示:
Afterthischange,theObjectDataSource’sdeclarativemarkupshouldlooklikethefollowing:
<
asp:
ObjectDataSourceID="
ObjectDataSource1"
runat="
server"
SelectMethod="
GetProducts"
TypeName="
ProductsBLL"
UpdateMethod="
UpdateProduct"
>
<
UpdateParameters>
ParameterName="
productName"
Type="
String"
/>
categoryID"
Int32"
supplierID"
discontinued"
Boolean"
productID"
/UpdateParameters>
/asp:
ObjectDataSource>
注意上面代码中OldValuesParameterFormatString已经被移除,并且在UpdateParameters集合中为UpdateProduct重载方法的每个入口参数提供了一个Parameter。
NotethattheOldValuesParameterFormatStringpropertyhasbeenremovedandthatthereisaParameterintheUpdateParameterscollectionforeachoftheinputparametersexpectedbyourUpdateProductoverload.
虽然ObjectDataSource被配置为只对产品的部分信息进行更新,而GridView却显示了所有的产品信息。
我们需要按照下面几点来调整GridView:
WhiletheObjectDataSourceisconfiguredtoupdateonlyasubsetofproductvalues,theGridViewcurrentlyshowsalloftheproductfields.TakeamomenttoedittheGridViewsothat:
∙只包括ProductName,SupplierName,CategoryName字段的绑定列和Discontinued字段的CheckBox列。
∙CategoryName和SupplierName字段在Discontinued前面显示(左边)
∙将CategoryName和SupplierName的标题分别改为“Category”和“Supplier”
∙启用编辑模式(在GridView的智能标记中选择启用编辑复选框)
∙ItonlyincludestheProductName,SupplierName,CategoryNameBoundFieldsandtheDiscontinuedCheckBoxField
∙TheCategoryNameandSupplierNamefieldstoappearbefore(totheleftof)theDiscontinuedCheckBoxField
∙TheCategoryNameandSupplierNameBoundFields’HeaderTextpropertyissetto“Category”and“Supplier”,respectively
∙Editingsupportisenabled(checktheEnableEditingcheckboxintheGridView’ssmarttag)
这些调整之后,设计器中的页面将如图3所示:
Afterthesechanges,theDesignerwilllooksimilartoFigure3,withtheGridView’sdeclarativesyntaxshownbelow.
图3:
移除GridView中无用的字段
Figure3:
RemovetheUnneededFieldsfromtheGridView
GridView的元素标记也像下面所示:
GridViewID="
GridView1"
AutoGenerateColumns="
False"
DataKeyNames="
ProductID"
DataSourceID="
EnableViewState="
Columns>
BoundFieldDataField="
ProductName"
HeaderText="
SortExpression="
CategoryName"
Category"
ReadOnly="
True"
SupplierName"
Supplier"
CheckBoxFieldDataField="
Discontinued"
/Columns>
GridView>
这时GridView的只读界面就改好了。
查看数据时,每种产品就作为GridView中的一行,并显示产品的name,category,supplier和discontinued状态。
AtthispointtheGridView’sread-onlybehavioriscomplete.Whenviewingthedata,eachproductisrenderedasarowintheGridView,showingtheproduct’sname,category,supplier,anddiscontinuedstatus.
图4:
GridView调整后的只读界面
Figure4:
TheGridView’sRead-OnlyInterfaceisComplete
三、在编辑界面中使用DropDownList显示Category和Supplier
Step3:
UsingaDropDownListfortheCategoryandSupplierEditingInterfaces
我们注意到ProductsRow对象包含产品的CategoryID,CategoryName,SupplierID和SupplierName属性,但是Products数据库只保存了外键,而对应的Name保存在Categories和Suppliers表中。
ProductsRow对象中的CategoryID和SupplierID可以读取和写入,而CategoryName和SupplierName属性则标记为只读。
RecallthattheProductsRowobjectcontainsCategoryID,CategoryName,SupplierID,andSupplierNameproperties,whichprovidetheactualforeign-keyIDvaluesintheProductsdatabasetableandthecorrespondingNamevaluesintheCategoriesandSupplierstables.TheProductRow’sCategoryIDandSupplierIDcanbothbereadfromandwrittento,whiletheCategoryNameandSupplierNamepropertiesaremarke
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- aspnettutorial20CustomizingDataModUIcs