DataGrid Paging - an event lingering awaiting postback

S

shefali

Hi,

I have the following code that implements custom pagin in a datagrid. The paging works correctly excpet for one problem. On the last page, where the 'next' page link should be inactive, the link remains active until a postback happens. In other words, I click on 'next', it takes me to the last page, but remains active. I click on it again (or click on any other button on the page), a postback takes place, and then link becomes inactive. The same is true for the Prev page link. Which event is rasied that is awaiting a postback? What is causing this event? How should I fix this?
Heres the code:


***************************************************
using System;
using System.Collections;
using System.Data.Common;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.Odbc;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using PowerUp.Web.UI.WebTree;


namespace croissantcli
{
/// <summary>
/// Summary description for adedit.
/// This page has 1 tree for categories, and
/// 1 data grid per SE.
/// Based on the cat and SE selected, ads are displayed as a list.
///
/// </summary>
public class WebForm1 : System.Web.UI.Page
{

protected System.Web.UI.WebControls.DataGrid adDataGridG;

protected System.Web.UI.WebControls.Image adeditSelectedImage;
protected ClickTracksWebControls.MyWebCustomControls MyWebCustomControls1;

protected string adID;
protected string seID;
public int mode = Global.AD_MODE_NEW;


private void InitializeComponent()
{

}

private void Page_Load(object sender, System.EventArgs e)
{

if (Global.Debug)
Session[Global.USER_ID_STR] = Global.USER_ID;

if (Session[Global.USER_ID_STR] == null)
Response.Redirect("login.aspx");

if (! IsPostBack)
{
// form loaded for the first time

adDataGridG.ShowHeader = false;

// set up pagination params
adDataGridG.VirtualItemCount = GetTotalItemsInQuery(1);
//adDataGridG.CurrentPageIndex = 0;
ViewState["LowID"] = 0;
BindData(adDataGridG, 1, -1, adDataGridG.PageSize, 0, Global.MY_NULL);

} // end if (!IsPostBack)

} // end Page_Load


public void BindData(DataGrid dg, int seID, int adID, int numTopRecs, int startRec, char nextOrPrevPage)
{

try
{
OdbcConnection conn = new OdbcConnection(Global.DBConnectString);
string cmdStr = "";
string cmdStrSel = ""; // select statement
string cmdStrSE = ""; // filter by seid
string cmdStrAd = ""; // filter by adid
string cmdStrAdPage = ""; // filter by adid for paging
string cmdStrOrder = ""; // order by statement
string cmdStrLimit = ""; // limit to x num of recs, same as select top x, ut select tp does not work in mysql.

if (seID == 2)
cmdStrSel = "SELECT adid, seid, adtitleG, adtitleO, addescO, adURL ";
else if (seID == 1)
cmdStrSel = "SELECT adid, seid, adtitleG, addescG1, addescG2, adURL ";
else
return;

if (adID <= 0)
{
cmdStrSE = " FROM ads WHERE seid = '" + seID + "' ";

if (startRec >= 0)
if (nextOrPrevPage == 'p')
cmdStrAdPage = " AND adid < '" + startRec + "' ";
else
cmdStrAdPage = " AND adid > '" + startRec + "' ";
else
cmdStrAdPage = " ";
}
else
{
// no need for seid or catid as adid is primary key
cmdStrSE = " FROM ads ";
cmdStrAd = " where adid = '" + adID + "' ";
} // end if adid <= 0


cmdStrOrder = " order by adid "; //adid needed for paging
if (nextOrPrevPage == 'p')
cmdStrOrder += " desc ";
else
cmdStrOrder += " asc ";

if (numTopRecs > 0)
cmdStrLimit = " LIMIT " + numTopRecs + " ";
else
cmdStrLimit = " ";

cmdStr = cmdStrSel + cmdStrSE + cmdStrAdPage + cmdStrAd + cmdStrOrder + cmdStrLimit;
OdbcCommand cmd = new OdbcCommand(cmdStr, conn);

// need to always 'show' data in datagrid in asc order
OdbcDataAdapter da = new OdbcDataAdapter();
da.SelectCommand = cmd;
conn.Open();
DataSet ds = new DataSet();
int numRows = da.Fill(ds, "ads");

if (numRows > 0)
{
DataView dv = new DataView(ds.Tables["ads"]);
dv.Sort = "adid asc";
dg.DataSource = dv;
dg.DataBind();

ViewState["LowID"] = dv[0]["adid"];
ViewState["HighID"] = dv[dv.Count-1]["adid"];
}
conn.Close();

dg.HorizontalAlign = HorizontalAlign.Center;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}


} // end BindData


public int GetTotalItemsInQuery(int seid)
{
int count = 0;
try
{
OdbcConnection conn = new OdbcConnection(Global.DBConnectString);
string uid = "";
if (Global.Debug)
uid = Global.USER_ID;
else
uid = Session[Global.USER_ID_STR].ToString();
string cmdStr = "SELECT COUNT(*) FROM ads where seid = '" + seid + "' and uid = '" + uid + "'";
OdbcCommand cmd = new OdbcCommand(cmdStr, conn);
conn.Open();
count = Convert.ToInt16(cmd.ExecuteScalar().ToString());
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

return count;
} // end GetTotalItemsInQuery


/// <summary>
/// See if the user is visiting the next page or the previous page.
/// Fetch records accordingly.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ChangePage(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
string catidStr = "";
int catID = -1;

if (catidStr.Length > 0)
catID = Convert.ToInt16(catID);

if (adDataGridG.CurrentPageIndex == e.NewPageIndex-1)
BindData(adDataGridG, 1, -1, adDataGridG.PageSize, Convert.ToInt16(ViewState["HighID"].ToString()), 'n'); // show next page
else
BindData(adDataGridG, 1, -1, adDataGridG.PageSize, Convert.ToInt16(ViewState["LowID"].ToString()), 'p'); // show prev page
adDataGridG.CurrentPageIndex = e.NewPageIndex;
} // end ChangePage


} // end class adedit
} // end namespace croissantcli

*****************************************************

Thanks.
-Shefali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top