DataList控件分页

 实现了对DataList的分页 HTML控件的值需要转换web应用程序就可以取到了 
ContractedBlock.gif ExpandedBlockStart.gif
  1ExpandedBlockStart.gifContractedBlock.gif /**//// <summary>
  2        /// 当前页数
  3        /// </summary>

  4        int CurrentPage;
  5ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
  6        /// 每页条数
  7        /// </summary>

  8        int PageSize;
  9ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 10        /// 总页数
 11        /// </summary>

 12        int PageCount;
 13ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 14        /// 总条数
 15        /// </summary>

 16        int RecordCount;
 17
 18ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 19        /// 页面首次加载
 20        /// </summary>
 21        /// <param name="sender">发送对象</param>
 22        /// <param name="e">事件参数</param>

 23        protected void Page_Load(object sender, EventArgs e)
 24ExpandedBlockStart.gifContractedBlock.gif        {
 25            PageSize = 2;//每页数据记录
 26
 27            if (!IsPostBack)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 29                CurrentPage = 0;//当前页习惯设为0
 30                ViewState["PageIndex"= 0;//页索引也设为0
 31
 32                //计算总共有多少记录
 33                RecordCount = CalculateRecord();
 34
 35                //计算总共有多少页
 36                if (RecordCount % PageSize == 0)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 38                    PageCount = RecordCount / PageSize;
 39                }

 40                else
 41ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 42                    PageCount = RecordCount / PageSize + 1;
 43                }

 44
 45                this.TotalLbl.Text = PageCount.ToString();//显示总页数
 46                ViewState["PageCount"= PageCount;
 47
 48                this.DataListBind();
 49            }

 50        }

 51
 52
 53ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 54        /// 计算记录数
 55        /// </summary>
 56        /// <returns></returns>

 57        private int CalculateRecord()
 58ExpandedBlockStart.gifContractedBlock.gif        {
 59            try
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 61                int recordCount;
 62                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
 63                con.Open();
 64
 65                string sql = "select count(*) as count from cps_adinfo";
 66                SqlCommand cmd = new SqlCommand(sql, con);
 67                SqlDataReader sdr = cmd.ExecuteReader();
 68
 69                if (sdr.Read())
 70ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 71                    recordCount = Int32.Parse(sdr["count"].ToString());
 72                }

 73                else
 74ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 75                    recordCount = 0;
 76                }

 77
 78                sdr.Close();
 79                con.Close();
 80
 81                return recordCount;
 82            }

 83
 84            catch (Exception ex)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 86                throw new Exception(ex.Message);
 87            }

 88        }

 89
 90
 91ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 92        /// 将数据绑定到Datalist控件
 93        /// </summary>

 94        public void DataListBind()
 95ExpandedBlockStart.gifContractedBlock.gif        {
 96            try
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 98                int StartIndex = CurrentPage * PageSize;//设定导入的起终地址
 99
100                string sql = "select * from cps_adinfo";
101
102                DataSet ds = new DataSet();
103                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString());
104                con.Open();
105
106                SqlDataAdapter sda = new SqlDataAdapter(sql, con);
107                //Fill方法的第一次重载
108                sda.Fill(ds, StartIndex, PageSize, "cps_adinfo");
109
110                this.TextDataList.DataSource = ds.Tables["cps_adinfo"].DefaultView;
111                this.TextDataList.DataBind();
112                this.PreviousLB.Enabled = true;
113                this.NextLB.Enabled = true;
114                if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用
115                if (CurrentPage == 0this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用
116                this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数
117
118            }

119
120            catch (Exception ex)
121ExpandedSubBlockStart.gifContractedSubBlock.gif            {
122                throw new Exception(ex.Message);
123            }

124        }

125
126ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
127        /// 按钮点击事件
128        /// </summary>
129        /// <param name="sender"></param>
130        /// <param name="e"></param>

131        public void LinkButton_Click(Object sender, CommandEventArgs e)
132ExpandedBlockStart.gifContractedBlock.gif        {
133            CurrentPage = Int32.Parse(ViewState["PageIndex"].ToString());//获得当前页索引
134            PageCount = Int32.Parse(ViewState["PageCount"].ToString());//获得总页数
135
136            string cmd = e.CommandName; //判断cmd,以判定翻页方向
137            switch (cmd)
138ExpandedSubBlockStart.gifContractedSubBlock.gif            {
139                case "prev"://上一页
140                    if (CurrentPage > 0) CurrentPage--;
141                    break;
142
143                case "next":
144                    if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页
145                    break;
146
147                case "first"://第一页
148                    CurrentPage = 0;
149                    break;
150
151                case "end"://最后一页
152                    CurrentPage = PageCount - 1;
153                    break;
154
155                case "jump"://跳转到第几页
156                    if (this.TextBox1.Value.Trim() == ""
157                        || Int32.Parse(this.TextBox1.Value.Trim()) > PageCount
158                        || Int32.Parse(this.TextBox1.Value.Trim()) == 0)
159                      
160ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
161                        return;
162                    }

163                    else
164ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
165                        CurrentPage = Int32.Parse(this.TextBox1.Value.ToString()) - 1;
166                        break;
167                    }

