Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx

上传人:b****6 文档编号:12142246 上传时间:2023-06-04 格式:DOCX 页数:14 大小:625.75KB
下载 相关 举报
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第1页
第1页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第2页
第2页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第3页
第3页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第4页
第4页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第5页
第5页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第6页
第6页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第7页
第7页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第8页
第8页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第9页
第9页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第10页
第10页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第11页
第11页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第12页
第12页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第13页
第13页 / 共14页
Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx

《Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx》由会员分享,可在线阅读,更多相关《Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx(14页珍藏版)》请在冰点文库上搜索。

Tutorial 10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView.docx

Tutorial10MasterDetailUsingSelectableMasterGridViewWithDetailsDataView

Tutorial10:

Master/DetailUsingaSelectableMasterGridViewwithaDetailsDetailView

 

ScottMitchell

June2006

DownloadtheASPNET_Data_Tutorial_10_VB.exesamplecode.

ContentsofTutorial10(VisualBasic)

Introduction

Step1:

CreatingaSelectableGridView

Step2:

DisplayingtheSelectedProduct'sDetailsinaDetailsView

Summary

Introduction

Intheprevioustutorialwesawhowtocreateamaster/detailreportusingtwowebpages:

a"master"webpage,fromwhichwedisplayedthelistofsuppliers;anda"details"webpagethatlistedthoseproductsprovidedbytheselectedsupplier.Thistwopagereportformatcanbecondensedintoonepage.ThistutorialwillhaveaGridViewwhoserowsincludethenameandpriceofeachproductalongwithaSelectbutton.ClickingtheSelectbuttonforaparticularproductwillcauseitsfulldetailstobedisplayedinaDetailsViewcontrolonthesamepage.

Figure1.ClickingtheSelectButtonDisplaystheProduct'sDetails

Step1:

CreatingaSelectableGridView

Recallthatinthetwo-pagemaster/detailreportthateachmasterrecordincludedahyperlinkthat,whenclicked,senttheusertothedetailspagepassingtheclickedrow'sSupplierIDvalueinthequerystring.SuchahyperlinkwasaddedtoeachGridViewrowusingaHyperLinkField.Forthesinglepagemaster/detailsreport,wewillneedaButtonforeachGridViewrowthat,whenclicked,showsthedetails.TheGridViewcontrolcanbeconfiguredtoincludeaSelectbuttonforeachrowthatcausesapostbackandmarksthatrowastheGridView'sSelectedRow.

StartbyaddingaGridViewcontroltotheDetailsBySelecting.aspxpageintheFilteringfolder,settingitsIDpropertytoProductsGrid.Next,addanewObjectDataSourcenamedAllProductsDataSourcethatinvokestheProductsBLLclass'sGetProducts()method.

Figure2.CreateanObjectDataSourceNamedAllProductsDataSource

Figure3.UsetheProductsBLLClass

Figure4.ConfiguretheObjectDataSourcetoInvoketheGetProducts()Method

EdittheGridView'sfieldsremovingallbuttheProductNameandUnitPriceBoundFields.Also,feelfreetocustomizetheseBoundFieldsasneeded,suchasformattingtheUnitPriceBoundFieldasacurrencyandchangingtheHeaderTextpropertiesoftheBoundFields.Thesestepscanbeaccomplishedgraphically,byclickingtheEditColumnslinkfromtheGridView'ssmarttag,orbymanuallyconfiguringthedeclarativesyntax.

Figure5.RemoveAllButtheProductNameandUnitPriceBoundFields

ThefinalmarkupfortheGridViewis:

GridViewID="ProductsGrid"runat="server"AutoGenerateColumns="False"DataKeyNames="ProductID"

DataSourceID="AllProductsDataSource"EnableViewState="False">

BoundFieldDataField="ProductName"HeaderText="Product"SortExpression="ProductName"/>

BoundFieldDataField="UnitPrice"DataFormatString="{0:

c}"HeaderText="UnitPrice"

HtmlEncode="False"SortExpression="UnitPrice"/>

GridView>

Next,weneedtomarktheGridViewasselectable,whichwilladdaSelectbuttontoeachrow.Toaccomplishthis,simplychecktheEnableSelectioncheckboxintheGridView'ssmarttag.

Figure6.MaketheGridView'sRowsSelectable

CheckingtheEnablingSelectionoptionaddsaCommandFieldtotheProductsGridGridViewwithitsShowSelectButtonpropertysettoTrue.ThisresultsinaSelectbuttonforeachrowoftheGridView,asFigure6illustrates.Bydefault,theSelectbuttonsarerenderedasLinkButtons,butyoucanuseButtonsorImageButtonsinsteadthroughtheCommandField'sButtonTypeproperty.

GridViewID="ProductsGrid"runat="server"AutoGenerateColumns="False"DataKeyNames="ProductID"

DataSourceID="AllProductsDataSource"EnableViewState="False">

CommandFieldShowSelectButton="True"/>

BoundFieldDataField="ProductName"HeaderText="Product"SortExpression="ProductName"/>

BoundFieldDataField="UnitPrice"DataFormatString="{0:

c}"HeaderText="UnitPrice"

HtmlEncode="False"SortExpression="UnitPrice"/>

GridView>

WhenaGridViewrow'sSelectbuttonisclickedapostbackensuesandtheGridView'sSelectedRowpropertyisupdated.InadditiontotheSelectedRowproperty,theGridViewprovidestheSelectedIndex,SelectedValue,andSelectedDataKeyproperties.TheSelectedIndexpropertyreturnstheindexoftheselectedrow,whereastheSelectedValueandSelectedDataKeypropertiesreturnvaluesbasedupontheGridView'sDataKeyNamesproperty.

