• asp.net 2.0教程 数据绑定控件


    尊重作者,请保留 www.it55.com 链接字样。

    使用Asp.Net 2.0技术实现数据访问及显示,主要依靠两种类型的服务器控件:数据源控件和数据绑定控件。数据源控件我们前几节已经讲述过了,本节讲述数据绑定控件。
    数据绑定控件包括GridView控件、DetailsView控件、FormView控件。

    1、GridView数据绑定控件
    功能:GridView控件提供数据的显示、排序、添加、修改、删除功能。
    创建:因为GridView控件依赖于数据源控件,所以在创建GridView控件以前必须首先创建一个数据源控件,如SqlDataSource数据源控件。
    首先创建数据源控件SqlDataSource1,方法见http://www.itgao.com/html/2007-05/45444.html。记得一定要设定SqlDataSource1的SelectCommand属性值,以便将数据库中数据查询出来供GridView使用:

     

    然后再设置其UpdateCommand属性值,供GridView更新数据时使用:

    将左侧工具箱中数据选项栏里的“GridView”控件拖到页面中,在GridView任务里选择数据源并选中"启用分页"及"启用编辑"选项。

    这里我们套用一下格式:

    运行一下:

    程序页面代码:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myradioConnectionString %>"
                ProviderName="<%$ ConnectionStrings:myradioConnectionString.ProviderName %>"
                SelectCommand="SELECT [AnnounceID], [UserName], [ip] FROM [Dv_bbs1] ORDER BY [AnnounceID] DESC" UpdateCommand="UPDATE [Dv_bbs1] SET [UserName]=@UserName,[ip]=@ip WHERE [AnnounceID]=@AnnounceID ">
                <UpdateParameters>
                    <asp:Parameter Name="AnnounceID" />
                    <asp:Parameter Name="UserName" />
                    <asp:Parameter Name="ip" />
                </UpdateParameters>
            </asp:SqlDataSource>
       
        </div>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                CellPadding="4" DataKeyNames="AnnounceID" DataSourceID="SqlDataSource1" ForeColor="#333333"
                GridLines="None">
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:BoundField DataField="AnnounceID" HeaderText="AnnounceID" InsertVisible="False"
                        ReadOnly="True" SortExpression="AnnounceID" />
                    <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />#p#分页标题#e#
                    <asp:BoundField DataField="ip" HeaderText="ip" SortExpression="ip" />
                </Columns>
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </form>
    </body>
    </html>

    如果我们还想细化一下,比如说想把某个列改为链接显示或者在每一列添加复选框等,我们都可以通过对Columns节中Field进行更改和设置,比如说改称HyperLinkField或者CheckBoxField等。具体教程你可以查阅 http://www.itgao.com/
    另外,GridView数据绑定控件的属性还有很多,你可以查看其属性面板来慢慢熟悉它们,或进入 http://www.itgao.com/ 进行查阅搜索。当然,它们都非常有用哦。

    2、DetailsView数据绑定控件
    DetailsView数据绑定控件大部分功能和GridView数据绑定控件基本一样,但是他们也有不同的地方:
    DetailsView数据绑定控件注重显示记录细节内容,所以它每次只显示一条;DetailsView数据绑定控件比GridView数据绑定控件多一个添加数据功能。
    创建方法:和GridView一样,首先应创建数据源控件,并添加SelectCommand、UpdateCommand、InsertCommand属性值(供DetailsView数据源绑定控件添加新数据时使用)。
    将左侧工具箱中数据选项栏里的“DetailsView”控件拖到页面中,在DetailsView任务里选择数据源并选中"启用分页"及"启用编辑"等等选项。

    运行:

    程序页面代码:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myradioConnectionString %>"
                ProviderName="<%$ ConnectionStrings:myradioConnectionString.ProviderName %>"
                SelectCommand="SELECT [AnnounceID], [UserName], [ip] FROM [Dv_bbs1] ORDER BY [AnnounceID] DESC" UpdateCommand="UPDATE [Dv_bbs1] SET [UserName]=@UserName,[ip]=@ip WHERE [AnnounceID]=@AnnounceID " InsertCommand="INSERT INTO Dv_bbs1(UserName, ip) VALUES (@UserName,@ip)">
                <UpdateParameters>
                    <asp:Parameter Name="AnnounceID" />
                    <asp:Parameter Name="UserName" />
                    <asp:Parameter Name="ip" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="UserName" />
                    <asp:Parameter Name="ip" />#p#分页标题#e#
                </InsertParameters>
            </asp:SqlDataSource>
       
        </div>
            <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
                BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
                CellPadding="3" DataKeyNames="AnnounceID" DataSourceID="SqlDataSource1" GridLines="Horizontal"
                Height="50px" Width="125px">
                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                <Fields>
                    <asp:BoundField DataField="AnnounceID" HeaderText="AnnounceID" InsertVisible="False"
                        ReadOnly="True" SortExpression="AnnounceID" />
                    <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
                    <asp:BoundField DataField="ip" HeaderText="ip" SortExpression="ip" />
                    <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
                </Fields>
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#F7F7F7" />
            </asp:DetailsView>
        </form>
    </body>
    </html>

    注:像大多数控件一样,你可以使用DetailsView的模版编辑功能来设计自己个性化的DetailsView数据源绑定控件.

    3、FormView数据绑定控件
    FormView数据源绑定控件拥有DetailsView控件一样的功能。但是,FormView数据源绑定控件相对于GridView控件和DetailsView控件将界面及程序的处理权限下放至更底层,你可以利用它的模版对界面的显示方式进行精确的控制,也可以以编程的方式访问FormView对象模型来动态设置其属性、处理事件等。但所有的事物都有两面性,正式因为其灵活,所以使用起来相对比较麻烦。
    创建方法:FormView数据源绑定控件添加方法同上,直接将FormView拖放至页面即可。我们通过FormView任务属性为其选择数据源、启用分页等功能:

    最后我们还可以通过点击“编辑模版”进入模版编辑状态,对数据的显示方式进行细化调整:

    运行:

    页面代码:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myradioConnectionString %>"#p#分页标题#e#
                ProviderName="<%$ ConnectionStrings:myradioConnectionString.ProviderName %>"
                SelectCommand="SELECT [AnnounceID], [UserName], [ip] FROM [Dv_bbs1] ORDER BY [AnnounceID] DESC" UpdateCommand="UPDATE [Dv_bbs1] SET [UserName]=@UserName,[ip]=@ip WHERE [AnnounceID]=@AnnounceID " InsertCommand="INSERT INTO Dv_bbs1(UserName, ip) VALUES (@UserName,@ip)">
                <UpdateParameters>
                    <asp:Parameter Name="AnnounceID" />
                    <asp:Parameter Name="UserName" />
                    <asp:Parameter Name="ip" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="UserName" />
                    <asp:Parameter Name="ip" />
                </InsertParameters>
            </asp:SqlDataSource>
       
        </div>
            <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="AnnounceID"
                DataSourceID="SqlDataSource1">
                <EditItemTemplate>
                    AnnounceID:
                    <asp:Label ID="AnnounceIDLabel1" runat="server" Text='<%# Eval("AnnounceID") %>'>
                    </asp:Label><br />
                    UserName:
                    <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>'>
                    </asp:TextBox><br />
                    ip:
                    <asp:TextBox ID="ipTextBox" runat="server" Text='<%# Bind("ip") %>'>
                    </asp:TextBox><br />
                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                        Text="更新">
                    </asp:LinkButton>
                    <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="取消">
                    </asp:LinkButton>
                </EditItemTemplate>
                <InsertItemTemplate>
                    UserName:
                    <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>'>
                    </asp:TextBox><br />#p#分页标题#e#
                    ip:
                    <asp:TextBox ID="ipTextBox" runat="server" Text='<%# Bind("ip") %>'>
                    </asp:TextBox><br />
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                        Text="插入">
                    </asp:LinkButton>
                    <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="取消">
                    </asp:LinkButton>
                </InsertItemTemplate>
                <ItemTemplate>
                    <strong>AnnounceID:</strong>
                    <asp:Label ID="AnnounceIDLabel" runat="server" Text='<%# Eval("AnnounceID") %>'></asp:Label><br />
                    <strong>UserName: </strong>
                    <asp:Label ID="UserNameLabel" runat="server" Text='<%# Bind("UserName") %>'></asp:Label><br />
                    <strong>ip:</strong>
                    <asp:Label ID="ipLabel" runat="server" Text='<%# Bind("ip") %>'></asp:Label><br />
                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="编辑"></asp:LinkButton>
                    <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                        Text="新建"></asp:LinkButton>
                </ItemTemplate>
            </asp:FormView>
        </form>
    </body>
    </html>

    上面只是几个简单的例子,数据源绑定控件的功能比我们想象的还要强大,要用好它们就需要我们多多学习他们的属性\方法\事件等,多多实践总结.

    接下来我们会接触到的是:数据缓存

  • 相关阅读:
    hnust Snowman
    hnust 可口可乐大促销
    hnust 聚宝盆
    hnust 搬书
    hnust 神奇的序列
    hnust 懒人多动脑
    hnust CZJ-Superman
    集合Set--BST实现
    快速排序
    位运算符
  • 原文地址:https://www.cnblogs.com/bk/p/917061.html
Copyright © 2020-2023  润新知