168            }

169            ViewState["PageIndex"= CurrentPage;//获得当前页
170            this.DataListBind();
171            
172        }
  

分页跳转时做了判断 不会报异常了
下面是设计页
ContractedBlock.gif ExpandedBlockStart.gif Code
 1  <asp:DataList ID="TextDataList" runat="server" Width="100%" DataKeyField="adid" EnableViewState="False"
 2                    CssClass="pic" Font-Overline="False">
 3                    <ItemTemplate>
 4                        <class="piczt">
 5                            文字链内容:
 6                            <asp:Label ID="lal" runat="server" Text='<%# Eval("adtitle") %>'></asp:Label><br />
 7                        </p>
 8                                               <div class="picco">
 9                            <class="picco1">
10                                 <asp:TextBox ID="txtHtmlCode" TextMode="MultiLine" Rows="3" Columns="40" Text='<%# Eval("htmlcode") %>'
11                                            runat="server" ReadOnly="true" EnableViewState="false"></asp:TextBox>
12                            </p>
13                            <class="picco2">
14                                <asp:TextBox ID="txtForumCode" TextMode="MultiLine" Rows="3" Columns="40" Text='<%# Eval("forumcode") %>'
15                                            runat="server" ReadOnly="true" EnableViewState="false"></asp:TextBox>
16                             </p>
17                        </div>
18                        <div class="picco">
19                            <class="picco1">
20                             <input name="button1" type="button"  class="an2" value="复制广告代码" onclick="setTxt('<%# Eval("htmlcode") %>')"/>
21                                    复制广告代码</button>
22                            </p>
23                            <class="picco2">
24                                <input name="button1" type="button" class="an2" value="复制论坛代码" onclick="setTxt('<%# Eval("forumcode") %>')"/>
25                                    复制论坛代码</button>
26                            </p>
27                        </div>
28                        <class="piczt">
29                            产品名称:鞋 产品ID:123456789</p>
30                        <class="piczt">
31                            提成比例:10%</p>
32                        <class="piczt">
33                            <span class="zi2">注意:</span> 您的联盟 ID, abcdef , 已经包含在代码中</p>
34                    </ItemTemplate>
35                    <ItemStyle Width="100%" />
36                </asp:DataList>
37                <div class="page">
38                    共<asp:Label ID="TotalLbl" runat="server"></asp:Label>页, 当前第<asp:Label ID="CurrentLbl"
39                        runat="server"></asp:Label>
40                    <asp:LinkButton ID="FirstLB" runat="server" OnCommand="LinkButton_Click" CommandName="first"
41                        Font-Overline="False">[ 首页 | </asp:LinkButton>
42                    <asp:LinkButton ID="PreviousLB" runat="server" OnCommand="LinkButton_Click" CommandName="prev"
43                        Font-Overline="False">上一页 | </asp:LinkButton>
44                    <asp:LinkButton ID="NextLB" runat="server" OnCommand="LinkButton_Click" CommandName="next"
45                        Font-Overline="False">下一页 | </asp:LinkButton>
46                    <asp:LinkButton ID="EndLB" runat="server" OnCommand="LinkButton_Click" CommandName="end"
47                        Font-Bold="False" Font-Overline="False">末页 ]</asp:LinkButton>
48                    <asp:LinkButton ID="JumpLB" runat="server" OnCommand="LinkButton_Click" CommandName="jump"
49                        Font-Overline="False">跳到</asp:LinkButton>
50                    <input id="TextBox1" runat="server" type="text" name="TextBox1" style="width: 30px"
51                        onkeyup="value=value.replace(/[^\d]/g,'')" />
52                </div>
53
54
功能上除了分页 还有一个复制文本框的方法
    <script type="text/javascript">
        function setTxt(obj) {
            window.clipboardData.setData('text', obj);
            alert("复制成功!");
        }
    </script>

转载于:https://www.cnblogs.com/uranuschen/archive/2009/07/21/1527833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值