Variable's value not retained in the method of a class - c#

In ASP>Net using C#, I declared a variable as member of the class as follows:
public class sales: System.Web.UI.Page
{
string type=null;
}
And i have an Image Button in my UI for which I've defined the event called
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
in this method, any value assigned to variable 'type'(by any other method of the class) is not retained and only value null is being retrieved though I've assigned some value to 'type' somewhere inside the class...
What could be the problem that I cant access the assigned value????

You have to persist data between postbacks.
I recommend you to read these articles:
ASP.NET Page Life Cycle Overview
Understanding ASP.NET View State
Nine Options for Managing Persistent User State in Your ASP.NET Application
ASP.NET State Management Recommendations

This is probably a result of the render pipeline. Remember, even though your event is being fired, the page and all variables are recreated with their default value. Create it as a property with at least a backing viewstate storage.
public String Type
{
get { return ViewState["Type"].ToString(); }
set { ViewState["Type"] = value; }
}

Each time you do a request to the page, the variable is re-initialized, if it is a postback or not.
Try to put your variable in a property instead of a field (as you are doing) and initialize the propertie the first time you load the page, only if is not a postback.
You should do something like:
//on the On_Load page's event
if (!IsPostback)
Type = null;
private string Type { get; set; }

If your assigning the value in your object and then doing a postback with the button, the value will be gone unless you stored that instance of the object in the session, viewstate, or something.
string sType = "something";
Session["YourSessionTagHere"] = sType;
Once the postback occurs you can then fetch the object out and access the value from your onclick event.
string sType = (string)(Session["YourSessionTagHere"]);
It just sounds like your not accessing the same object and your object is getting lost because after each postback your string would be new.
You could also make this a bit cleaner by just setting up a property that does the session stuff for you in the get and set.

Related

How to maintain the values in view when i do postback in MVC

I have some controls in my page, when the page load first time the values are getting from database and placed in corresponding controls. When i click the another button again it will go to the controller and get the value from the database and bind the gridview. I have three class in my model, second and third class wrapped in first class. when i bind the second class in gridview, that time first class comes null so all the values are becoming null and bind the gridview only. How to solve this.
Again HTTP is stateless, unless you store your current model in a persistence medium like session, it will get lost in post back!
if I understood your question right !
when you bind your classes for the First Time , put them in a Session var then return it to the view ,
then when you post the second time when you click the other button, make sure to retrieve the session var in the actionmethod and then assign the new values to the class inside this session var, instead of just returning the new ones thinking that old ones are still there.
If I understand what you are asking then you can store it in TempData. TempData will persist until the next request.
public class YourView
{
public ActionResult Index()
{
string firstName = "Stephen";
TempData["FirstName"] = firstName;
return View();
}
public void ButtonClicked()
{
string firstName = (string)TempData["FirstName"];
}
}
Note though that temp data only lasts until the next request. So for this to work, after your view was loaded then the next call would have to be the ButtonClicked call.
The controller is stateless so if you need to persist something longer you have to make it kinda hackish and really ugly like this TempData["FirstName"] = TempData["FirstName"] in every spot that your controller will be called until you need to use that value. Like I said I don't recommend that (or for that case using Session) but if you needed to, then that's the safest way, in my opinion.

How to achieve a dynamic RegularExpressionValidator via the constructor?

I'm building a Custom Control which simply inherits from RegularExpressionValidator. In the constructor I need to be able to see what the viewstate value of the control is so that I can use that value during the construction to determine which validation expression I need it to use.
The problem is the constructor does not have access to viewstate when it is called. At least viewstate is always null due to the timing when the constructor is called.
I have tried overriding the render method, but for some reason, even though the control has access to the viewstate the base.validationexpression always fails due to it not being assigned in the constructor.
What I have is a public property that looks at the viewstate.
public NameType NameValidationType
{
get
{
return this.ViewState["NameType"] == null ? NameType.FirstName : (NameType)ViewState["NameType"];
}
set
{
this.ViewState["NameType"] = value;
}
}
This value is always null when the constructor is called. Is there some way to delay the execution of the constructor to read from viewstate before being constructed?
Thanks for any help.
My solution to get this to work is to build the controls base validationexpression and other attributes in the OnPreRender event. Render was too late and anything before the OnPreRender did not have access to viewstate.

ASP.NET WebForms Custom UserControl - base.SaveViewState() is returning null -- why?

