ASP网格分页工作不正常 [英] ASP Grid paging not working fine
问题描述
我有一个网格视图,显示数据库中的数据,我上面有一个下拉列表。在页面加载事件中,它显示来自数据库的所有记录,当我从下拉列表中选择年份时,它显示在该年份注册的记录。到目前为止它很好,
在我的下拉列表中我有2012年和2013年,如果我点击2012,网格显示2012年注册的所有记录,网格有3页,当我转到第3页并点击在1页上,网格开始再次显示完整数据,而不是显示2012年的数据,如何解决它
这是代码
I have a grid view that displays data from data base, and I have a dropdown list above it. At the pages load event it shows all records from database and as I select year from dropdown, it shows records that are registered on that year. Upto this it is fine,
In my dropdown I have year 2012 and 2013, If I click 2012, Grid shows all records registered in 2012, grid has 3 pages, when I go to 3rd page and click on 1 page, grid start showing complete data again, instead of showing data from 2012, how to fix it
Here is code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SitefinityWebApp.Modules.Data;
using System.Data.Objects.SqlClient;
using System.Data;
using Telerik.Web.UI;
namespace SitefinityWebApp.Modules.PartnersCard.Admin
{
public partial class CommunitySellersListView : System.Web.UI.UserControl
{
#region constant
TFPData context = new TFPData();
#endregion
protected void Page_Load(object sender, EventArgs e)
{
//By Imad on 03/10/2013--change
if (!IsPostBack)
{
BindControls();
}
}
protected void BindControls()
{
var ddlYearDates = (from ddl in context.CSRegdtls
orderby ddl.CreatedDate
select ddl.CreatedDate.Value.Year).Distinct();
ddlYear.DataSource = ddlYearDates;
ddlYear.SelectedValue = DateTime.Now.Year.ToString();
ddlYear.DataBind();
}
protected void rgrdCSList_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
try
{
int year = Convert.ToInt32(ddlYear.SelectedValue);
rgrdCSList.DataSource = (from cr in context.CSRegdtls
where (cr.IsDeleted == null || cr.IsDeleted == false)
orderby cr.CreatedDate descending
select new
{
ID = cr.CSRegID,
RegDate = cr.CreatedDate,
FirstName = cr.FirstName,
LastName = cr.LastName,
Phone = cr.PhoneNo,
Email = cr.EmailID,
TotalSale = (from p in context.Pcdtls
where (p.IsManualSell == true || p.PaymentStatus == "1") && (p.IsDeleted == null || p.IsDeleted == false)
&& p.SourceID == cr.CSRegID
select p).Sum(s => s.Quantitiy) == null ? 0 :
(from p in context.Pcdtls
where (p.IsManualSell == true || p.PaymentStatus == "1") && (p.IsDeleted == null || p.IsDeleted == false)
&& p.SourceID == cr.CSRegID && p.CreatedDate.Value.Year == year
select p).Sum(s => s.Quantitiy)
});
}
catch (Exception ex)
{
string Error = "Modules/PartnersCard/Admin/CommunitySellersListView.ascx:- rgrdCSList_NeedDataSource()";
Error += ex.Message;
string str = HttpContext.Current.Request.PhysicalApplicationPath + @"LogError\";
LogError.Log_Error(Error, str);
}
}
protected void rgrdCSList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
try
{
if (e.CommandName == "Details")
{
Response.Redirect(string.Concat(Telerik.Sitefinity.Web.SiteMapBase.GetActualCurrentNode().Url + "/community_seller_detail/" + e.CommandArgument.ToString()), false);
}
else if (e.CommandName == "Del")
{
long lCsregID = Convert.ToInt64(e.CommandArgument);
var csdetails = (from c in context.CSRegdtls
where c.CSRegID == lCsregID
select c).FirstOrDefault();
if (csdetails != null)
{
csdetails.IsDeleted = true;
}
context.SaveChanges();
rgrdCSList.Rebind();
}
else if (e.CommandName == "TotalSales")
{
long lCsregID = Convert.ToInt64(e.CommandArgument);
ExportTotalSales(lCsregID);
}
}
catch (Exception ex)
{
string Error = "Modules/PartnersCard/Admin/CommunitySellersListView.ascx:- rgrdCSList_ItemCommand()";
Error += ex.Message;
string str = HttpContext.Current.Request.PhysicalApplicationPath + @"LogError\";
LogError.Log_Error(Error, str);
}
}
protected void btnExport_Click(object sender, EventArgs e)
{
try
{
int year = Convert.ToInt32(ddlYear.SelectedValue);
Uitility objut = new Uitility();
//Assign Web Page
#region Card Price
double cardprice = 0;
cardprice = Convert.ToDouble(objut.GetLatestCardAmount());
#endregion
string strLinks = string.Concat(Request.Url.GetLeftPart(UriPartial.Authority), "/partners-card/cs/cs-detail?csid=");
var cslist = (from cr in context.CSRegdtls
where cr.IsDeleted == null || cr.IsDeleted == false
orderby cr.CreatedDate descending
select new
{
Registration_Date = cr.CreatedDate,
CS_Last_Name = cr.LastName,
/* previous code
CS_First_Name = cr.FirstName,
CS_Address = cr.Address,
CS_Phone = cr.PhoneNo,
CS_Email = cr.EmailID,
Password=cr.Password,
Packets_Requested = cr.NoOfPack,
//Total_Page_Visits = cr.Visitors,
/*Total_Web_Orders = (from p in context.Pcdtls
where (p.IsDeleted == null || p.IsDeleted == false) &&
(p.IsManualSell == false || p.IsManualSell == null) &&
p.SourceID == cr.CSRegID && p.PaymentStatus == "1" && p.CreatedDate.Value.Year == year
select p.Quantitiy).Sum(),*/
Total_Card_Sales_2012 = (from p in context.Pcdtls
where (p.IsDeleted == null || p.IsDeleted == false) &&
p.CreatedDate.Value.Year == 2012 &&
p.SourceID == cr.CSRegID
select p).Count(),
Total_Web_Orders_Cards = "",
Total_Web_Orders_Sales = "",
Total_Cash_Orders = (from p in context.Pcdtls
where (p.IsDeleted == null || p.IsDeleted == false) &&
p.IsManualSell == true && p.Paymentby == "Cash" &&
p.SourceID == cr.CSRegID && p.CreatedDate.Value.Year == year
select p.Quantitiy).Sum(),
Total_CC_Orders = (from p in context.Pcdtls
where (p.IsDeleted == null || p.IsDeleted == false) &&
p.IsManualSell == true && p.Paymentby == "Credit Card" &&
p.SourceID == cr.CSRegID && p.CreatedDate.Value.Year == year
select p.Quantitiy).Sum(),
Total_Check_Orders = (from p in context.Pcdtls
where (p.IsDeleted == null || p.IsDeleted == false) &&
p.IsManualSell == true && p.Paymentby == "Check" &&
p.SourceID == cr.CSRegID && p.CreatedDate.Value.Year == year
select p.Quantitiy).Sum(),
Total_Cards_Sold = "",
Total_Amount = ""
});
DataTable dt = Uitility.LINQToDataTable(cslist);
foreach (DataRow row in dt.Rows)
{
double tweborder = 0;
double tcashorder = 0;
double tccorder = 0;
double tcheckorder = 0;
double totalamount = 0;
//if (row["Total_Web_Orders"].ToString() != "")
//{
// tweborder = Convert.ToDouble(row["Total_Web_Orders"]) * cardprice;
//}
//Below code was commented by Imad on 27/09/2013 because that columnn was not mentioned in excel sheet
//if (row["Total_Web_Orders"].ToString() != "")
//{
// tweborder = Convert.ToDouble(row["Total_Web_Orders"]) * cardprice;
//}
if (row["Total_Cash_Orders"].ToString() != "")
{
tcashorder = Convert.ToDouble(row["Total_Cash_Orders"]) * cardprice;
}
if (row["Total_CC_Orders"].ToString() != "")
{
tccorder = Convert.ToDouble(row["Total_CC_Orders"]) * cardprice;
}
if (row["Total_Check_Orders"].ToString() != "")
{
tcheckorder = Convert.ToDouble(row["Total_Check_Orders"]) * cardprice;
}
totalamount = tweborder + tcashorder + tccorder + tcheckorder;
//row["Total_Web_Orders"] = tweborder;
//Below line was commented by Imad on 27/09/2013 because that columnn was not mentioned in excel sheet
//row["Total_Web_Orders"] = tweborder;
row["Total_Cash_Orders"] = tcashorder;
row["Total_CC_Orders"] = tccorder;
row["Total_Check_Orders"] = tcheckorder;
//row["Total_Amount"] = totalamount;
//Below line was commented by Imad on 27/09/2013 because that columnn was not confirmed in excel sheet
//row["Total_Amount"] = totalamount;
row.AcceptChanges();
}
int n = dt.Columns.Count;
for (int i = 0; i < n; i++)
{
string colname = dt.Columns[i].ColumnName;
if (colname == "Total_Card_Sales_2012")
{
colname = "2012_Total_Cards_Sales";
}
colname = colname.Replace("_", " ");
dt.Columns[i].ColumnName = colname;
dt.AcceptChanges();
}
//change column name Total_Card_Sales_2012 tp 2012 total card sales
GridView gvCsList = new GridView();
gvCsList.DataSource = dt;
gvCsList.DataBind();
objut.ExportToExcel("Community_Seller_List", gvCsList);
dt.Dispose();
}
catch (Exception ex)
{
string Error = "Modules/PartnersCard/Admin/CommunitySellersListView.ascx:- btnExport_Click()";
Error += ex.Message;
string str = HttpContext.Current.Request.PhysicalApplicationPath + @"LogError\";
LogError.Log_Error(Error, str);
}
}
protected void ExportTotalSales(long CSId)
{
Uitility objut = new Uitility();
try
{
var CSDetails = (from c in context.CSRegdtls
where (c.IsDeleted == null || c.IsDeleted == false) && c.CSRegID == CSId
select c).FirstOrDefault();
var TotalWebSales = (from p in context.Pcdtls
where p.SourceID == CSId && (p.IsDeleted == null || p.IsDeleted == false) &&
(p.IsManualSell == false || p.IsManualSell == null) && p.PaymentStatus == "1"
select new
{
CS_First_Name = CSDetails.FirstName,
CS_Last_Name = CSDetails.LastName,
Team = "",
Order_Date = "",
Purchaser_First_Name = p.Bl_FirstName,
Purchaser_Last_Name = p.Bl_LastName,
Order_Type = "Web",
Qty = p.Quantitiy,
Total_Amount = "",
OrderTotal = p.OrderTotal,
Donation = p.DonationAmount,
CreatedDate = p.CreatedDate
}
);
var TotalCashSales = (from p in context.Pcdtls
where p.SourceID == CSId && (p.IsDeleted == null || p.IsDeleted == false) &&
(p.IsManualSell == true)
select new
{
CS_First_Name = CSDetails.FirstName,
CS_Last_Name = CSDetails.LastName,
Team = "",
Order_Date = "",
Purchaser_First_Name = p.Bl_FirstName,
Purchaser_Last_Name = p.Bl_LastName,
Order_Type = p.Paymentby == "Credit Card" ? "CC" : p.Paymentby,
Qty = p.Quantitiy,
Total_Amount = "",
OrderTotal = p.OrderTotal,
Donation = p.DonationAmount,
CreatedDate = p.CreatedDate
}
);
var allSales = TotalWebSales.Union(TotalCashSales).OrderByDescending(p => p.Order_Type);
if (allSales.Count() <= 0)
{
Response.Write("<script>alert('No records to export.');</script>");
return;
}
DataTable dtSalesReport = Uitility.LINQToDataTable(allSales);
foreach (DataRow dr in dtSalesReport.Rows)
{
dr["Order_Date"] = Convert.ToDateTime(dr["CreatedDate"]).ToShortDateString();
if (!dr["Donation"].ToString().IsNullOrEmpty())
{
dr["Total_Amount"] = (Convert.ToDouble(dr["OrderTotal"]) - Convert.ToDouble(dr["Donation"])).ToString();
}
else
{
dr["Total_Amount"] = dr["OrderTotal"].ToString();
}
}
if (dtSalesReport.Rows.Count > 0)
{
dtSalesReport.Columns.Remove("OrderTotal");
dtSalesReport.Columns.Remove("Donation");
dtSalesReport.Columns.Remove("CreatedDate");
dtSalesReport.AcceptChanges();
}
//
int n = dtSalesReport.Columns.Count;
for (int i = 0; i < n; i++)
{
string colname = dtSalesReport.Columns[i].ColumnName;
colname = colname.Replace("_", " ");
dtSalesReport.Columns[i].ColumnName = colname;
dtSalesReport.AcceptChanges();
}
GridView gv = new GridView();
gv.DataSource = dtSalesReport;
gv.DataBind();
objut.ExportToExcel("Community_Seller_Breakdown", gv);
}
catch (Exception ex)
{
string Error = "Modules/PartnersCard/Admin/CommunitySellersListView.ascx:- ExportTotalSales()";
Error += ex.Message;
string str = HttpContext.Current.Request.PhysicalApplicationPath + @"LogError\";
LogError.Log_Error(Error, str);
}
}
protected void rgrdCSList_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
try
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
LinkButton lbtn = (LinkButton)item["TCTotalSales"].FindControl("lbtnTotalSales");
HiddenField hfTotalSale = (HiddenField)item["TCTotalSales"].FindControl("hfTotalSales");
if (hfTotalSale.Value == "0")
{
lbtn.Enabled = false;
}
}
}
catch (Exception ex)
{
string Error = "Modules/PartnersCard/Admin/CommunitySellersListView.ascx:- rgrdCSList_ItemDataBound()";
Error += ex.Message;
string str = HttpContext.Current.Request.PhysicalApplicationPath + @"LogError\";
LogError.Log_Error(Error, str);
}
}
//By Imad on 03/10/2013
protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int year = Convert.ToInt32(ddlYear.SelectedValue);
rgrdCSList.DataSource = (from cr in context.CSRegdtls
where (cr.IsDeleted == null || cr.IsDeleted == false) && cr.CreatedDate.Value.Year==year
orderby cr.CreatedDate descending
select new
{
ID = cr.CSRegID,
RegDate = cr.CreatedDate,
FirstName = cr.FirstName,
LastName = cr.LastName,
Phone = cr.PhoneNo,
Email = cr.EmailID,
TotalSale = (from p in context.Pcdtls
where (p.IsManualSell == true || p.PaymentStatus == "1") && (p.IsDeleted == null || p.IsDeleted == false)
&& p.SourceID == cr.CSRegID
select p).Sum(s => s.Quantitiy) == null ? 0 :
(from p in context.Pcdtls
where (p.IsManualSell == true || p.PaymentStatus == "1") && (p.IsDeleted == null || p.IsDeleted == false)
&& p.SourceID == cr.CSRegID
select p).Sum(s => s.Quantitiy)
});
rgrdCSList.DataBind();
}
catch (Exception ex)
{
string Error = "Modules/PartnersCard/Admin/CommunitySellersListView.ascx:- ddlYear_SelectedIndexChanged()";
Error += ex.Message;
string str = HttpContext.Current.Request.PhysicalApplicationPath + @"LogError\";
LogError.Log_Error(Error, str);
}
}
}
}
推荐答案
I can’t revise the code, but I can give you the hint how to do it.
Do the below steps.
1. While selecting theDropDownList
, inSelectedIndexChanged
Event, store the selected value inSession
something like below...
I can't revise the code, but I can give you the hint how to do it.
Do the below steps.
1. While selecting theDropDownList
, inSelectedIndexChanged
Event, store the selected value inSession
something like below...
Session["year"] = ddlYear.SelectedValue;
2. Now where ever you are populating the Grid (I can’t make out from the pasted code), just try to check the Session
value and populate according to that, something like below...
2. Now where ever you are populating the Grid (I can't make out from the pasted code), just try to check the Session
value and populate according to that, something like below...
if(Session["year"] != null)
{
// Read the Value.
int value = 0;
int.TryParse(Session["year"].ToString(), out value);
}
Now, you can use this value
to load the Grid.
Now, you can use this value
to load the Grid.
这篇关于ASP网格分页工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!