当前位置: 移动技术网 > IT编程>开发语言>.net > 在ASP.NET 2.0中操作数据之三十四:基于DataList和Repeater跨页面的主/从报表

在ASP.NET 2.0中操作数据之三十四:基于DataList和Repeater跨页面的主/从报表

2017年12月12日  | 移动技术网IT编程  | 我要评论

古代美女换装游戏,八爪椅,广东通缉涉毒逃犯

导言

  在前面一章里我们学习了如何在一个页里显示主/从信息.另外一种经常使用的模式就是将主从信息用两个页分别显示.在前面的跨页面的主/从报表 我们通过gridview显示所有的supplier来使用这个模式.gridview里包含一个hyperlinkfield,链接到另外一个页,并将supplierid通过querystring传过去.第二个页使用gridview列出了选中的supplier提供的product.

  这样的两页主/从表也可以用datalist和repeater来实现.唯一的区别是datalist和repeater都不提供hyperlinkfield.所以我们需要添加一个hyperlink控件或者在itemtemplate里使用html <a>.hyperlink的navigateurl属性和<a>的href属性可以通过声明或者编程来自定义.

本章我们将探讨使用repeater列出categories.每个list item都包含了category的name和description.通过name可以直接链接到第二个页面.在第二页里用datalist显示选中的categroy提供的proudct.

第一步: 列出categories

  所有创建主从表的第一步都是显示主记录.因此,我们首先在"主"页里显示categories.打开datalistrepeaterfiltering文件夹里的categorylistmaster.aspx页,添加一个repeater,然后通过智能标签添加一个objectdatasource.使用categriesbll类的getcategories方法配置它.见图1.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909311129.jpg
图 1:使用categoriesbll类的getcategories方法配置objectdatasource

我们先不关心如何添加link.将repeater的template配置成显示每个category的name和description.见下面的代码:

<asp:repeater id="repeater1" runat="server" datasourceid="objectdatasource1"
 enableviewstate="false">
 <headertemplate>
 <ul>
 </headertemplate>
 
 <itemtemplate>
 <li><%# eval("categoryname") %> - <%# eval("description") %></li>
 </itemtemplate>
 
 <footertemplate>
 </ul>
 </footertemplate>
</asp:repeater>
 
<asp:objectdatasource id="objectdatasource1" runat="server"
 oldvaluesparameterformatstring="original_{0}"
 selectmethod="getcategories" typename="categoriesbll">
</asp:objectdatasource>

完成了上面的代码后,在浏览器里浏览页面.如图2所示.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909315643.jpg
图 2: 列出所有的 category

第二步: 将category name 转换成链到details page 的link

  我们现在来添加一个link,当用户点击时,将会链到第二个页(productsforcategorydetails.aspx),在这个页里显示"从"信息.这页里用datalist显示选中的category的product.为了判断是哪个category的链接被点了,我们需要将categoryid传到第二页.最直接的方法是通过querystring.我们通过名为categoryid的querystring字段将这个传给productsforcategorydetails.aspx.例如,查看beverages categroy下的product,categoryid为1.用户将访问productsforcategorydetails.aspx?categoryid=1页.

  为了创建hyperlink我们需要添加hyperlink控件或者在itemtemplate里添加html<a>.在每行的hyperlink都相同的情况下,两种方法都足够了.对repeater来说我更愿意使用<a>.见下面的代码:

<li>
 <a href='productsforcategorydetails.aspx?categoryid=<%# eval("categoryid") %>'>
 <%# eval("categoryname") %>
 </a> - <%# eval("description") %>
</li>

注意categoryid可以直接通过href属性写入.注意引号和省略号.

<li>
 <asp:hyperlink runat="server" text='<%# eval("categoryname") %>'
 navigateurl='<%# "productsforcategorydetails.aspx?categoryid=" &
  eval("categoryid") %>'>
 </asp:hyperlink>
 - <%# eval("description") %>
</li>

