Important


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DemoBAL;

namespace DemoPresentation
{
public partial class _Default : System.Web.UI.Page
{
#region “Page Events”
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid(“0”);
Session[“dtCategory”] = null;
lblMessage.Text = string.Empty;
}
lblMessage.Text = string.Empty;
}
#endregion

#region “Button Events”
protected void btnPrevious_Click(object sender, EventArgs e)
{
BindGrid(false);
}

protected void btnNext_Click(object sender, EventArgs e)
{
BindGrid(true);
}

protected void btnSave_Click(object sender, EventArgs e)
{
if (Session[“dtCategory”] != null)
{
int count = 0;
DataTable dtCategory = (DataTable)Session[“dtCategory”];
foreach (GridViewRow row in grdProducts.Rows)
{
dtCategory.Rows[count][“CategoryName”] = ((TextBox)row.FindControl(“txtCategoryName”)).Text;
count += 1;
}
Session[“dtCategory”] = null;
count = ProductBAL.Update(dtCategory, Convert.ToInt32(lblCurrentPage.Text) – 1, grdProducts.PageSize);
BindGrid(lblCurrentPage.Text);
lblMessage.Text = “[ ” + count + ” ]Record Update!”;
}
}
#endregion

#region “Gridview Events”
protected void grdProducts_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == “cmdExpand”)
{
int id = Convert.ToInt32(e.CommandArgument);
if (id >= Convert.ToInt32(grdProducts.PageSize))
{
id = id – (Convert.ToInt32(grdProducts.PageSize) * Convert.ToInt32(grdProducts.PageIndex));
}

decimal CategoryId = Convert.ToDecimal(grdProducts.DataKeys[id][“CategoryId”]);
BindSubGrid(((GridView)grdProducts.Rows[id].FindControl(“grdSubProducts”)), CategoryId, id);
}
}

#endregion

#region “Private Methods”
private void BindGrid(string CurrentPage)
{
lblCurrentPage.Text = CurrentPage != “0” ? (Convert.ToInt32(CurrentPage) – 1).ToString() : CurrentPage;
DataTable dtCategory = CategoryBAL.Get(0, Convert.ToInt32(lblCurrentPage.Text), grdProducts.PageSize);
if (dtCategory != null && dtCategory.Rows.Count > 0)
{
grdProducts.DataSource = dtCategory;
grdProducts.DataBind();
int RecordCount = CategoryBAL.Get();
lblPageCount.Text = ” Of ” + ((RecordCount / grdProducts.PageSize) + (RecordCount % 2 == 0 ? 1 : 0)).ToString();
lblCurrentPage.Text = (Convert.ToInt32(lblCurrentPage.Text) + 1).ToString();
Session[“dtCategory”] = dtCategory;
}
else
{
lblPageCount.Text = “0”;
lblCurrentPage.Text = “0”;
}
setButtonVisibility();
}

/// <summary>
/// Bind Page Data as per supplied parameters.
/// </summary>
/// <param name=”IsNext”>Pass true if Next page button and false incase of previous page button click.</param>
private void BindGrid(bool IsNext)
{

DataTable dtCategory = CategoryBAL.Get(0, IsNext == true ? Convert.ToInt32(lblCurrentPage.Text) : Convert.ToInt32(lblCurrentPage.Text) – 2, grdProducts.PageSize);
if (dtCategory != null && dtCategory.Rows.Count > 0)
{
grdProducts.DataSource = dtCategory;
//                grdProducts.PageIndex = (IsNext == true ? grdProducts.PageIndex + 1 : grdProducts.PageIndex – 1);
grdProducts.DataBind();
int RecordCount = CategoryBAL.Get();
lblPageCount.Text = ” Of ” + ((RecordCount / grdProducts.PageSize) + (RecordCount % 2 == 0 ? 1 : 0)).ToString();
lblCurrentPage.Text = IsNext == true ? (Convert.ToInt32(lblCurrentPage.Text) + 1).ToString() : (Convert.ToInt32(lblCurrentPage.Text) – 1).ToString();
Session[“dtCategory”] = dtCategory;
}
else
{
lblPageCount.Text = “0”;
lblCurrentPage.Text = “0”;
}
setButtonVisibility();
}

private void BindSubGrid(GridView grdSubCategory, decimal CategoryId, int rownum)
{
DataTable dtSubCategory = ProductBAL.Get(CategoryId);
if (dtSubCategory != null && dtSubCategory.Rows.Count > 0)
{
grdSubCategory.DataSource = dtSubCategory;
grdSubCategory.DataBind();
((Label)grdProducts.Rows[rownum].FindControl(“lblNodataFound”)).Text = string.Empty;
}
else
{
((Label)grdProducts.Rows[rownum].FindControl(“lblNodataFound”)).Text = “No sub category found!”;
}
}

private void setButtonVisibility()
{
if (Convert.ToInt32(lblCurrentPage.Text) == 1)
{
btnPrevious.Enabled = false;
}
else
{
btnPrevious.Enabled = true;
}

int RecordCount = CategoryBAL.Get();
if (Convert.ToInt32(lblCurrentPage.Text) == (RecordCount / grdProducts.PageSize) + (RecordCount % 2 == 0 ? 1 : 0))
{
btnNext.Enabled = false;
}
else
{
btnNext.Enabled = true;
}
}
#endregion
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: