ASP.NET3.5中如何使用ListView控件(二)

原创|其它|编辑:郝浩|2009-06-11 11:33:20.000|阅读 1649 次

概述:ASP.NET的新ListView控件为显示和CRUD数据库操作提供了基于模板的布局,使其成为构建以数据为中心的Web应用程序的方法。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  在Listing 1中,通过设置ConnectionString 和SelectCommand 属性的适当值,SqlDataSource控件检索来自AdventureWorks数据库的Product表格的数据。ConnectionString属性通过一个ASP.NET表达式从web.config中获得连接字串。在我测试机器上,连接字串在web.config中定义如下:

  < connectionStrings>

  < add name="AdventureWorks"

  connectionString="server=localhost;uid=sa;

  pwd=thiru;database=AdventureWorks;"/>

  < /connectionStrings>
 
  当设置SqlDataSource属性之后,下一步通过ListView控件显示那个数据。这里的标记包括在LayoutTemplate中:

  < LayoutTemplate>

  < table cellpadding="2" runat="server" id="tblProducts"

  style="width:460px">

  < tr runat="server" id="itemPlaceholder">

  < /tr>

  < /table>

  < asp:DataPager runat="server" ID="DataPager" PageSize="3">

  < Fields>

  < asp:NumericPagerField ButtonCount="10"

  PreviousPageText="< --" NextPageText="-->" />

  < /Fields>

  < /asp:DataPager>

  < /LayoutTemplate>
 
  LayoutTemplate为从ListView中产生的输出定义一个容器。除了定义ListView控件的整体布局的table控制,LayoutTemplate也定义了< asp:DataPager>控件,为ListView控件提供了页面功能。DataPager控件标页数据并为任何实施IPageableItemContainer接口(ListView控制的)的数据绑定控件显示导航控件。

  这有两个将DataPager控件和一个数据绑定控件结合在一起的方法:

  将DataPager控件的PagedControlID属性设置到数据绑定控件的名字。

  在数据绑定控件等级里面放入DataPager控件。对于Listview控件,你可以将DataPager控件放入LayoutTemplate元素中。

  设置DataPager的PageSize属性来控制项为每个数据页面显示的项目数量。通过设置QueryStringField属性,你也可以改变网页被提交到服务器的方法。

  

  在DataPager控制中,你指定NumericPageField模板,它确保用户通过选择页数来导航特定的页面。

  < asp:NumericPagerField ButtonCount="10"

  PreviousPageText="< --"

  NextPageText="-->" />
 
  用Eval()语句,通过检索来自数据源控件的数据,ItemTemplate元素为每个记录的细节提供标记。

  图1 显示的是在浏览器中导航页面所产生的输出。

  用ListView控件编辑数据

  如你所见,通过一个ListView控件显示数据是相对直接的,但是你也可以让用户在一个ListView中编辑数据。添加一个新的叫做ListViewEditExample.aspx 的ASP.NET页面到一个简单的网站中并修改它的代码如Listing 2所示:

  HTML (ASP.NET)
  Listing 2. Editable ListView:

  This page sets up an editable ListView, where users can update data from the AdventureWorks database.

  < %@ Page Language="C#" %>

  < script runat="server">

  void deptsView_ItemUpdated(object sender,

  ListViewUpdatedEventArgs e)

  {

  lblResult.Text = e.AffectedRows.ToString() +

  " row(s) successfully updated";

  }

  void deptsView_PagePropertiesChanged(object sender, EventArgs e)

  {

  //Set the text to empty when navigating to a different page

  lblResult.Text = "";

  }

  < /script>

  < html xmlns="http://www.w3.org/1999/xhtml">

  < head runat="server">

  < link rel="Stylesheet" type="text/css" href="StyleSheet.css" />

  < title>Editing Data using ListView Control< /title>

  < /head>

  < body>

  < form id="form1" runat="server">

  < div>

  < asp:ListView ID="ContactsListView" DataSourceID="deptSource"

  DataKeyNames="DepartmentID" runat="server"

  OnItemUpdated="deptsView_ItemUpdated"

  OnPagePropertiesChanged="deptsView_PagePropertiesChanged">

  < LayoutTemplate>

  < table cellpadding="2" width="640px" border="1"

  runat="server" id="tblProducts">

  < tr id="row1" runat="server" class="header">

  < th id="header2" runat="server">Name< /th>

  < th id="header3" runat="server">Group Name< /th>

  < th id="header1" runat="server">Action< /th>

  < /tr>

  < tr runat="server" id="itemPlaceholder" />

  < /table>

  < asp:DataPager runat="server" ID="deptsDataPager"

  PageSize="3">

  < Fields>

  < asp:NextPreviousPagerField ShowFirstPageButton="True"

  ShowLastPageButton="True" FirstPageText="|< < "

  LastPageText=" >>|" NextPageText=" > "

  PreviousPageText=" < " />

  < /Fields>

  < /asp:DataPager>

  < /LayoutTemplate>

  < ItemTemplate>

  < tr id="row2" runat="server">

  < td>

  < asp:Label ID="lblName" runat="Server"

  Text='< %#Eval("Name") %>' />

  < /td>

  < td valign="top">

  < asp:Label ID="lblGroupName" runat="Server"

  Text='< %#Eval("GroupName") %>' />

  < /td>

  < td>

  < asp:LinkButton ID="btnEdit" runat="Server" Text="Edit"

  CommandName="Edit" />

  < /td>

  < /tr>

  < /ItemTemplate>

  < EditItemTemplate>

  < tr style="background-color: #ADD8E6">

  < td>

  < asp:TextBox ID="txtName" runat="server"

  Text='< %# Bind("Name") %>'

  MaxLength="50" />< br />

  < /td>

  < td>

  < asp:TextBox ID="txtGroupName" runat="server" Text='< %#

  Bind("GroupName") %>' MaxLength="50" />< br />

  < /td>

  < td>

  < asp:LinkButton ID="btnUpdate" runat="server"

  CommandName="Update" Text="Update" />?

  < asp:LinkButton ID="btnCancel" runat="server"

  CommandName="Cancel" Text="Cancel" />

  < /td>

  < /tr>

  < /EditItemTemplate>

  < /asp:ListView>

  < asp:SqlDataSource ID="deptSource" runat="server"

  ConnectionString="< %$ ConnectionStrings:AdventureWorks %>"

  SelectCommand="SELECT [DepartmentID],[Name],[GroupName] FROM

  HumanResources.Department" UpdateCommand="UPDATE

  HumanResources.Department SET Name = @Name,

  GroupName = @GroupName WHERE DepartmentID = @DepartmentID">

  < /asp:SqlDataSource>

  < br />< br />

  < asp:Label runat="server" ID="lblResult" Text=""

  Font-Bold="true" />

  < /div>

  < /form>

  < /body>

  < /html>


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:IT专家网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP