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页面代码,如下:
- <EditItemTemplate>
- <table style="width:100%;">
- <tr>
- <td width="40%"><asp:Label ID="Label1" runat="server" Text="学号"
- Width="100%"></asp:Label> </td>
- <td width="60%"><asp:Label ID="noLabel1" runat="server" Text='<%# Eval("no") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名"
- Width="100%"></asp:Label> </td>
- <td width="60%"><asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日"
- Width="100%"></asp:Label> </td>
- <td width="60%"> <asp:TextBox ID="birthTextBox" runat="server" Text='<%# Bind("birth") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>' ></td>
- </tr>
- <tr>
- <td width="40%"> </td>
- <td width="60%" align="center">
- <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
- CommandName="Update" Text="更新" />
- <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
- CommandName="Cancel" Text="取消" />
- </td>
- </tr>
- </table>
- </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控件设计代码如下:
- <asp:FormView ID="FormView1" runat="server" DataKeyNames="no"
- DataSourceID="SqlDataSource1" AllowPaging="True" CellPadding="4" ForeColor="#333333"
- Width="231px">
- <PagerSettings Mode="NextPreviousFirstLast" NextPageText="下一页&gt;"
- PreviousPageText="上一页&lt;" />
- <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
- <EditItemTemplate>
- <table style="width:100%;">
- <tr>
- <td width="40%"><asp:Label ID="Label1" runat="server" Text="学号"
- Width="100%"></asp:Label> </td>
- <td width="60%"><asp:Label ID="noLabel1" runat="server" Text='<%# Eval("no") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名"
- Width="100%"></asp:Label> </td>
- <td width="60%"><asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日" Width="100%"></asp:Label> </td>
- <td width="60%"> <asp:TextBox ID="birthTextBox" runat="server" Text='<%# Bind("birth") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>' ></td>
- </tr>
- <tr>
- <td width="40%"> </td>
- <td width="60%" align="center">
- <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" />
- <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" />
- </td>
- </tr>
- </table>
- </EditItemTemplate>
- <InsertItemTemplate>
- <table style="width:100%;">
- <tr>
- <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:TextBox ID="birthTextBox" runat="server" Text='<%# Bind("birth") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>' ></td>
- </tr>
- <tr>
- <td width="40%"> </td>
- <td width="60%" align="center">
- <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Inert" Text="插入" />
- <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" />
- </td>
- </tr>
- </table>
- </InsertItemTemplate>
- <ItemTemplate>
- <table style="width:100%;">
- <tr>
- <td width="40%"><asp:Label ID="Label1" runat="server" Text="学号" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:Label ID="noLabel" runat="server" Text='<%# Eval("no") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label2" runat="server" Text="姓名" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label3" runat="server" Text="生日" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:Label ID="birthLabel" runat="server" Text='<%# Bind("birth") %>' /></td>
- </tr>
- <tr>
- <td width="40%"><asp:Label ID="Label4" runat="server" Text="地址" Width="100%"></asp:Label> </td>
- <td width="60%"><asp:Label ID="addressLabel" runat="server" Text='<%# Bind("address") %>' /></td>
- </tr>
- <tr>
- <td width="40%"> </td>
- <td width="60%" align="right">
- <asp:LinkButton ID="NewButton" runat="server" CausesValidation="True" CommandName="New" Text="新建" />
- <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="更新" />
- <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" />
- </td>
- </tr>
- </table>
- </ItemTemplate>
- <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
- <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <HeaderTemplate>
- 学生详细信息
- </HeaderTemplate>
- </asp:FormView>
页面设计外观如图8-33所示。
图8-33 FormViewBinding页面设计效果- FormView控件的数据呈现和处理
- FormView控件的数据呈现和处理3
- FormView 控件修改绑定数据的方式
- FormView和GridView数据同步
- 使用FormView控件对数据分页
- 控件呈现的顺序
- FormView控件
- 获取formview里面的控件的值
- ASP.NET 2.0 的数据绑定控件概述与区别(GridView、DetailsView、FormView 、Repeater、DataList)
- ASP.NET 2.0 的数据绑定控件概述与区别(GridView、DetailsView、FormView 、Repeater、DataList)
- ASP.NET 2.0 的数据绑定控件概述与区别(GridView、DetailsView、FormView 、Repeater、DataList)
- ASP.NET的五大数据控件(GridView,DataList,Repeater ,DetailsView,FormView)浅谈
- 对GridView、DetailsView 和 FormView 控件分页属性的PagerSettings类的设置
- 基于FormView界面的文档工程中改变背景色和控件颜色
- 呈现代码 声明代码和数据绑定的区别
- 数据绑定控件收藏 GridView DataList Repeater FormView DetailsView
- ASP.NET 数据控件:GridView,DataList,Repeater ,DetailsView,FormView。
- 3 推荐数据的呈现
- NSData 和 NSString\ Byte数组 \ UIImage
- HDU 2473 Junk-Mail Filter
- 在VMware 8虚拟机下,ubuntu系统如何安装 vmware tools【使用所有Linux系统安装vmware tools】
- RHEL 6.2 设置yum源
- http://blog.csdn.net/smallwhiteyt/article/details/5793555
- FormView控件的数据呈现和处理
- iPhone开发 判断当前的网络是3g还是wifi
- Java程序员从笨鸟到菜鸟之(三十一)大话设计模式(一)设计模式遵循的七大原则
- ARM指令寻址的几种方式
- EXCEL快捷键大全
- 计算两个时间相距离多少
- RHEL6.2 安装 mplayer
- FormView控件的数据呈现和处理3
- gentoo系统启动初始化