在每个标签前加上一个包含说明文本(例如,“书名 ID”、“书名”、“出版日期”和“价格”)的 HTML Label 以添加描述这些标签的文本。为此,请打开工具箱,将 Label 从“HTML”选项卡拖到 Web 窗体设计器表面。将标签放在对应的绑定标签的前面,并适当修改其文本。重复此过程以创建四个标签。
如果类代码尚不可见,在“解决方案资源管理器”中双击 MyPaymentRules.vb 并将如下函数添加到类中: ' Visual Basic Public Class MyPaymentRules ' Add the following code Dim price As Double Public Function CalcDiscount(ByVal price As Double) Return 0.9 * price End Function ' End of the new code. End Class 使用业务对象
在本节中,您将新的绑定标签添加到 Web 窗体页。新标签显示包括 10% 折扣的优惠价格。
实现业务对象
将 WebForms Label 控件从工具箱拖到 Web 窗体设计器表面(它采用 ID Label5)。
将 HTML Label 控件从工具箱拖到 Web 窗体设计器表面并将其放在 Label5 的前面。
将标签文本更改为“优惠价格”。
查看 WebForm1.aspx.cs 文件(或 WebForm1.aspx.vb 文件)并将下列代码添加到 DataGrid1_SelectedIndexChanged 方法/子方法的结尾: // C# // Declare an instance of the business object: MyPaymentRules pr = new MyPaymentRules(); // Invoke the CalcDiscount Method: try { decimal price = myDataSet1.titles[index].price; Label5.Text = String.Format("{0:C}", pr.CalcDiscount(Convert.ToDouble(price))); } catch { // If the price is blank, display a message: Label1.Text = "Price is not available for this item."; Label5.Text = "Discount is not available for this item."; } ' Visual Basic ' Declare an instance of the business object Dim pr As New MyPaymentRules()
' Invoke the CalcDicount Method: Try Dim price As decimal