FormView控件的数据呈现和处理

来源:互联网 发布:arp a mac好多相同 编辑:程序博客网 时间:2024/05/16 08:29

转载地址:http://book.51cto.com/art/201007/213946.htm

FormView控件的数据呈现和处理(1)

FormView控件提供了内置的数据处理功能,只需绑定到支持这些功能的数据源控件,并进行配置,无需编写任何代码即可实现对数据的分页和增删改功能。要使用FormView控件内置的增删改功能,需要为更新操作提供EditItemTemplate和InsertItemTemplate模板,FormView 控件显示指定的模板以提供允许用户修改记录内容的用户界面。每个模板都包含用户可以单击以执行编辑或插入操作的命令按钮。用户单击命令按钮时,FormView控件使用指定的编辑或插入模板重新显示绑定记录以允许用户修改记录。插入或编辑模板通常包括一个允许用户显示空白记录的"插入"按钮或保存更改的"更新"按钮。用户单击"插入"或"更新"按钮时,FormView控件将绑定值和主键信息传递给关联的数据源控件,该控件执行相应的更新。例如,SqlDataSource控件使用更改后的数据作为参数值来执行SQL Update语句。

由于FormView控件的各个项通过自定义模板来呈现,因此,控件并不提供内置的实现某一功能(如删除)的特殊按钮类型,而是通过按钮控件的CommandName属性与内置的命令相关联。FormView控件提供如下命令类型(区分大小写):

Edit:引发此命令控件转换到编辑模式,并用已定义的EditItemTemplate呈现数据。

New:引发此命令控件转换到插入模式,并用已定义的InsertItemTemplate呈现数据。

Update:此命令将使用用户在EditItemTemplate界面中输入的值在数据源中更新当前所显示的记录。引发ItemUpdating和ItemUpdated事件。

Insert:此命令用于将用户在InsertItemTemplate界面中输入的值在数据源中插入一条新的记录。引发ItemInserting和ItemInserted事件。

Delete:此命令删除当前显示的记录。引发ItemDeleting和ItemDeleted事件。

Cancel:此命令在更新或插入操作中取消操作和放弃用户输入值,然后控件会自动转换到DefaultMode属性指定的模式。

在命令所引发的事件中,我们可以执行一些额外的操作,例如对于Update和Insert命令,因为ItemUpdating和ItemInserting事件是在更新或插入数据源之前触发的,所以可以在ItemUpdating和ItemInserting事件中先判断用户的输入值,满足要求后才访问数据库,否则取消操作。

下面通过【例8-8】演示如何使用FormView控件完成数据的分页显示,并实现编辑、更新、删除、添加等数据处理功能。

【例8-8】在FormView控件中实现数据的分页显示,并实现编辑、更新、删除和添加操作。

(1) 在DataBinding网站中新建一个名为FormViewBinding.aspx的页面,在页面上添加一个FormView控件。

(2) 为FormView控件添加并编辑项模板,由于要实现数据的更新和插入操作,需要三种项模板:ItemTemplate、EditItemTemplate和InsertItemTemplate,分别在显示、更新和插入状态下呈现数据。在FormView控件中也提供了模板编辑界面(如DataList控件),这里我们直接在页面代码中进行编辑,首先编辑EditItemTemplate页面代码,如下:

  1. <EditItemTemplate>
  2. <table style="width:100%;">
  3. <tr>
  4. <td width="40%"><asp:Label ID="Label1" runat="server" Text="学号"
  5. Width="100%"></asp:Label> </td>
  6. <td width="60%"><asp:Label ID="noLabel1" runat="server" Text='<%# Eval("no") %>' /></td>
  7. </tr>
  8. <tr>
  9. <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名"
  10. Width="100%"></asp:Label> </td>
  11. <td width="60%"><asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /></td>
  12. </tr>
  13. <tr>
  14. <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日"
  15. Width="100%"></asp:Label> </td>
  16. <td width="60%"> <asp:TextBox ID="birthTextBox" runat="server" Text='<%# Bind("birth") %>' /></td>
  17. </tr>
  18. <tr>
  19. <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
  20. <td width="60%"><asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>' ></td>
  21. </tr>
  22. <tr>
  23. <td width="40%"> </td>
  24. <td width="60%" align="center">
  25. <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
  26. CommandName="Update" Text="更新" />
  27. <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
  28. CommandName="Cancel" Text="取消" />
  29. </td>
  30. </tr>
  31. </table>
  32. </EditItemTemplate>

