当前位置: 移动技术网 > IT编程>开发语言>.net > 在ASP.NET 2.0中操作数据之十三:在DetailsView控件中使用TemplateField

在ASP.NET 2.0中操作数据之十三:在DetailsView控件中使用TemplateField

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

导言

  比起boundfield、checkboxfield、hyperlinkfield以及其他的那些数据字段控件(data field controls)来说,templatefield提供了一种高度复杂的数据呈现的方法。在上一节中,我们主要着重于在gridview中使用templatefield,以实现:

·在一列中显示多个数据字段。比如说,将firstname和lastname字段合并起来显示在一个gridview列中。

·使用交互web控件来展示数据。我们看到了如何使用一个calendar控件来显示hireddate的值。

·显示基于潜在数据的状态信息。尽管employees表中并没有包含一个关于雇员在公司干了多久的数据列,但我们仍然可以使用templatefield 和格式化方法在gridview中实现这样的功能,就像我们在上一节中做的那样。

  就像在gridview中那样,detailsview控件也可以同样的使用templatefield。在本节教程中,我们将使用一个包含两个templatefield的detailsview来一次一个的显示产品信息。第一个templatefield将整合unitprice、unitsinstock和unitsonorder等数据并显示在一个detailsview行上。第一个templatefield则将显示discontinued的数据,不过将使用格式化方法,在有折扣的时候就显示“yes”,否则就显示“no”。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910499029.jpg

图一:使用两个模板列来自定义显示

好了,让我们开始吧!

第一步:将数据绑定到detailsview

  像前一节中所讨论的那样,要使用templatefield最简单的办法就是先创建一个仅包含boundfield的detailsview控件,然后添加新的templatefield或是将某些boundfield转换成templatefield。因此,我们先通过设计器向页面上添加一个detailsview控件,并绑定一个返回产品列表的objectdatasource给它。这些操作将创建一个带有boundfield和checkboxfield 的detailsview,boundfield用于非布尔值,checkboxfield当然就是用于布尔值了(比如说“是否打折”)。

  打开detailsviewtemplatefield.aspx页面,从工具箱中拖一个detailsview到设计器上。从detailsview的智能标签(smart tag)上选择并添加一个新的调用productsbll类的getproducts ()方法的objectdatasource控件。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910491816.jpg

图二:添加一个新的调用getproducts ()方法的objectdatasource控件

  在这个报表中,删除productid、supplierid、categoryid以及reorderlevel等boundfield。然后,调整剩下的boundfield的顺序以使categoryname和suppliername跟在productname后面。然后设置各boundfield的headertext和formatting属性,不用紧张,随便填,只要你觉得爽就行。就像在gridview中那样,可以通过字段对话框或是直接修改声明代码(declarative syntax。译者注:直译应该是什么?声明语法?不管怎么样,反正就是html视图里面的那些东西)来编辑这些绑定列。最后,清空detailsview的height和width属性,这样可以使其根据需要显示的数据来自动扩展,另外,再在智能标签中选上“启用分页(enable paging)”复选框。

  在做了这些更改之后,你的detailsview控件的声明标记应该像下面这样:

<asp:detailsview id="detailsview1" runat="server" autogeneraterows="false"
 datakeynames="productid" datasourceid="objectdatasource1" allowpaging="true"
 enableviewstate="false">
 <fields>
  <asp:boundfield datafield="productname" headertext="product"
   sortexpression="productname" />
  <asp:boundfield datafield="categoryname" headertext="category"
   readonly="true" sortexpression="categoryname" />
  <asp:boundfield datafield="suppliername" headertext="supplier"
   readonly="true" sortexpression="suppliername" />
  <asp:boundfield datafield="quantityperunit"
   headertext="qty/unit" sortexpression="quantityperunit" />
  <asp:boundfield datafield="unitprice" headertext="price"
   sortexpression="unitprice" />
  <asp:boundfield datafield="unitsinstock"
   headertext="units in stock" sortexpression="unitsinstock" />
  <asp:boundfield datafield="unitsonorder"
   headertext="units on order" sortexpression="unitsonorder" />
  <asp:checkboxfield datafield="discontinued"
   headertext="discontinued" sortexpression="discontinued" />
 </fields>
</asp:detailsview>

  同样,我们还是花点时间到浏览器中看看效果吧!现在,你可以看到一个单独的产品(chai),它包括一些显示其属性的行:名称、分类、供应商、库存量、订货量还有它的打折状态。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910498587.jpg

图三:使用一组绑定列来显示产品明细

第二步:将单价、库存量和订货量合并在一列中

  detailsview有一列用于unitprice、unitsinstock和unitsonorder。通过templatefield可以将这3个数据合并到一行中,你可以添加一个新的templatefield,也可以将unitprice、unitsinstock或unitsonorder任何一个boundfield直接转换成templatefield。虽然我个人还是喜欢将已有的boundfield转换成templatefield这种方式,不过这里我们还是来联系一下添加新的templatefield吧。

  在detailsview的智能标签的弹出菜单中点击“编辑字段(edit fields)”。在弹出的字段对话框中,添加一个新的templatefield并将其headertext属性设置为“price and inventory”,然后将这个新的templatefield移动到unitprice的上面。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910497145.jpg

图四:给detailsview控件添加一个模板列

  由于新添加的templatefield将要显示unitprice、unitsinstock以及unitsonorder等boundfield中的数据,所以让我们先把这几个boundfield删了。

  这一个步骤的最后一个任务是定义“price and inventory”这个templatefield 的itemtemplate,你可以通过设计器中detailsview的模板编辑界面也可以手工编写声明代码来完成这个任务。就像gridview那样,在智能标签的弹出菜单中点击“编辑模板”( edit templates),就可以使用模板编辑界面了。这里你可以在下拉框中选择你想要编辑的模板并从工具箱中添加任何你喜欢的web控件。

  在本教程中,首先给“price and inventory”模板列的itemtemplate添加一个label。然后,在label控件的智能标签上点击“编辑数据绑定(edit databindings)”并将其text属性绑定到unitprice字段上。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910507458.jpg

图五:将label的text属性绑定到unitprice字段上

  将单价格式化为货币形式,做了这个操作之后,“price and inventory”模板列的label上仅显示了所选产品的单价。图六向我们展示了到现在为止我们所做的成果。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910515858.jpg

图六:“单价和总量”模板列显示了单价

  注意产品的单价现在还没有格式化为货币格式。如果是一个boundfield,格式化可以通过将htmlencode属性设置为false并将dataformatstring属性设置为“{0:formatspecifier}”来实现。然而,在templatefield中,任何格式化说明都必须在数据绑定语法中指定或是通过使用一个在应用程序的某个地方(比如说在asp.net页面的后置代码类中)编写的格式化方法。

  要指定label的数据绑定代码中的格式化,可以在label的智能标签中点击“编辑数据绑定(edit databindings)”,然后在弹出的数据绑定对话框中的格式(format)下拉框直接输入格式化说明或选择一个预定义的格式化字符串。就像boundfield的dataformatstring属性那样,格式化使用{0:formatspecifier}来进行指定。为了使unitprice字段使用货币格式,我们可以在那个下拉框中选择一个合适值,也可以直接输入“{0:c}”。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910517645.jpg

图七:将单价格式化为货币形式

  说明一下,格式化说明是bind或eval方法的第二个参数。刚才通过设计器添加的这个设置表现为以下的标记语言:<asp:label id="label1" runat="server" text='<%# eval("unitprice", "{0:c}") %>'></asp:label>将剩下的数据字段添加到templatefield中

  现在,我们已经在“price and inventory”模板列中显示并格式化了unitprice字段,不过我还需要将unitsinstock和unitsonorder显示出来。让我们把它们显示在单价的下面一行的圆括号中吧!在设计器的模板编辑器中,这些用于显示的标记语言可以简单的用键盘输入,当然你需要先将光标定位到模板中的某个位置。另外,你也可以直接在声明代码中直接输入。

  添加了静态的标记语言、label控件以及数据绑定代码,所以“price and inventory”模板列可以像下面这样显示单价和总量信息:

单价(in stock / on order: 库存量 / 订货量)

做了这些之后,你的detailsview的声明标记代码应该像这个样子:

<asp:detailsview id="detailsview1" runat="server" autogeneraterows="false"
 datakeynames="productid" datasourceid="objectdatasource1" allowpaging="true"
 enableviewstate="false">
 <fields>
  <asp:boundfield datafield="productname"
   headertext="product" sortexpression="productname" />
  <asp:boundfield datafield="categoryname" headertext="category"
   readonly="true" sortexpression="categoryname" />
  <asp:boundfield datafield="suppliername"
   headertext="supplier" readonly="true"
   sortexpression="suppliername" />
  <asp:boundfield datafield="quantityperunit"
   headertext="qty/unit" sortexpression="quantityperunit" />
  <asp:templatefield headertext="price and inventory">
   <itemtemplate>
    <asp:label id="label1" runat="server"
     text='<%# eval("unitprice", "{0:c}") %>'></asp:label>
    <br />
    <strong>
    (in stock / on order: </strong>
    <asp:label id="label2" runat="server"
     text='<%# eval("unitsinstock") %>'></asp:label>
    <strong>/</strong>
    <asp:label id="label3" runat="server"
     text='<%# eval("unitsonorder") %>'>
    </asp:label><strong>)</strong>
   </itemtemplate>
  </asp:templatefield>
  <asp:checkboxfield datafield="discontinued"
   headertext="discontinued" sortexpression="discontinued" />
 </fields>
