I need a hand on this basic topic since I’m pretty new to webpages.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UserLogin ObjUser = new UserLogin();
Persona ObjPersona = new Persona();
DataSet ds = new DataSet();
string UserName = null;
UserName = WindowsIdentity.GetCurrent().Name;
UserName = Regex.Replace(UserName, ".*\\\\(.*)", "$1", RegexOptions.None);
ds = ObjUser.GetUserData(UserName);
ObjPersona.UserName = UserName;
ObjPersona.RealName = ds.Tables[0].Rows[0][0].ToString();
ObjPersona.Ranking = ds.Tables[0].Rows[0][1].ToString();
if (((bool)ds.Tables[0].Rows[0]["TNT"] == false)) ObjPersona.TNT = false;
else ObjPersona.TNT = true;
if (((bool)ds.Tables[0].Rows[0]["TLG"] == false)) ObjPersona.TLG = false;
else ObjPersona.TLG = true;
if (((bool)ds.Tables[0].Rows[0]["NEG"] == false)) ObjPersona.Negocios = false;
else ObjPersona.Negocios = true;
if (((bool)ds.Tables[0].Rows[0]["RES"] == false)) ObjPersona.Residenciales = false;
else ObjPersona.Residenciales = true;
if (((bool)ds.Tables[0].Rows[0]["BO"] == false)) ObjPersona.BO = false;
else ObjPersona.BO = true;
if (((bool)ds.Tables[0].Rows[0]["BOA"] == false)) ObjPersona.BOA = false;
else ObjPersona.BOA = true;
ObjUser.CreateRegister(ObjPersona);
}
}
A simple execution once the page is loaded. A Stored procedure fills a DataSet and then I use the dataset to place the data into the Object, in this case ObjPersona.
Now, when I intend to use ObjPersona in another call, let’s say.
protected void BtnClose_Click(object sender, EventArgs e)
{
ObjUser.UpdateRegister(ObjPersona);
LblClose.Text = "Sesión Cerrada";
}
It won’t work since there’s no data in there. (hits error once I want to use data from within the object)
I want to call the procedure for getting the user data just once (this case the page_load), and from there work with it. How can I access data recalled in another control?
Thanks.
A new instance of the asp.net Page object is created for every request. Every time you postback to a page, the
Init,Load, eventhandlers are called. You are not dealing with the same object.If you populated a member variable during the last request, it will not be available this time.
You need to use some means to persists you data across Postbacks.
Asp.net provides two inbuilt methods to do this. One is the
ViewStateand the other is theSession. Session varialbes are stored on the server, andViewStatedata is stored in a hidden input variable in the form.If you have a lot of data in that
DataSet, avoid putting it in theViewState. It would create a large ViewState and make your page loads slow.