编辑状态模板中用一个Label控件和一个TextBox控件代表数据源中的一个字段,其中,TextBox控件为绑定字段,共四行,对应于数据源记录中的四个字段。与DataList控件不同,TextBox控件的绑定表达式为<%# Bind("address") %>,表达式标记中调用了Bind方法(DataList中使用Eval方法),Bind方法构成与数据源的双向影射,通过双向影射配合数据源控件可以完成控件内置的更新操作(仅对数据源控件有效)。而DataList控件中使用的Eval方法为单向影射不能更新数据。

用于数据显示的ItemTemplate和用于插入的InsertItemTemplate与之类似,这里不再赘述。

(3) 配置完各个项模板之后,为FormView控件配置分页,由于分页功能是内置的,只需要设置FormView控件的分页属性即可,如图8-32所示。

图8-32 为FormView设置分页属性

FormView控件的数据呈现和处理(2)

(4) 调整FormView控件的外观,设置页眉模板HeaderTemplate,完成整个页面的设计,完整的FormView控件设计代码如下:

  1. <asp:FormView ID="FormView1" runat="server" DataKeyNames="no"
  2. DataSourceID="SqlDataSource1" AllowPaging="True" CellPadding="4" ForeColor="#333333"
  3. Width="231px">
  4. <PagerSettings Mode="NextPreviousFirstLast" NextPageText="下一页&amp;gt;"
  5. PreviousPageText="上一页&amp;lt;" />
  6. <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  7. <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
  8. <EditItemTemplate>
  9. <table style="width:100%;">
  10. <tr>
  11. <td width="40%"><asp:Label ID="Label1" runat="server" Text="学号"
  12. Width="100%"></asp:Label> </td>
  13. <td width="60%"><asp:Label ID="noLabel1" runat="server" Text='<%# Eval("no") %>' /></td>
  14. </tr>
  15. <tr>
  16. <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名"
  17. Width="100%"></asp:Label> </td>
  18. <td width="60%"><asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /></td>
  19. </tr>
  20. <tr>
  21. <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日" Width="100%"></asp:Label> </td>
  22. <td width="60%"> <asp:TextBox ID="birthTextBox" runat="server" Text='<%# Bind("birth") %>' /></td>
  23. </tr>
  24. <tr>
  25. <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
  26. <td width="60%"><asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>' ></td>
  27. </tr>
  28. <tr>
  29. <td width="40%"> </td>
  30. <td width="60%" align="center">
  31. <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" />
  32. <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" />
  33. </td>
  34. </tr>
  35. </table>
  36. </EditItemTemplate>
  37. <InsertItemTemplate>
  38. <table style="width:100%;">
  39. <tr>
  40. <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名" Width="100%"></asp:Label> </td>
  41. <td width="60%"><asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /></td>
  42. </tr>
  43. <tr>
  44. <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日" Width="100%"></asp:Label> </td>
  45. <td width="60%"><asp:TextBox ID="birthTextBox" runat="server" Text='<%# Bind("birth") %>' /></td>
  46. </tr>
  47. <tr>
  48. <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
  49. <td width="60%"><asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>' ></td>
  50. </tr>
  51. <tr>
  52. <td width="40%"> </td>
  53. <td width="60%" align="center">
  54. <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Inert" Text="插入" />
  55. <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" />
  56. </td>
  57. </tr>
  58. </table>
  59. </InsertItemTemplate>
  60. <ItemTemplate>
  61. <table style="width:100%;">
  62. <tr>
  63. <td width="40%"><asp:Label ID="Label1" runat="server" Text="学号" Width="100%"></asp:Label> </td>
  64. <td width="60%"><asp:Label ID="noLabel" runat="server" Text='<%# Eval("no") %>' /></td>
  65. </tr>
  66. <tr>
  67. <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名" Width="100%"></asp:Label> </td>
  68. <td width="60%"><asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>' /></td>
  69. </tr>
  70. <tr>
  71. <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日" Width="100%"></asp:Label> </td>
  72. <td width="60%"><asp:Label ID="birthLabel" runat="server" Text='<%# Bind("birth") %>' /></td>
  73. </tr>
  74. <tr>
  75. <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
  76. <td width="60%"><asp:Label ID="addressLabel" runat="server" Text='<%# Bind("address") %>' /></td>
  77. </tr>
  78. <tr>
  79. <td width="40%"> </td>
  80. <td width="60%" align="right">
  81. <asp:LinkButton ID="NewButton" runat="server" CausesValidation="True" CommandName="New" Text="新建" />
  82. <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="更新" />
  83. <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" />
  84. </td>
  85. </tr>
  86. </table>
  87. </ItemTemplate>
  88. <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
  89. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
  90. <HeaderTemplate>
  91. 学生详细信息
  92. </HeaderTemplate>
  93. </asp:FormView>

页面设计外观如图8-33所示。

图8-33 FormViewBinding页面设计效果
原创粉丝点击