p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   C# Global variable issues. (http://p2p.wrox.com/showthread.php?t=59793)

vijiljones July 5th, 2007 05:28 AM

C# Global variable issues.
 
Hi all,

I wrote code like this

public partial class frmTaskView : System.Web.UI.Page
{
    private int intVar;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          // Call the dataBase sp and assign the variable intVar
          intVar = "10"; // Set through the Databace call
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
       // do search based on the Variable intVar
    }
}


Problem:
        when I click the Search button I am always getting value 0.
If I remove the IsPostBack condition I am getting actual value.This is increasing the Database call.

How I persist the value without the Database call. Please help me.




DZukiewicz July 9th, 2007 04:29 AM

The problem with web pages is that global variables are useless, as the page is regenerated on each request.

The best place to store it if you are returning to the same page is in the ViewState, or the Session if you need to persist it across web pages.

As for the reason it work, I am unsure. Normally, the value would stay as 0 as the compiler default it to 0.

Regards,

Dom

vijiljones July 9th, 2007 04:40 AM

Thanks for the Replay.

But I have Doute, In view State Can i store objects?


vijil jones




planoie July 9th, 2007 07:43 AM

Anything that can be serialized should be able to be stored in viewstate.

If your object is serializable then you should be able to store it.

-Peter

vijiljones July 9th, 2007 08:22 AM

Dear planoie,

 I am very satisfied your quick replay. I achived lot through ur support. Thank you very much.

vijil jones


vijiljones July 9th, 2007 08:26 AM

 Dear planoie and DZukiewicz

 I am very satisfied your quick replay. I achived lot through ur support. Thank you very much.

vijil jones





All times are GMT -4. The time now is 10:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.