If I've got a setup where my master page has a member which is an EF generated object, and I want to use this member then within the page itself (but have need for it within the master page as well).
Currently I've got a using loop within the Master Page Page_Init event to establish the member. However if then I try to get any properties of this within the Page I get an error about "The ObjectContext instance has been disposed...", which is fair enough (It is within it's own using loop, however that's a different OC instance, so it's throwing this error.)
What's the best/preferred way around this situation? Is it to open the Object Context within the MasterPage pre_init event, and then dispose of it during the Page_Dispose event of the Master Page, or use a second OC Instance in the Page, and just pull a local version of the EF object by checking it's ID against the Master Page's object ID?
Thanks, Psy
Can't you create a POCO when you load the master page and make it a protected property which your derived pages can get access to. Results in 1 trip to the database.
It's best practice not to use the Object Context in your UI layer to begin with. You should a business/data access layer to broker the communication with the database.
Related
I have a single-page site that has an UpdatePanel. Within that UpdatePanel, there are UserControls that are dynamically loaded.
All linking between 'pages' (which are just UserControls) is done by using a WebMethod that changes a Session variable that stores the UserControl to load. The page reloads, with a new UserControl, and everything works great!
The downside to this methodology, however, is that I'm aware that Session variables don't scale well. Too many of them kicking around is not a good thing, so I've heard. I've unsuccessfully attempted to use different methods but not have been able to succeed. I'm looking to set the UserControl to load very early in the Page Lifecycle.
I've tried HttpContext.Current.Items, UserControl public properties and even UserControl HTML injection. It's just a big mess.
Is there a best practice for this type of scenario? Any helpful links or suggestions?
All is much appreciated.
Clarity update
I'm looking to change the UserControl to be loaded by the C# code-behind file through either jQuery method calls or a Webmethod. Session variables work, but don't scale.
I would use localStorage, you can store lots of information like this
localStorage.setItem('var', 'data');
and get the data back like this
var data = localStorage.getItem('var');
Exactly what happens in TrackViewState Page Event Method in ASP.NET. How it differs from LoadViewState.
Thanks,
Pravin
The ViewState property is of type System.Web.UI.StateBag. The StateBag class provides a means to store name and value pairs, using a System.Collections.Specialized.HybridDictionary behind the scenes.
The reason the StateBag has the TrackViewState() method is to keep the view state as trimmed down as possible. Again, we don't want to store the initial property values in the view state, as they don't need to be persisted across postbacks. Therefore, the TrackViewState() method allows the state management to begin after the instantiation and initialization stages.
for more info follow the subtittle
Timing the Tracking of View State in this link
I have a page called test.aspx with test.cs.
However, i want to access my control called mbResult
Which is my custom messagebox control, from a sepearate CS page.
I know many people have asked this question and i have found that this is a method to access my controls.
MessageBoxControl mbox1 = this.FindControl("mbResult") as MessageBoxControl;
But I keep getting this error
Error 5 Keyword 'this' is not valid in a static property, static method, or static field initializer
Any ideas on how to access this control all i am trying to do is make it visible.
Thanks
You need to move the code into a non-shared method. You need to be operating in an instance of the page.
Update for clarification in comments
Unfortunately, your application is going to need some restructuring.
If the messageboxcontrol is shown in a new window, then you will need to pass the value from your source page to the new window in the query string.
However, if you want the messagebox control to be displayed on the source page, then you will need to convert it from a page to a UserControl, add a reference to the user control to your source page, and then add an instance of the usercontrol directly to the source page.
Statics don't have instance-based contexts, so using this is not applicable. You'll need a reference to the control for which you want to use .FindControl (possibly by passing it as a parameter).
In my asp .net C# project I have a page defualt.aspx on which I have placed 2 components.
So in all I have:
1) default.aspx (main page, not doing much code in it)
2) wuc_Lookup.ascx (doing a lot here, grabbing data, setting session, etc)
3) wuc_PageMessages.ascx (has a couple of panels and labels for message output
)
The intent is to use 3) in any page in my application. 1) and 2) are already working. My issue is that the Page_Load sequence is:
1st default Loads
2nd wuc_pageMessage loads
3rd wuc_lookup loads
The problem with this is that The wuc_pageMessage is relevant only after wuc_lookup runs.
My intent was not to put code in Page_Load for the message wuc_pageMessage control because I wanted to be able to call a method to post the message during the component load of wuc_lookup. I do this because only after wuc_lookup do I set the session which I use for the message value.
I actually got values showing up if I put the code in wuc_lookup to manipulate the code-in-front server control (panels and labels) using this.parent.findControl syntax...
But then when I try to rip that code and put it into the code-behind for wuc_pageMessage, and then call the method from the wuc_lookup it has fallen out of scope or context...
So I tried to change this.Parent by passing httpContext.current.handler as casting it as page...that didn't work...then I tried passing Object sender from the calling component...that didn't work either. Neither of them had the Parent property and or it was null which led me to believe that once the wuc_PageMessages.ascx loaded it was a dead deal until a repost happens and that is ugly and something I don't want to do.
I am having some implementation issues and I am not sure what to do. I have been stunk on this for eight hours and Is there just something I am not seeing?
I want to keep away from spagetti code. I don't want to have to scatter code-behind in 3 different files. Theoretically I should only need 2 of these to talk to eachother. i don't want code-behind in default..it's basically just a container. I want to trigger the wuc_pageMessage from wuc_Lookup.ascx without having it be "in" wuc_Lookup.ascx (peer web user controls) I want that to always be a peer relationship. Any advice would be great ...thanks...
Try moving the wuc_PageMessages logic from the page_load to the page_prerender event.
If you are going to use the preRender you would do it on the default.aspx preRender because this event fires after the wuc_lookup. Prerender will not fire on the components for some reason. So yes, this only solves part of the issue. I am not sure how you would get the alreeady loaded component of wuc_PageMessages to get back into scope. If you try to reference components on a component that has already loaded, you will get a null, like they are not there or not in scope anymore... anyone have any ideas?
Have any ways to keep a variables (object) in a page scope? I need pass some object from master page to all ascx files. Normally, i passing by using a method or structure with parameters. But now im asking another way to do this. Store it in session is a bad idea because my object should exists in a page scope only.
One way is to set up properties in your user controls. You have access to read and set these from all pages that implement them.
Another alternative is to store the shared object(s) in the HttpContext.Items collection.
You could expose your variables a public properties of the master page:
public string MyVariable { get; set; }
then access them from the user controls by referencing the master page, and casting to its specific type:
((MyMasterPageType)Page.Master).MyVariable
So you have masterpage, user controls and the page itself.
The page is the container for the masterpage and the user controls, hence it's the only one that 'knows' the 2 parties. So I'll suggest (if you haven't) you have the page instance to facilitate the object/variable passing amongst them.
If your variable's value is going to be changed on per page basis then i would recommend you to write that code in base page (or user control) and inherit all the page (or usercontrol),if the values are going to be similar for all pages(or user control) you can use cache object as well.
In a more better approach if you feel you can even create one helper class and call it from your base page (or user control), so you can separate variable assignment code from your page.