302班

java突击队
posts - 151, comments - 74, trackbacks - 0, articles - 14
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Datalist自定义分页【A】

Posted on 2007-07-21 17:31 停留的风 阅读(552) 评论(0)  编辑  收藏 所属分类: .NET技巧特辑
前台代码:
<%@ Page Language="C#" MasterPageFile="~/admin/header_footer.master" AutoEventWireup="true" CodeFile="editPrice.aspx.cs" Inherits="admin_editPrice" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table border="0" cellpadding="0" cellspacing="0" align="center">
        <tr>
            <td style="text-align: center;">
                价格信息</td>
        </tr>
        <tr>
            <td style="text-align: center; height: 139px;">
                <asp:DataList ID="DataList1" runat="server" OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand" OnDeleteCommand="DataList1_DeleteCommand">
                    <ItemTemplate>
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style="width: 100px; height: 16px">
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("type_Id") %>'></asp:Label></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("type") %>'></asp:Label></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("price") %>'></asp:Label></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:Button ID="btnEdit" runat="server" Text="修改" OnClientClick="return confirm('确认要编辑吗?');"  CommandName="Edit" CommandArgument=' <%# Eval( "price") %>   ' /></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:Button ID="Button2" runat="server" Text="删除"   OnClientClick="return confirm('确认要删除吗?');"  CommandArgument='<%#Eval("type_Id") %>' CommandName="Delete" /></td>
                            </tr>
                        </table>
                    </ItemTemplate>
                    <HeaderTemplate>
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style="width: 100px; height: 12px;">
                                    编号</td>
                                <td style="width: 100px; height: 12px;">
                                    类型</td>
                                <td style="width: 100px; height: 12px;">
                                    价格</td>
                                <td style="width: 100px; height: 12px;">
                                    编辑</td>
                                <td style="width: 100px; height: 12px">
                                    删除</td>
                            </tr>
                        </table>
                    </HeaderTemplate>
                    <EditItemTemplate>
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style="width: 100px; height: 16px">
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("type_Id") %>'></asp:Label></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("type") %>'></asp:Label></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:TextBox ID="tbPrice" runat="server" Text='<%# Bind("price") %>' Width="50px"></asp:TextBox></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:Button ID="btnUpdate" runat="server" Text="更新" CommandArgument='<%# Eval("type_Id") %>   ' CommandName="Update" /></td>
                                <td style="width: 100px; height: 16px">
                                    <asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="Cancel" /></td>
                            </tr>
                        </table>
                    </EditItemTemplate>
                </asp:DataList>
                <asp:Label ID="lbCount" runat="server" Text="lbCount"></asp:Label>
                <asp:Label ID="lbTotalPage" runat="server" Text="lbTotalPage"></asp:Label>&nbsp;
                <asp:Label ID="lbCurPage" runat="server" Text="lbCurPage"></asp:Label>&nbsp;<asp:HyperLink
                    ID="hlnkHead" runat="server">首页</asp:HyperLink>
                <asp:HyperLink ID="hlnkPrev" runat="server">上一页</asp:HyperLink>
                <asp:HyperLink ID="hlnkNext" runat="server">下一页</asp:HyperLink>
                <asp:HyperLink ID="hlnkEnd" runat="server">尾页</asp:HyperLink>
                </td>
        </tr>
        <tr>
            <td style="text-align: left">
                在这里你也可以添加相关的类型,并对其进行价格录入。</td>
        </tr>
        <tr>
            <td style="text-align: center">
                类型添加:<asp:TextBox ID="tbType" runat="server"></asp:TextBox>
               &nbsp;单价:<asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
                <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="添加" Width="58px" /></td>
        </tr>
        <tr>
            <td>
            </td>
        </tr>
    </table> 
</asp:Content>  
后台绑定:
  private void dListBindData()
    {
        string strCon = ConfigurationManager.AppSettings["SqlConnectionString"].ToString();
        SqlConnection con = new SqlConnection(strCon);
        SqlDataAdapter da = new SqlDataAdapter("select * from type", con);
        DataSet ds = new DataSet();
        try
        {
            con.Open();
            //填充数据
            da.Fill(ds, "type");
            // 创建分页类
            PagedDataSource objPage = new PagedDataSource();           
            objPage.DataSource = ds.Tables["type"].DefaultView;
            //设置总的查询结果
            lbCount.Text = "总共有" + ds.Tables["type"].Rows.Count.ToString() + "条记录";           
            //设置可以分页以及每页的行数
            objPage.AllowPaging = true;
            objPage.PageSize = 6;
            //总页数
            lbTotalPage.Text = "总页数:" + objPage.PageCount.ToString() + "页";     
            //定义变量来保存当前页
            int CurPage;
            //判断是否具有页面跳转的请求
            if (Request.QueryString["Page"] != null)
            {
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurPage = 1;
            }
            //设置当前页的索引
            objPage.CurrentPageIndex = CurPage - 1;
            lbCurPage.Text = "当前页 : 第" + CurPage.ToString() + "页";
            //如果不是首页
            if (!objPage.IsFirstPage)
            {
                //定义上一页超链接的url为: 当前执行页面的虚拟路径,并传递下一页面的索引值
                hlnkHead.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
                hlnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1); 
            }
            //如果不是最后一页
            if (!objPage.IsLastPage)
            {
                //定义“下一页”超链接url为:当前执行页面的虚拟路径,并传递下一页面的索引值
                hlnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
                hlnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPage.PageCount.ToString();
            }
            DataList1.DataSource = objPage;
            DataList1.DataBind();           
            con.Close();
        }
        catch(Exception error)
        {
            Response.Write(error.ToString());
        }
    }

只有注册用户登录后才能发表评论。


网站导航: