FormView分页显示数据的例子

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FormView控件.aspx.cs" Inherits="WebApplication1.FormView控件" %>

<!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:FormView ID="FormView1" runat="server" AllowPaging="true" 
            onpageindexchanging="FormView1_PageIndexChanging">
       <HeaderTemplate>
       <table border="1" cellpadding="0" cellspacing="0" >
        <tr><td>编号</td><td>真实姓名</td><td>年龄</td><td>性别</td><td>手机</td><td>电话</td><td>电子邮件</td></tr>
       </HeaderTemplate>
       <ItemTemplate>
       <tr><td><%#Eval("ID")%></td><td><%#Eval("RealName")%></td><td><%#Eval("Age")%></td>
       <td><%#bool.Parse(Eval("Sex").ToString())?"":"<font color='red'>女</font>"%></td>
       <td><%#Eval("Mobile") %></td> <td><%#Eval("Phone") %></td> <td><%#Eval("Email") %></td></tr>
       </ItemTemplate>
       <FooterTemplate>
        </table>
     </FooterTemplate>
      </asp:FormView>
    </div>
    </form>
</body>
</html>

CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class FormView控件 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData(0);
            }
        }
        private void BindData(int pageIndex)
        {
             SqlConnection conn = new SqlConnection(@"server=Rose-PC\SQLEXPRESS;Database=User;User Id=sa;password=");
                SqlCommand command = new SqlCommand("Select top 5* from UserInfo order by ID desc", conn);
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable data = new DataTable();
                adapter.Fill(data);

               FormView1.DataSource = data;
               FormView1.DataBind();
          }

        protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {
            BindData(e.NewPageIndex);
        }

      


       
    }
}

 

posted @ 2013-10-08 20:31  笨笨丫头~双  阅读(239)  评论(0编辑  收藏  举报