</asp:detailsview>

做了这些修改之后,我们已经把单价和总量信息统一的显示在一个单独的detailsview行中了。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910513945.jpg

图八:单价和总量信息显示在一个单独的行上了

第三步:自定义折扣字段的信息

  products表的discontinued字段是一个bit值,它指明一个产品是否打折。当把一个detailsview(或者gridview)绑定到一个数据源控件时,布尔型的字段(比如说discontinued)会被实现为checkboxfield,而非布尔型的字段(比如说productid、productname等等)将被实现为boundfield。checkboxfield被呈现为一个禁用的checkbox,如果数据的值为true则checkbox为选中状态,否则就是未选中状态。

  比起显示一个checkboxfield,可能我们更希望将其显示为一个文本以说明这个产品是不是有折打。要做到这一点,我们可以从detailsview中删掉这个checkboxfield,再添加一个boundfield,并将其datafield属性设置到discontinued上。嗯,花点时间完成它!做完这个改变后,detailsview对那些打折的产品就显示一个“true”,而对其他的就显示一个“false”。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910514946.jpg

图九:字符串“true”和“false”用来显示打折状态

  想想一下,我们不想用“true”或者“false”,而是想要“yes”和“no”。这样的自定义可以由一个templatefield和一个格式化方法来实现。格式化方法可以接受若干个输入参数,但是智能返回一个用于插入到模板中的html(字符串类型的)

  在detailsviewtemplatefield.aspx页面的后置代码类中添加一个名为displaydiscontinuedasyesorno的格式化方法,它接受一个布尔型的值作为参数并返回一个字符串。就像前一节中讨论的那样,这个方法必须标记为protected或是public,不然就不能从模板中访问到它了。

protected string displaydiscontinuedasyesorno(bool discontinued)
{
 if (discontinued)
  return "yes";
 else
  return "no";
}

这个方法检查输入的参数(是否折扣),如果为true则返回“yes”,否则就返回“no”。

注意:回忆一下我们在上一节中的相关内容,传递给格式化方法的参数可能是空值,所以我们需要在访问雇员的hireddate之前对它进行空值检查。这样的检查在这里却是不需要的,因为discontinued字段永远不会是空值。此外,这也是为什么这个方法接受的是一个布尔值而不是productsrow实例或object类型的参数的原因。

  完成了这个格式化方法之后,剩下的就只是在templatefield的itemtemplate中调用它了。要创建这个templatefield,我们可以删除discontinued绑定列再添加一个新的templatefield,也可以直接将discontinued boundfield转换成templatefield。然后,在源视图(译者注:就是html视图)编辑templatefield以使其包含一个调用displaydiscontinuedasyesorno方法的itemtemplate,传过去的参数就是当前productrow实例的discontinued属性。这个可以通过eval方法来访问。现在,这个templatefield的标记代码就像这样了:

<asp:templatefield headertext="discontinued" sortexpression="discontinued">
 <itemtemplate>
  <%# displaydiscontinuedasyesorno((bool)
   eval("discontinued")) %>
 </itemtemplate>
</asp:templatefield>

  这样,displaydiscontinuedasyesorno方法就会在呈现detailsview时被调用,传给它的是productrow实例的discontinued值。由于eval方法返回的是一个obejct类型的值,而displaydiscontinuedasyesorno方法仅接受布尔型的参数,所以我们将eval方法的返回值转换成布尔型的。根据接收到的值,displaydiscontinuedasyesorno方法将会返回“yes”或“no”,这个返回值就是要在这个detailsview行中显示的东西。

http://www.lhsxpumps.com/_images/10qianwan/20171212/b_1_201712121910514475.jpg

图十:现在在折扣行中显示的就是“yes”或“no”了

总结

在detailsview控件中,相对于其他的列控件来说,模板列可以处理更加复杂的数据呈现。模板列主要用于这样一些情况:

·一个detailsview列中需要显示多个数据列

·使用一个web控件来展示数据比一段简单的文本更好

·页面的输出取决于绑定到detailsview的数据,比如说元数据或者说是数据的重新格式化

虽然模板列可以高度复杂的呈现detailsview的数据,但detailsview的输入仍然让人感到有些别扭,因为它把每个字段都显示成html标记<table>的一行。

formview控件提供了一种更加复杂的输出呈现。formview并不含有什么列,它仅仅包括一系列的模板(itemtemplate、edititemtemplate、headertemplate等等)。在下一节中,我们将看到如何使用formview来达到呈现更多控件的效果。

编程愉快!

关于作者

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

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

相关文章:

验证码:
移动技术网