ASP.NET+Listview+DropDownList分頁
阿新 • • 發佈:2019-01-02
<div class="gywm_Zi"> <div> <asp:ListView ID="lsvNews" runat="server" > <LayoutTemplate> <div id="itemPlaceholder" runat="server"> </div> </LayoutTemplate> <ItemTemplate> <div class="xwht"> <span class="ht_l">> <a href="NewsDetail.aspx?Id=<%#Eval("Id") %>"> <b><%# Container.DataItemIndex + 1 %></b>. <%#Eval("Title") %> </a> </span> <span class="ht_r">[<%#Eval("createTime") %>]</span> </div> </ItemTemplate> </asp:ListView> </div> </div> <div class="clear"></div> <div class="sxy"> 共有<span id="totalRows" runat="server"></span>條 <span id="totalPage" runat="server"></span> <asp:DataPager runat="server" PagedControlID="lsvNews" PageSize="10" ID="DDPager" > <Fields> <asp:NextPreviousPagerField ShowFirstPageButton="true" FirstPageText="首頁" PreviousPageText="上一頁" ShowNextPageButton="false" /> <asp:NumericPagerField ButtonCount="4" /> <asp:NextPreviousPagerField ShowPreviousPageButton="false" LastPageText="最後一頁" NextPageText="下一頁" ShowNextPageButton="true" ShowLastPageButton="true" /> </Fields> </asp:DataPager> <asp:DropDownList ID="ddlPager" runat="server" AutoPostBack="true" onselectedindexchanged="ddlPager_SelectedIndexChanged"> </asp:DropDownList> </div> </div> </div> </div> <div class="clear"></div>
//後臺程式碼
SqlConnection conn = GetConnection.GetsqlConn(); Comm comm = new Comm(); public string parentTitle = "", twoTitle = "", content = ""; protected void Page_Load(object sender, EventArgs e) { int Id = 0; if (Request.QueryString["Id"] != null) { Id = Convert.ToInt32(Request.QueryString["Id"].ToString()); } string sql1 = "select tb_Content.Id,titleTable.twoTitle,title, [content],tb_Content.remark,isFirst,createTime from tb_Content,titleTable where tb_Content.connectionId=titleTable.Id and connectionId="+Id+" order by createTime desc";//新聞 string connString = ConfigurationManager.AppSettings["ConnectionString"]; SqlConnection connection = new SqlConnection(connString); connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(sql1, connection); DataSet ds = new DataSet(); adapter.Fill(ds); lsvNews.DataSource = ds; connection.Close(); } /// <summary> /// 預備傳送事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_PreRender(object sender, EventArgs e) { lsvNews.DataBind(); //繫結ListView int d = DDPager.TotalRowCount; //DataPager總行數 totalRows.InnerText = d.ToString(); //總行數(總條數) int Var_DataPager = Convert.ToInt32(Math.Ceiling(System.Convert.ToDouble(DDPager.TotalRowCount) / DDPager.MaximumRows));//總頁數 if (Var_DataPager > 1) //如果總頁數大於1 { int pageIndex = DDPager.StartRowIndex / DDPager.PageSize + 1;//當前頁碼 totalPage.InnerText = pageIndex + "/" + Var_DataPager;// 當前頁/總頁數 } else { totalPage.InnerText = Var_DataPager.ToString(); //顯示第一頁 } //設定下拉列表頁碼 ddlPager.Items.Clear(); //每次都要清空原有頁碼 for (int i = 1; i <= Var_DataPager; i++) { ddlPager.Items.Add(new ListItem(i.ToString(), i.ToString())); } ddlPager.SelectedValue = DDPager.StartRowIndex / DDPager.PageSize + 1+""; //分頁下拉列表顯示當前頁碼 }
//分頁下拉列表的選擇事件 protected void ddlPager_SelectedIndexChanged(object sender, EventArgs e) { int pageNo = Convert.ToInt32(ddlPager.SelectedValue.ToString());//拿到下拉列表中選擇的頁碼 int startRowIndex = (pageNo - 1) * DDPager.PageSize; //獲取開始頁碼 DDPager.SetPageProperties(startRowIndex, DDPager.PageSize, true);//呼叫這個方法進行選擇性分頁 }