Paging.aspx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProjectInspiration.ascx.cs"
Inherits="UserControls_ProjectInspiration" %>
Show projects by product type
Find a project by product:
Dsiplaying <%= GetCurrentPageNumber() %> of <%= GetProjectListCount() %> projects
|
<%# Eval("Name")%>
Products used:
<%# Eval("ProductInfo") %>
<%# GetVariantsByProjectId( int.Parse(Eval("ProjectID").ToString()) )%> with a
<%# Eval("Finish") %>
|
Paging.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS_Library.ProjectManagement;
using CMS_Library.PageManagment;
using System.Data;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using CMS_Library.ProductManagement;
using CMS_Library.ProjectManagement.ProjectProductTypeEntityTableAdapters;
using CMS_Library;
using System.Configuration;
public partial class UserControls_ProjectInspiration : System.Web.UI.UserControl
{
private Project CurrentProject = new Project();
private ProjectImage CurrentProjectImage = new ProjectImage();
private tProjectProductTypeTableAdapter TaProjectProductType = new tProjectProductTypeTableAdapter();
private int projectProductTypeId = 0;
private int pagingSize = CMS_Library.Helpers.ConfigHelper.PagingSize;
private int projectTableCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
BindProductType();
}
}
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
rptPagingTop.ItemCommand += new RepeaterCommandEventHandler(rptPagingTop_ItemCommand);
}
protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandArgument != "")
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
LoadData();
}
}
protected void rptPagingTop_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandArgument != "")
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
LoadData();
}
}
public void rptPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("btnPage");
if (lnkbtnPage.CommandArgument.ToString() == (PageNumber + 1).ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Style.Add("font-size", "12px");
lnkbtnPage.Font.Bold = true;
}
}
}
public void rptPageingTop_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("btnPage");
if (lnkbtnPage.CommandArgument.ToString() == (PageNumber + 1).ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Style.Add("font-size", "12px");
lnkbtnPage.Font.Bold = true;
}
}
}
public string GetProjectImageUrl(int projectId)
{
//Project image
CurrentProjectImage = ProjectImage.GetProjectImagesByProjectId(projectId);
string sProjectImageUrl = "";
if (CurrentProjectImage.ThumbURL != "" && CurrentProjectImage.ThumbURL != null)
{
sProjectImageUrl = CurrentProjectImage.ThumbURL;
}
return sProjectImageUrl;
}
public string GetVariantsByProjectId(int projectId)
{
// Project Products
CurrentProject.GetVariants(projectId);
string sProduct = "";
foreach (Variant var in CurrentProject.Variants)
{
var.GetProduct();
if (var.Product != null)
{
//if (sProduct != string.Empty) { sProduct += " /"; }
sProduct += " GA " + var.Code + "";
}
}
return sProduct;
}
private void LoadData()
{
// Access Base Page Class
PageManager _PageManeger = (PageManager)this.Page;
List
if (projectProductTypeId > 0)
{
projects = Project.GetPageProjects(_PageManeger._Page.PageId, projectProductTypeId, true);
}
else
{
projects = Project.GetPageProjects(_PageManeger._Page.PageId, true);
}
DataTable dt = ToDataTable
projectTableCount = dt.Rows.Count;
PagedDataSource pgItems = new PagedDataSource();
DataView dv = new DataView(dt);
pgItems.DataSource = dv;
pgItems.AllowPaging = true;
//pgItems.PageSize = 5;
pgItems.PageSize = pagingSize;
pgItems.CurrentPageIndex = PageNumber;
if (pgItems.PageCount > 1)
{
rptProjects.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgItems.PageCount; i++)
{
pages.Add((i + 1).ToString());
rptPages.DataSource = pages;
rptPages.DataBind();
// Bind data to top paging.
rptPagingTop.DataSource = pages;
rptPagingTop.DataBind();
}
rptPages.Visible = true;
rptPagingTop.Visible = true;
}
else
{
rptPages.Visible = false;
rptPagingTop.Visible = false;
}
rptProjects.DataSource = pgItems;
rptProjects.DataBind();
}
protected void btnNext_Click(Object sender, EventArgs e)
{
if (PageNumber < rptPages.Items.Count - 1)
{
PageNumber++;
LoadData();
}
}
protected void btnPrevious_Click(Object sender, EventArgs e)
{
if (PageNumber > 0)
{
PageNumber--;
LoadData();
}
}
// Convert generic list to data table.
private static DataTable ToDataTable
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable dt = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
dt.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T _pro in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(_pro);
}
dt.Rows.Add(values);
}
return dt;
}
protected void btnSearch_Click(Object sender, EventArgs e)
{
projectProductTypeId = int.Parse(ddlProductType.SelectedValue);
LoadData();
}
// Bind Project product type to dropdownlist
private void BindProductType()
{
ProjectProductTypeEntity.tProjectProductTypeDataTable dt =
TaProjectProductType.GetData();
if (dt.Rows.Count > 0)
{
ddlProductType.DataSource = dt;
ddlProductType.DataValueField = "ProjectProductTypeId";
ddlProductType.DataTextField = "Name";
ddlProductType.DataBind();
ddlProductType.Items.Insert(0, new ListItem ("Choose product type", "0"));
}
}
// Get current page number
public string GetCurrentPageNumber()
{
string strCurrentPageNumber = "";
if (PageNumber > 0)
{
strCurrentPageNumber = ((PageNumber * pagingSize) + 1) + "-" + ((PageNumber + 1) * pagingSize);
}
else
{
strCurrentPageNumber = (PageNumber + 1) + "-" + ((PageNumber + 1) * pagingSize);
}
return strCurrentPageNumber;
}
public string GetProjectListCount()
{
string strProjectListCount = "";
strProjectListCount = projectTableCount.ToString();
return strProjectListCount;
}
}
No comments:
Post a Comment