I've got a custom UserControl I'm working with which hosts dynamically created controls and I'm running into an odd situation where I'm seemingly losing ViewState information. In my overridden SaveViewState method on the base.SaveViewState() call, I would expect it to return me back an object containing everything that was in ViewState, but it is instead returning null.
protected override object SaveViewState()
{
object[] tempState = new object[2];
tempState[0] = base.SaveViewState(); //***suspected issue here, tempState[0] == null -- why?
//? this.ViewState["CompanyID"] returns me the guid I'm expecting
//... code instantiating and populating lstDataControlInfos
tempState[1] = lstDataControlInfos;
return tempState;
}
I put a breakpoint in that method, and I can use the immediate window to query ViewState["CompanyID"] and verify that there is indeed a CompanyID stored in ViewState. Maybe I'm misunderstanding something about ViewState, but shouldn't that call be returning what is already in ViewState (including my CompanyID)? The issue becomes a problem on postback in the overridden LoadViewState method, as I no longer have any way to get CompanyID out of ViewState; it comes back null.
protected override void LoadViewState(object savedState)
{
object[] tempState = (object[])savedState;
//tempState[0] == null, as it was stored in the previous SaveViewState call
//any reference to ViewState["CompanyID"] comes back null
base.LoadViewState(tempState[0]);
}
I mentioned I've got dynamic controls I'm serializing to and deserializing from ViewState inside this control, and those all seem to be working properly. What would cause base.SaveViewState() to return null when I know for a fact and can verify (via immediate window) there is data in ViewState during the SaveViewState call?
When you save values to ViewState, the value is marked as dirty if ViewState is being tracked.
Tracking starts in TrackViewState, which is after Initialize and before Load.
Tracking ends in SaveViewState, which is after PreRender.
If ViewState isn't being tracked when a value is set in ViewState, it won't be persisted when you call SaveViewState. This is a good thing: it means you can use ViewState during the Init phase without bloating the serialized ViewState.
When you dynamically add controls to the tree, you should note that:
Values set before you add the control to the control tree will not be persisted, even if you add the control while ViewState is being tracked.
Values set after you add the control to the control tree will be persisted, if you are in a phase where ViewState is being tracked.
At what point in the page lifecycle are you setting ViewState["CompanyID"], and are you doing so before or after you add your control to the control tree?

.net - Accessing a pages global variables from a user control

I'd like to know if it's possible to access a page's global variables from a user control that is on that page.
public partial class index : System.Web.UI.Page
{
FormsIdentity id = (FormsIdentity)User.Identity;
string custID = "some guid";
string accID = "some guid";
protected void Page_Load(object sender, EventArgs e)
{
}
}
This is just an example, my actual global variables are more complex custom configuration objects but it should be the same.
I'd like to access the variables from a user control. this would be helpful since I'd need to use the same objects on both the page and the user control and I don;t really want to have retrieve and store in in memory twice.
Anyone know if/how to do this? Thanks!
Yes, you can. It is called passing the variables to the user control. Sure it is stored in memory more than once, but that is a bit of the "price" of keeping a separation of concerns and making reusable controls.
If the items are constants, you can store them as true globals and access via a static method in global.asax. If on a request basis, this won't work in a simple manner, but you can fashion it.
Now, for your not wanting to store in memory twice comment. I would not worry about this, as it is a small amount of information (1), especially since it will only last for the duration of the request (2). Not completely true, as GC does not fire immediately.
If you are depending on this information over multiple controls, its best to have a UserControlBase that inherits from UserControl and then make your controls inherit from UserControlBase. Do the same with the Page classes. Then you can add the property to your BasePage. To reference in your UserControlBase just do...
protected UserInfo UserInfo
{
get
{
return ((BasePage)this.Page).UserInfo;
}
}

How to access the current [web] page (outside of the current page)?

The situation is this:
I have an abstract class used globally that used to reference a Session variable via a public property. We now want to change its behavior to return a property on a master page.
(By simply changing the guts of this particular property, we hope to avoid doing a lot of rewrites)
This is just a snippet of the class:
public abstract class AppSession
{
public static CaseNumber CurrentCaseNo
{
/* OLD METHOD DELETED */
get
{
if (CurrentPage.Master != null)
// property on the master page
return CurrentPage.Master.CurrentCaseNo;
else
throw new Exception("This page has no master page");
}
}
}
Above, "CurrentPage" is not real/valid. I just wrote that there to show context.
Is this even possible?
Thanks!
J
Look at the HttpContext.Current object. I believe it's Handler property will return the currently executing page. It would be easier to read a value stored in the Session that pulling it out of a property since the Session is available off of HttpContext.Current.
Building on David's answer, this can be used statically throughout your application:
Page myPage = System.Web.HttpContext.Current.CurrentHandler as Page;
if( myPage != null )
return ((MyMaster)myPage.Master).CurrentCaseNo;
I think that you would need to work with something that took a "page" object in as a parameter, and from there, you could determine if the page that was passed is your master page. And do what you need from there....
But that adds quite a bit of overhead.
The real question here is what are you trying to avoid? Trying to get rid of session and move to viewstate?

Categories