TheDataKeyNamespropertyisusedtoassociateoneormoredatafieldvalueswitheachrowandiscommonlyusedtoattributeuniquelyidentifyinginformationfromtheunderlyingdatawitheachGridViewrow.TheSelectedValuepropertyreturnsthevalueofthefirstDataKeyNamesdatafieldfortheselectedrowwhereastheSelectedDataKeypropertyreturnstheselectedrow'sDataKeyobject,whichcontainsallofthevaluesforthespecifieddatakeyfieldsforthatrow.

TheDataKeyNamespropertyisautomaticallysettotheuniquely-identifyingdatafield(s)whenyoubindadatasourcetoaGridView,DetailsView,orFormViewthroughtheDesigner.Whilethispropertyhasbeensetforusautomaticallyintheprecedingtutorials,theexampleswouldhaveworkedwithouttheDataKeyNamespropertyspecified.However,fortheselectableGridViewinthistutorial,aswellasforfuturetutorialsinwhichwe'llbeexamininginserting,updating,anddeleting,theDataKeyNamespropertymustbesetproperly.TakeamomenttoensurethatyourGridView'sDataKeyNamespropertyissettoProductID.

Let'sviewourprogressthusfarthroughabrowser.NotethattheGridViewliststhenameandpriceforalloftheproductsalongwithaSelectLinkButton.ClickingtheSelectbuttoncausesapostback.InStep2we'llseehowtohaveaDetailsViewrespondtothispostbackbydisplayingthedetailsfortheselectedproduct.

Figure7.EachProductRowContainsaSelectLinkButton

HighlightingtheSelectedRow

TheProductsGridGridViewhasaSelectedRowStylepropertythatcanbeusedtodictatethevisualstylefortheselectedrow.Usedproperly,thiscanimprovetheuser'sexperiencebymoreclearlyshowingwhichrowoftheGridViewiscurrentlyselected.Forthistutorial,let'shavetheselectedrowbehighlightedwithayellowbackground.

Aswithourearliertutorials,let'sstrivetokeeptheaesthetic-relatedsettingsdefinedasCSSclasses.Therefore,createanewCSSclassinStyles.cssnamedSelectedRowStyle.

.SelectedRowStyle

{

background-color:

Yellow;

}

ToapplythisCSSclasstotheSelectedRowStylepropertyofallGridViewsinourtutorialseries,edittheGridView.skinSkinintheDataWebControlsThemetoincludetheSelectedRowStylesettingsasshownbelow:

GridViewrunat="server"CssClass="DataWebControlStyle">

GridView>

Withthisaddition,theselectedGridViewrowisnowhighlightedwithayellowbackgroundcolor.

Figure8.CustomizetheSelectedRow'sAppearanceUsingtheGridView'sSelectedRowStyleProperty

Step2:

DisplayingtheSelectedProduct'sDetailsinaDetailsView

WiththeProductsGridGridViewcomplete,allthatremainsistoaddaDetailsViewthatdisplaysinformationabouttheparticularproductselected.AddaDetailsViewcontrolabovetheGridViewandcreateanewObjectDataSourcenamedProductDetailsDataSource.SincewewantthisDetailsViewtodisplayparticularinformationabouttheselectedproduct,configuretheProductDetailsDataSourcetousetheProductsBLLclass'sGetProductByProductID(productID)method.

Figure9.InvoketheProductsBLLClass'sGetProductByProductID(productID)Method

HavetheproductIDparameter'svalueobtainedfromtheGridViewcontrol'sSelectedValueproperty.Aswediscussedearlier,theGridView'sSelectedValuepropertyreturnsthefirstdatakeyvaluefortheselectedrow.Therefore,it'simperativethattheGridView'sDataKeyNamespropertyissettoProductID,sothattheselectedrow'sProductIDvalueisreturnedbySelectedValue.

Figure10.SettheproductIDParametertotheGridView'sSelectedValueProperty

OncetheproductDetailsDataSourceObjectDataSourcehasbeenconfiguredcorrectlyandboundtotheDetailsView,thistutorialiscomplete!

Whenthepageisfirstvisitednorowisselected,sotheGridView'sSelectedValuepropertyreturnsNothing.SincetherearenoproductswithaNULLProductIDvalue,norecordsarereturnedbytheGetProductByProductID(productID)method,meaningthattheDetailsViewisn'tdisplayed(seeFigure11).UponclickingaGridViewrow'sSelectbutton,apostbackensuesandtheDetailsViewisrefreshed.ThistimetheGridView'sSelectedValuepropertyreturnstheProductIDoftheselectedrow,theGetProductByProductID(productID)methodreturnsaProductsDataTablewithinformationaboutthatparticularproduct,andtheDetailsViewshowsthesedetails(seeFigure12).

Figure11.WhenFirstVisited,OnlytheGridViewisDisplayed

Figure12.UponSelectingaRow,theProduct'sDetailsareDisplayed

Summary

Inthisandtheprecedingthreetutorialswe'veseenanumberoftechniquesfordisplayingmaster/detailreports.InthistutorialweexaminedusingaselectableGridViewtohousethemasterrecordsandaDetailsViewtodisplaydetailsabouttheselectedmasterrecordonthesamepage.Intheearliertutorialswelookedathowtodisplaymaster/detailsreportsusingDropDownListsanddisplayingmasterrecordsononewebpageanddetailrecordsonanother.

Thistutorialconcludesourexaminationofmaster/detailreports.Startingwiththenexttutorialwe'llbeginourexplo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2