Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

How can I increment the page counter each time when the page gets refreshed.

Here's my code:

C#
protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Uid"].ToString() != null)
        {
            UserId = Convert.ToInt64(Session["Uid"].ToString());
            lblCount.Text = Convert.ToString(Convert.ToInt32(lblCount.Text) + 1);
        }
        if (Session["iDataHUBID"].ToString() != null)
        {
            iDataHUBID = Convert.ToInt64(Session["iDataHUBID"].ToString());
            //lblCount.Text = Convert.ToString(Convert.ToInt32(lblCount.Text) + 1);
        }
            lblCount.Text = Convert.ToString(Convert.ToInt32(lblCount.Text) + 1);
        if (!Page.IsPostBack)
        {
            txtCDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            BindRejectReasonType();
            BindProofTypes();
            ScriptManager1.SetFocus(txtCDate);
            txtBarcode.Attributes["onchange"] = "javascript:return Barcodetest();";
            txtCAFNo.Attributes["onchange"] = "javascript:return CAFtest();";
            btnAccept.Attributes.Add("onclick", "javascript:return ValidateAccept();");
            lblCount.Text = "0";
        }
    }



Regards,

Raj
Posted

If The page is refreshing then it means

Page.IsPostBack condition will be false.

so put you code inside

if(!Page.IsPostBack)
{
int PageRefreshCounter = 0;
Session["Counter"] = PageRefreshCounter;
}
if(Page.IsPostBack)
{

Session["Counter"] = ((int)Session["Counter"])++;
}
 
Share this answer
 
v2
Comments
Raj.rcr 25-Jan-11 8:13am    
Thank You...! I will try as the code u have given..
Check the sample and change your code accordingly
Label1.Text = "1";
        if (Session["counter"] != null)
        {
            Label1.Text = Session["counter"].ToString();
            Label1.Text = (Convert.ToInt16(Label1.Text) + 1).ToString();
        }
        if (!Page.IsPostBack)
        {
            Session["counter"] = Label1.Text;
        }
        Label1.Text = Label1.Text;
 
Share this answer
 
Comments
Raj.rcr 25-Jan-11 8:13am    
Thank You...! I will try, as the code u have given..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900