ASP.NET Repeater控件使用方法
HTML前台模版:
<asp:Repeater ID="lstProduct" runat="server"> <HeaderTemplate> <table class="lstTable"> <tr class="FormHeader" style="font-weight: bold;"> <td> 产品货号 </td> <td> 产品名称 </td> <td> 产品现价 </td> <td> 产品原价 </td> <td> 产品包装量 </td> <td> 编辑 </td> </tr> </HeaderTemplate> <ItemTemplate> <tr style="background-color: #EFF3FB; border-color: #E0E0E0; height: 30px;"> <td style="white-space: nowrap;"> <%#Eval("ProductCode")%> </td> <td style="white-space: nowrap;"> <%#Eval("ProductName")%> </td> <td> <%#Eval("CurrentPrice")%> </td> <td> <%#Eval("OriginalPrice")%> </td> <td style="white-space: nowrap;"> <%#Eval("PackingUnit")%> </td> <td> <asp:HyperLink ID="hylOperate" runat="server" NavigateUrl='<%# "UpdateProductList.aspx?ProductID="+Eval("ProductID")%>'>编辑</asp:HyperLink></td></tr></ItemTemplate><AlternatingItemTemplate> <tr style="background-color: White; border-color: #E0E0E0; height: 30px;"> <td style="white-space: nowrap;"> <%#Eval("ProductCode")%> </td> <td style="white-space: nowrap;"> <%#Eval("ProductName")%> </td> <td> <%#Eval("CurrentPrice")%> </td> <td> <%#Eval("OriginalPrice")%> </td> <td style="white-space: nowrap;"> <%#Eval("PackingUnit")%> </td> <td> <asp:HyperLink ID="hylOperate" runat="server" NavigateUrl='<%# "UpdateProductList.aspx?ProductID="+Eval("ProductID")%>'>编辑</asp:HyperLink></td></tr></AlternatingItemTemplate><FooterTemplate> </table> </FooterTemplate> </asp:Repeater>
<webdiyer:AspNetPager runat="server" ID="pagerOrderList" FirstPageText="首页" CssClass="lstPager"
LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" PageSize="25" CurrentPageButtonStyle="color:white;"
OnPageChanged="pagerOrderList_PageChanged" SubmitButtonText="Go" UrlPaging="True">
</webdiyer:AspNetPager>
后台代码:
数据库操作类 /// <summary> /// 检索各品牌下的产品信息 /// </summary> /// <param name="ProductCode"></param> /// <param name="ProductName"></param> /// <param name="CompanyID"></param> /// <param name="UserName"></param> /// <returns></returns> public DataTable GetSearchProduct(string ProductCode, string ProductName, string CompanyID, string UserName, int pageIndex, int pageSize) { int firstPage = pageIndex * pageSize; //当前页码X每页记录条数 DataTable dt = new DataTable(); DataSet ds = new DataSet(); string strSql = "SELECT ProductID,ProductCode,ProductName,CurrentPrice,OriginalPrice,CompanyID,PackingUnit,OrderNum," + "Specification,PromotionalPrice,PromotionalStart,PromotionalEnd FROM tblProduct WHERE " + "CompanyId IN(SELECT Company_Id FROM tblSupplierCompany WHERE Supplier_Code in(SELECT SupplierCode FROM tblSupplier" + " WHERE Supplier_UserName=@UserName))"; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (ProductCode != "") { strSql += " AND ProductCode like @ProductCode "; cmd.Parameters.AddWithValue("@ProductCode", "%" + ProductCode + "%"); } if (ProductName != "") { strSql += " AND ProductName LIKE @ProductName"; cmd.Parameters.AddWithValue("@ProductName", "%" + ProductName + "%"); } if (CompanyID != "" && CompanyID != "-1") { strSql += " AND CompanyID =@CompanyID"; cmd.Parameters.AddWithValue("@CompanyID", CompanyID); } cmd.Parameters.AddWithValue("@UserName", UserName); cmd.CommandText = strSql; conn.Open(); SqlDataAdapter sdp = new SqlDataAdapter(cmd); sdp.Fill(ds, firstPage, pageSize, "tblProduct"); dt = ds.Tables[0]; conn.Close(); return dt; } /// <summary> /// 获取总记录数 /// </summary> /// <returns></returns> public int GetAllCount(string ProductCode, string ProductName, string CompanyID, string UserName) { string strSql = "SELECT COUNT(ProductID) FROM tblProduct WHERE " + "CompanyId IN(SELECT Company_Id FROM tblSupplierCompany WHERE Supplier_Code in(SELECT SupplierCode FROM tblSupplier" + " WHERE Supplier_UserName=@UserName))"; SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; if (ProductCode != "") { strSql += " AND ProductCode like @ProductCode "; cmd.Parameters.AddWithValue("@ProductCode","%"+ ProductCode+"%" ); } if (ProductName != "") { strSql += " AND ProductName LIKE @ProductName"; cmd.Parameters.AddWithValue("@ProductName", "%" + ProductName+"%"); } if (CompanyID != "" && CompanyID != "-1") { strSql += " AND CompanyID =@CompanyID"; cmd.Parameters.AddWithValue("@CompanyID",CompanyID); } cmd.Parameters.AddWithValue("@UserName", UserName); cmd.CommandText = strSql; conn.Open(); int AllCount = Int32.Parse(cmd.ExecuteScalar().ToString()); conn.Close(); return AllCount; } /// <summary> /// 数据绑定 分页显示 /// </summary> private void GetProduct() { this.pagerOrderList.RecordCount = new SupplierManagementDA().GetAllCount(txtProductCode.Text.Trim(), txtProductName.Text, ddlCompany.SelectedValue, Session["LoginName"].ToString()); int PageIndex = this.pagerOrderList.CurrentPageIndex - 1; int PageSize = this.pagerOrderList.PageSize = 25; lstProduct.DataSource = new SupplierManagementDA().GetSearchProduct(txtProductCode.Text.Trim(), txtProductName.Text, ddlCompany.SelectedValue, Session["LoginName"].ToString(), PageIndex, PageSize); lstProduct.DataBind(); }