【Asp.net之旅】-数据绑定控件之ListView

【Asp.net之旅】--数据绑定控件之ListView
       前几篇博客对数据绑定控件进行了基本的讨论,并添加了一些实例来巩固代码。对于.NET绑定控件,各有其利弊,功能齐全的当属ListView,今天就来讨论下ListView的用法。

      数据绑定控件的文章链接:【Asp.net之旅】--数据绑定控件之Repeater
                                                      【Asp.net之旅】--数据绑定控件之DataList
                                                      【GDI+编程--番外篇(一)】--GridView编程技巧

一、绑定控件之ListView

      

        在往下进行前我们下来讨论下Asp.net的特性,对于Asp.net微软为我们封装了众多的控件,将控件拖拽到页面上就可以使用控件进行编程,而且值得称道的是有些封装良好的控件可以可视化的设置,然后就可以直接开发使用,就正如今天要说的ListView控件。

       ListView控件是功能最强大的数据绑定控件,它能够可视化的开发实现数据的基本操作增删改,另外还支持排序和分页,只不过其分页的效果必须配合DataPager控件。这种分页对于小数据量来说还是很高效的,但对于大数量来说它的效率就很低下了。

       优点:支持增、删、改、排序,继承了分页功能,还支持自定义模板。
       缺点:影响程序性能,大数据量分页效率低下。
【Asp.net之旅】-数据绑定控件之ListView

二、控件使用技巧

       

        ListView是.net封装良好的控件,该控件是从.framework 3.5开始集成的,它的操作能够完全通过设计前台代码的方式来实现,能够通过可视化的设计窗口完成设计,并且在不编写后台代码的基础上完成开发。

       对于下面示例中使用到的数据源我们使用SqlDataSource控件来绑定,在该控件中添加了增删改查语句,并在语句中指定了使用的参数。

 <!--在为sql语句添加参数时和sql语句的参数添加方法相同,另外如果想要获取行的主键需要在控件中绑定DataKeyNames另外在控件中添加SelectedValue属性即可-->
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyBlogConnectionString %>" DeleteCommand="DELETE FROM match WHERE (id=@id)" InsertCommand="INSERT INTO match(name) VALUES (@name)" SelectCommand="SELECT match.* FROM match" UpdateCommand="UPDATE match SET name = @name where id=@id">
            <DeleteParameters>
                <asp:ControlParameter ControlID="ListView1" Name="id" PropertyName="SelectedValue" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="name" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="name" />
                <asp:ControlParameter ControlID="ListView1" Name="id" PropertyName="SelectedValue" />
            </UpdateParameters>
</asp:SqlDataSource>

        Note:想要在ListView中实现增删改的功能,方法有两种,其一可通过在SqlDataSource中编写增删改语句;其二是前篇博客中提供的传统方法,在后台代码中编写控制。下面的代码示例采用的是第一种方法来实现的,这种方法能够不编写后台代码直接实现了增删改,.NET封装了具体的实现过程。

       表格的基本样式,使用CSS样式来控制显示表格的样式。

<style>
        table {
            border:solid 1px #cccccc;
            width:250px;
        }
            table th {
                color: #00FFFF;
                background: #284775;
                font-weight: normal;
                padding: 2px;
            }
            table tr {
                border:solid 1px black;
            }
            td {
                border:groove 1px #ffd800;
            }
        
</style>

  1、编辑

        在编辑时和DataList控件相同是在EditItemplate模板中定义需要的控件,当单击编辑按钮后将会跳转进入编辑界面。需要说明的是LayoutTemplate模板,在下面的示例中都使用了该模板,它里面的内容应该存放用户自定义的内容,即控件封装的功能以外的其它显示内容。

<asp:ListView ID="ListView1" runat="server" DataMember="DefaultView" DataSourceID="SqlDataSource1">
            <AlternatingItemTemplate>
                <tr style="background-color:#cccccc;">
                    <td>
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                    </td>
                    <td>
                        <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                    </td>
                    <td>
                        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                    </td>
                    <td>
                        <asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
        
            <ItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                    </td>
                    <td>
                        <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                    </td>
                    <td>
                        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server" cellpadding="0" cellspacing="0">
                    <tr runat="server">
                        <td runat="server">
                            <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr runat="server" style="">
                                    <th runat="server"></th>
                                    <th runat="server">id</th>
                                    <th runat="server">name</th>
                                </tr>
                                <tr id="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="">
                        </td>
                    </tr>
                </table>

            </LayoutTemplate>
</asp:ListView>

          通过上面的前台代码在不编写后台代码的情况下能够轻松实现编辑的功能,开发简单。代码的效果图如下:

【Asp.net之旅】-数据绑定控件之ListView【Asp.net之旅】-数据绑定控件之ListView

   2、删除和插入

        删除的实现时是在Itemplate模板中添加的删除命令来实现的,需要我们体现设计ListView控件的DatakeyNames属性指定控件的主键值,这样在SqlDataSource中使用的属性@id才能生效。

<asp:ListView ID="ListView1" runat="server" DataMember="DefaultView" DataSourceID="SqlDataSource1" DataKeyNames="id" InsertItemPosition="LastItem">
    <AlternatingItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
            </td>
            <td>
                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
            </td>
            <td>
                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
            </td>
            <td> </td>
            <td>
                <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
            </td>
        </tr>
    </InsertItemTemplate>
    <ItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
            </td>
            <td>
                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
            </td>
            <td>
                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                        <tr runat="server" style="">
                            <th runat="server"></th>
                            <th runat="server">id</th>
                            <th runat="server">name</th>
                        </tr>
                        <tr runat="server" id="itemPlaceholder">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr runat="server">
                <td runat="server" style="">
                </td>
            </tr>
        </table>
    </LayoutTemplate>