注意在绑定语法里静态url— productsforcategorydetails.aspx?categoryid— 是如何直接和eval("categoryid")的结果串联.

  使用hyperlink控件的一个好处是如果需要的话可以编程访问repeater的itemdatabound event handler.例如你可以将没有关联product的categories显示为文本,而不是link.将那些没有关联product的categories的hyperlink的navigateurl属性设为一个空的字符串,这样category name就显示为一个text(而不是link).更多的通过itemdatabound event handler的编程来格式化datalist和repeater内容的信息请看格式化datalist和repeater的数据.如果你在跟着教程做的话,使用上面两种方法都可以.当浏览这页时,每个category name都以link的形式呈现,可以链接到productsforcategorydetails.aspx页,并将categoryid的值传过去.见图3.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909316929.jpg
图 3: category names 现在链接到productsforcategorydetails.aspx页

第三步: 列出选中的category下的products

  完成categorylistmaster.aspx页后,我们现在来完成"从"页,productsforcategorydetails.aspx.打开这个页,拖一个datalist控件进来,并将id设置为productsincategory.然后在智能标签里选择添加一个名为productsincategorydatasource的objectdatasource.并用productsbll类的getproductsbycategoryid(categoryid)方法配置它.在insert,update,delete标签里选择none.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909319971.jpg
图 4: 使用productsbll类的getproductsbycategoryid(categoryid)方法配置objectdatasource

  由于getproductsbycategoryid(categoryid)方法接收一个参数,所以向导会提示我们指定参数来源.设置parameter source为querystring,querystringfield为categoryid.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909317243.jpg
图 5: 使用querystring field 作为parameter source

  象前面教程里看到的那样,完成数据源配置后,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的截图.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909328528.jpg
图 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)方法配置它.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909325800.jpg
图 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).

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909321315.jpg
图 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事件.

  在这个例子里我们可以在page_load事件处理里设置label的visible属性..由于数据绑定到datalist先于page的load事件.然而,这种方法在一般情况下不会起作用,因为从objectdatasource来的数据是在页面周期之后绑定到datalist.如果显示的数据基于另一个控件的值,例如,象在使用dropdownlist显示主记录的主/从表的例子里,数据直到page的生命周期的prerender后才绑定到控件.

  一个在所有情况下都起作用的解决方案是在datalist的itemdatabound(或itemcreated)事件处理中设置visible为false.在这种情况下我们知道数据源里至少有一个数据项,因此可以隐藏noproductsmessage label.除了这个event handler外,我们还需要一个datalist databingd的事件处理,来初始化label的visible属性为true.由于databinding时间在itemdatabound事件后激发,label的visible属性会初始化为true.如果有数据,它会被设为false.见下面的代码:

protected void productsincategory_databinding(object sender, eventargs e)
{
 // show the label
 noproductsmessage.visible = true;
}
 
protected void productsincategory_itemdatabound(object sender, datalistitemeventargs e)
{
 // if we have a data item, hide the label
 if (e.item.itemtype == listitemtype.item ||
 e.item.itemtype == listitemtype.alternatingitem)
 noproductsmessage.visible = false;
}
      

  在northwind数据库里的category都和一个或多个product关联.为了测试上面的功能,我手动修改了northwind数据库,将produce category(categoryid=7)的product都和seafood category(categoryid=8)关联起来.这个可以在server explorer里选择new query并使用下面的语句:

update products set
 categoryid = 8
where categoryid = 7

  更新了数据库后,回到categorylistmaster.aspx页,点produce链接.由于produce category下面已经没有任何product,所以你会看到"there are no products for the selected category…"的提示,见图9.

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121909329058.jpg
图 9: 选中的category下无product时会显示一个提示消息

总结

  主/从记录可以在一个页上显示,也可以在两个页上分别显示.在本章里我们学习了如何在"主"页上用repeater列出category,将相关的product在"从"页上列出.每个"主"页上的项都包含一个链到"从"页的link,并将行的categoryid值传过去.

  在"从"页里通过productsbll类的getproductsbycategoryid(categoryid)方法返回product.categoryid参数通过querystring的categoryid值指定.而且我们还将category的细节使用formview显示在"从"页里,当选中的category无关联product时,会显示一条提示信息.

  祝编程愉快!

作者简介

  scott mitchell,著有六本asp/asp.net方面的书,是4guysfromrolla.com的创始人,自1998年以来一直应用 微软web技术。scott是个独立的技术咨询顾问,培训师,作家,最近完成了将由sams出版社出版的新作,24小时内精通asp.net 2.0。他的联系电邮为,也可以通过他的博客与他联系。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网