datalist 分行分页显示 带按钮

来源:互联网 发布:阿里云tv助手 编辑:程序博客网 时间:2024/05/26 22:09

<asp:DataList ID="dlNewTitle" runat="server" RepeatColumns="3" Width="100%" RepeatDirection="Horizontal"
                                            OnItemDataBound="dlNewTitle_ItemDataBound1">
                                            <HeaderTemplate>
                                                <table style="border-collapse: separate" border="0" cellspacing="5" cellpadding="0">
                                                    <tr>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <td width="250" style="background-color: #ffffff" class="listItem">
                                                    <a title="单击打开" href="#" target="_blank">
                                                        <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px;
                                                            border-left-width: 0px" title="单击打开" src="admin/servicepic/<%# Eval("service_picpath")%>"
                                                            width="250" height="111"></a>
                                                    <div style="margin-top: 5px; float: left">
                                                        <span id="Span7">
                                                            <%# Eval("service_title")%>
                                                        </span>
                                                    </div>
                                                </td>
                                            </ItemTemplate>
                                            <FooterTemplate>
                                                </tr> </table>
                                                <%--这是脚模板--%>
                                                <table align="center" width="100%" border="0" cellspacing="0" cellpadding="0">
                                                    <tr>
                                                        <td height="32" class="main" style="border-top: gainsboro 1px solid;">
                                                            共<span id="Span11"><asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label></span>页
                                                            当前为第
                                                            <asp:Label ID="lblp" class="currentPageLabel" runat="server" Text="Label"></asp:Label>页
                                                            <asp:HyperLink ID="hlfir" class="pagerButton" runat="server" Text="首页"></asp:HyperLink>
                                                            <asp:HyperLink ID="hlp" class="pagerButton" runat="server" Text="上一页"></asp:HyperLink>
                                                            <asp:HyperLink ID="hln" class="pagerButton" runat="server" Text="下一页"></asp:HyperLink>
                                                            <asp:HyperLink ID="hlla" class="pagerButton" runat="server" Text="尾页"></asp:HyperLink>
                                                            跳至第
                                                            <asp:DropDownList ID="ddlp" class="pagerButton" runat="server" AutoPostBack="true"
                                                                OnSelectedIndexChanged="ddlp_SelectedIndexChanged">
                                                            </asp:DropDownList>页
                                                        </td>
                                                    </tr>
                                                </table>
                                            </FooterTemplate>
                                        </asp:DataList>

 

 

 

    protected void dlNewTitle_ItemDataBound1(object sender, DataListItemEventArgs e)
    {

       

        if (e.Item.ItemType == ListItemType.Footer)
        {
            DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");

            HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
            HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
            HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
            HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");

            pds().CurrentPageIndex = ddlp.SelectedIndex;

            int n = Convert.ToInt32(pds().PageCount);//n为分页数
            int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页

            Label lblpc = (Label)e.Item.FindControl("lblpc");
            lblpc.Text = n.ToString();
            Label lblp = (Label)e.Item.FindControl("lblp");
            lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);

            if (!IsPostBack)
            {
                for (int j = 0; j < n; j++)
                {
                    ddlp.Items.Add(Convert.ToString(j + 1));
                }
            }

            if (i <= 0)
            {
                lpfirst.Enabled = false;
                lpprev.Enabled = false;
                lplast.Enabled = true;
                lpnext.Enabled = true;
            }
            else
            {
                lpprev.NavigateUrl = "?page=" + (i - 1);
            }
            if (i >= n - 1)
            {
                lpfirst.Enabled = true;
                lplast.Enabled = false;
                lpnext.Enabled = false;
                lpprev.Enabled = true;
            }
            else
            {
                lpnext.NavigateUrl = "?page=" + (i + 1);
            }

            lpfirst.NavigateUrl = "?page=0";//向本页传递参数page
            lplast.NavigateUrl = "?page=" + (n - 1);

            ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号
        }
    }
    private PagedDataSource pds()
    {
        string connstring = ConfigurationManager.ConnectionStrings["encsale"].ConnectionString;
        SqlConnection con = new SqlConnection(connstring);

        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter("select * from service", con);
        sda.Fill(ds, "service");
        SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor", con);
        //sda2.Fill(ds, "title");
        //ds.Relations.Add("myrela", ds.Tables["name"].Columns["au_id"], ds.Tables["title"].Columns["au_id"]);

        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables["service"].DefaultView;
        pds.AllowPaging = true;//允许分页
        pds.PageSize = 3;//单页显示项数
        pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
        return pds;
    }


    protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)
    {//脚模板中的下拉列表框更改时激发
        string pg = Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue) - 1));//获取列表框当前选中项
        Response.Redirect("project.aspx?page=" + pg);//页面转向
    }
   

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //if (Request.QueryString["classid"] != null)
            //{

            //    DataBindToDataGirdinfoselect();
            //    Repservicepic.DataSource = pds();
            //    Repservicepic.DataBind();
            //    DataBindToDataGirdclass();
            //    DataBindToDataGirdcontent();
            //}
            //else
            //{
                     dlNewTitle.DataSource = pds();
            dlNewTitle.DataBind();
                      //}
        }
    }

 

原创粉丝点击