</asp:ListView>

       示例图:

【Asp.net之旅】-数据绑定控件之ListView

  3、分页和排序

        分页是在LayoutTemplate模板中添加的DataPager控件来实现的,所以可以通过修改DataPager的属性值来指定显示的导航的样式。
        DataPager的Fields字项里来添加分页的标签,其中NextPreviousPagerField 控制按钮的导航,通过设置它的属性来显示第一页、上一页等的导航按钮,另外可通过设置NumericPagerField 来指定页数导航。

<LayoutTemplate>
    <table runat="server">
        <tr runat="server">
            <td runat="server">
                <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                    <tr runat="server" style="">
                        <th runat="server">id</th>
                        <th runat="server">name</th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder">
                    </tr>
                </table>
            </td>
        </tr>
        <tr runat="server">
            <td runat="server" style="background-color:#284775;">
                <asp:DataPager ID="DataPager1" runat="server">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" 
                            ShowFirstPageButton="True" 
                            ShowLastPageButton="True" />
                        <asp:NumericPagerField ButtonType="Button" ButtonCount="3" />
                    </Fields>
                </asp:DataPager>
            </td>
        </tr>
    </table>
     <!--排序事件,必须放在LayoutTemplate模板中,并且CommandName值为Sort,CommandArgument值为需要排序的数据库字段名称-->
     <asp:Button Text="按id排序" runat="server" CommandName="Sort" CommandArgument="id" />
</LayoutTemplate>

        另外排序的实现是通过在LayoutTemplate模板中添加asp.net控件来实现的,设置控件CommandName="Sort",并将CommandArgument赋值为需要排序的数据库字段名称,如下想要对id排序,则CommandArgument="id"。

【Asp.net之旅】-数据绑定控件之ListView


4、分组

         分组也是ListView控件的一大特色,把数据当做一个个的子集显示到控件上,就好像是分块显示一样,把一部分数据分到一个块中,另一部分分到另一个块中,可通过设置该控件的GroupItemCount来控制数据所分的组数。

【Asp.net之旅】-数据绑定控件之ListView
     另外在分组时需要在GroupTemplate模板中添加一个ID名称为itemPlaceholderPlaceHolder控件,然后在LayoutTemplate模板中添加一个ID名称为groupPlaceholderPlaceHolder控件,这样就能够简单的达到对数据分组的目的。

<asp:ListView ID="ListView1"  GroupItemCount="5" runat="server" DataSourceID="SqlDataSource1">
    <GroupTemplate>
        <tr>
            <asp:PlaceHolder runat="server" ID="itemPlaceholder" />                    
        </tr>
    </GroupTemplate>
    <GroupSeparatorTemplate>
        <tr id="Tr1" runat="server">
            <td colspan="5"><hr /></td>
        </tr>
    </GroupSeparatorTemplate>
    <LayoutTemplate>
        <table>
            <asp:PlaceHolder ID="groupPlaceholder" runat="server" />
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <td style="background-color:#ffd800;">
            id:<asp:Label ID="id" runat="server" Text='<%#Eval("id") %>'></asp:Label><br />
            name:<asp:Label ID="Label1" runat="server" Text='<%#Eval("name") %>'></asp:Label><br />
        </td>
               
    </ItemTemplate>
</asp:ListView>

     分组后的示例图:

【Asp.net之旅】-数据绑定控件之ListView


三、对比升华


        有关数据绑定的控件已经讨论完成,让我们最后回过头来继续讨论下几种经常使用的数据绑定控件。

【Asp.net之旅】-数据绑定控件之ListView
         对于数据绑定控件来说Repeater控件是最基础的了,它因为最原始所以受到广大开发人员的喜爱,对有经验程序员来说在开发时往往采用Repeater,因为它使用灵活,稳定,不会产生恶意代码,并且效率高。
         对比几种控件这就不得不说说另外.net平台的厉害之处,针对不同开发程度的人员封装了不同的开发控件,对于菜鸟级别的开发人员来说GridView和ListView应该是他们的首选,因为操作简单,只需要点几个按钮选几个选项就能实现强大的功能。另外对于老程序员来说习惯了编写代码,不敲代码手痒这时候首选当然是Repeater。不多对于学习人员来说还是推荐使用Repeater控件,因为它的功能少,而且灵活,得到学习的机会更多。

       代码示例下载地址:ListViewDemo。


结语


       控件的基本使用已经讨论完成,对于每一个控件的讨论都经历了两个步骤,分别是基础和应用,其中应用部分是重点,通过应用把控件熟练掌握。这正是学习--思考--实践--总结很好的映射,另外提倡多去查阅MSDN,里面有详细的使用方法。








4楼mail_li46分钟前
感谢分享
Re: zhang_xinxiu21分钟前
客气……
3楼lfmilaoshi1小时前
对于控件绑定数据来说,一定要考虑的一个因素,就是灵活性。
2楼lantingxv_jing1小时前
数据绑定控件
Re: zhang_xinxiu1小时前
恩,很重要的一课……
1楼tang_huan_1111小时前
MSDN,站在巨人肩膀上