加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在ASP.NET 2.0中操作数据之三十四:基于DataList和Repeater跨页

发布时间:2016-11-24 00:16:36 所属栏目:MsSql教程 来源:站长网
导读:导言 在前面一章里我们学习了如何在一个页里显示主/从信息.另外一种经常使用的模式就是将主从信息用两个页分别显示.在前面的跨页面的主/从报表 我们通过GridView显示所有的supplier来使用这个模式.GridView里包含一个HyperLinkField,链接到另外一个页,并将

  象前面教程里看到的那样,完成数据源配置后,Visual Studio会自动创建一个ItemTemplate列出每个字段的name和value.我们只显示name,supplier和price.将DataList的RepeatColumns属性设为2.完成这些后你的声明标记看起来应该和下面差不多:

<asp:DataList ID="ProductsInCategory" runat="server" DataKeyField="ProductID"
 RepeatColumns="2" DataSourceID="ProductsInCategoryDataSource"
 EnableViewState="False">
 <ItemTemplate>
 <h5><%# Eval("ProductName") %></h5>
 <p>
  Supplied by <%# Eval("SupplierName") %><br />
  <%# Eval("UnitPrice", "{0:C}") %>
 </p>
 </ItemTemplate>
</asp:DataList>
 
<asp:ObjectDataSource ID="ProductsInCategoryDataSource"
 OldValuesParameterFormatString="original_{0}" runat="server"
 SelectMethod="GetProductsByCategoryID" TypeName="ProductsBLL">
 <SelectParameters>
 <asp:QueryStringParameter Name="categoryID" QueryStringField="CategoryID"
  Type="Int32" />
 </SelectParameters>
</asp:ObjectDataSource>

  现在我们来看看效果,先浏览CategoryListMater.aspx页.然后在列出的category上点一个link.这样就会跳到ProductsForCategoryDetails.aspx页,并将CategoryID通过querystring传过去.ProductsInCategoryDataSource ObjectDataSource会返回指定category的product并将它们显示在DataList中,每行两个.图6是点击Beverages的截图.

/uploads/allimg/c161121/14OI9314445P-195201.png
图 6: 每行两个的显示Beverages

第四步: 在ProductsForCategoryDetails.aspx里显示 Category 信息

  当用户在CategoryListMaster.aspx页点击一个category时,会跳到ProductsForCategoryDetails.aspx页并显示选中的categry下的product.然而在这个页里并没有包含哪个category被选中了的信息.用户可能想点Beverages,但是结果点了Condiments,这时他没办法知道自己是否点错了.为了剞劂这个问题,我们可以将选中的category信息显示在ProductsForCategoryDetails.aspx页的顶部(name和description).在ProductsForCategoryDetails.aspx的Repeater上添加一个FormView.然后通过智能标签添加一个名为CategoryDataSource的ObjectDataSource,并用CategoriesBLL类的GetCategoryByCategoryID(categoryID)方法配置它.

/uploads/allimg/c161121/14OI9314H550-20XU.png
图 7: 配置CategoryDataSource

  在第三步增加ProductsInCategoryDataSource ObjectDataSource时,向导提示我们为GetCategoryByCategoryID(categoryID)方法指定输入参数.在这里我们使用和前面一样的配置,将parameter source设为QueryString,QueryStringField设为CategoryID(见图5).

  完成向导后,Visual Studio会为FormView自动创建ItemTemplate,EditItemTemplate和InsertItemTemplate.由于只提供只读的界面,我们将EditItemTemplate和InsertItemTemplate.当然你也可以自定义FormView的ItemTemplate.完成上面的操作偶你的标记语言应该和下面差不多:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="CategoryID"
 DataSourceID="CategoryDataSource" EnableViewState="False" Width="100%">
 <ItemTemplate>
 <h3>
  <asp:Label ID="CategoryNameLabel" runat="server"
  Text='<%# Bind("CategoryName") %>' />
 </h3>
 <p>
  <asp:Label ID="DescriptionLabel" runat="server"
  Text='<%# Bind("Description") %>' />
 </p>
 </ItemTemplate>
</asp:FormView>
 
<asp:ObjectDataSource ID="CategoryDataSource" runat="server"
 OldValuesParameterFormatString="original_{0}"
 SelectMethod="GetCategoryByCategoryID" TypeName="CategoriesBLL">
 <SelectParameters>
 <asp:QueryStringParameter Name="categoryID" Type="Int32"
  QueryStringField="CategoryID" />
 </SelectParameters>
</asp:ObjectDataSource> 

注意:我们还在FormView上加了一个HyperLink,它会将用户链回到category页(CategoryListMaster.aspx).

/uploads/allimg/c161121/14OI931500Q0-2121N.png
图 8: Category 信息显示在页的顶部

第五步: 如果选中的Category下无 Products 则显示一个提示信息

  无论有没有相关联的product,CategoryListMaster.aspx页都会列出所有的category.如果用户点了一个无product的category,由于数据源为空,在ProductsForCategoryDetails.aspx页里的DataList将不会显示.在前面的教程里我们看到了GridView提供了一个EmptyDataText属性,可以用来在数据源无记录时定义一个消息.不幸的是DataList和Repeater都没有这个属性.

  为了在category无product时提示用户,我们需要在页里加一个Label控件.在没有匹配的product时将它的Text属性设置为要显示的信息.我们需要根据DataList有没有内容来编程设置它的Visible属性.首先在DataList下加一个Label控件.将它的ID设为NoProductsMessage,Text设为"There are no products for the selected category…".然后我们根据是否有数据绑定到ProductsInCategory DataList来设置它的Visible属性.这一步需要在数据绑定到DataList之后做.对GridView,DetailsView和FormView来说,我们可以为DataBound事件创建一个event handler.在数据绑定完后激发.然而DataList和Repeater都没有DataBound事件.

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读