I am binding it to XPCollection, how to dispose old image of devexpress PictureEdit control, when a new image is set?
Try handling the EditValueChanging event and disposing e.OldValue as IDisposable.
Related
I am using web forms to develop a web application.
From the very beginning, I have always been surprised by the page_load event being fired twice, and finally today I found out that all gridviews and texts are rendered after first page_load, and it takes another page_load to render all the dynamic asp.net charts..
why is it so, is there an attribute on the chart web server control that I can use to bypass this?
Check to see if you "AutoEventWireup" set to true. If this is a control, check the parent control/page also. Even if the control itself is set to false, but the parent is set to true, it seems to fire anyway.
Also check where you assign listener to Page_Load event:
this.Load += new System.EventHandler(this.Page_Load);
Should be in InitializeComponent method
How need I to refresh my page after SqlDataSource1.DataBind();
for my GridView appears this new data.
A grid view has a DataBound event to which you can subscribe to and achieve the desired behaviour by calling Response.Redirect(Request.RawURL) or via javascript window.location.reload()
I have a User Control (uc) on a page. uc exposes some properties that get set on Page_Load() event of the parent page, and should be read while user control loads up.
Looks like Page_Load() of the uc fires before any properties get set from the parent page.
On what event should I set the uc properties so that it can use those properties as it gets rendered?
I use ASP.NET 3.5 and C#
p.s. i just dug out a solution from my old code:
in Page_Load of the control do:
Page.LoadComplete += delegate { LoadTheControl(); };
i'd still like to hear your ideas though.
You can get access to the property OnPreRender without any tricks.
I JUST ran into this problem this afternoon myself.
I created a public method on the UserControl called LoadData() and called it from the Page_Load of my hosting page after setting the property that the data depended on.
Added - code example
In the user control:
public property SomeProp {get, set};
public void LoadData()
{
// do work
}
and in the Page_Load on the hosting page
myControl.SomeProp = 1;
myControl.LoadData();
If you're putting the usercontrol on the page declaratively, just set the property there.
<cc1:myUserControl MyProperty="1" />
If you're adding the controls to the page dynamically, just set the property before you add the control to the hosting page's control collection. None of the lifecycle events for the control will fire until you call Page.Controls.Add(myUserControl).
If it is impossible to avoid the setup you've got right now, and the parent must perform some logic in Page_Load and the UC has to be there already, then I would suggest what David Stratton posted, but in my experience this is usually standardized to be called Initialize() or Init().
Well,
None of the answer help me.
But when a set manually the properties on the event InitComplete() everything works fine.
My user control was, now, enabled to read the property AFTER i set the property not before.
I have encountered a similar problem:-
I first add a UserControl dynamically, and then attempt to assign values to the control's internal controls (a textbox within the user control). Using any event such as INIT or LOAD, the control gives me a null reference exception, because as evidenced by stepping through the code, the controls have not as yet been created.
Later I found that this problem only occurs if I declare the control through a dim statement;
Dim MyControl as new MyUserControl
Me.Panel1.Controls.add(MyControl)
MyControl.SetLabelText("My Name") 'Uses a method to set the label text on load.
On the other hand, if I use the control as follows it works just fine:
Dim MyControl as MyUserControl = LoadControl("MyUserControl.ascx")
Me.Panel1.Controls.add(MyControl)
MyControl.SetLabelText("My Label Text")
Hi I have a Repeater and I am doing various things during the ItemDataBound event. My Repeater is in a control. What I am noticing is that the things that are supposed to happen in the ItemDataBound event happen after the Page_Load of the page hosting the control. Is there a way to use some sort of ItemDataBoundComplete trigger so I can do other things on my page after the events of the ItemDataBound have taken place? Thanks, please let me know if I have not been clear.
[Edit] I have controls that are being bound to the ItemDataBound and they are not available until after the Page_Load for the page hosting the control.
[Solution] (In my case):
In my page I used the following:
Control.Page.LoadComplete += new EventHandler(Control_LoadComplete);
Then I performed what I had to do in that event.
What kind of things? You can override the OnPreRenderComplete method, which is called immediately before the page renders.
You can also change to a ListView, which is support as much flexibility in the html, and use the DataBound event (which is called after the whole ListView has finished data binding).
I have a rather complex page that dynamically builds user controls inside of a repeater. This repeater must be bound during the Init page event before ViewState is initialized or the dynamically created user controls will not retain their state.
This creates an interesting Catch-22 because the object I bind the repeater to needs to be created on initial page load, and then persisted in memory until the user opts to leave or save.
Because I cannot use ViewState to store this object, yet have it available during Init, I have been forced to store it in Session.
This also has issues, because I have to explicitly null the session value during non postbacks in order to emulate how ViewState works.
There has to be a better way to state management in this scenario. Any ideas?
Edit: Some good suggestions about using LoadViewState, but I'm still having issues with state not being restored when I do that.
Here is somewhat if the page structure
Page --> UserControl --> Repeater --> N amount of UserControls Dynamicly Created.
I put the overridden LoadViewState in the parent UserControl, as it is designed to be completely encapsulated and independent of the page it is on. I am wondering if that is where the problem is.
The LoadViewState method on the page is definitely the answer. Here's the general idea:
protected override void LoadViewState( object savedState ) {
var savedStateArray = (object[])savedState;
// Get repeaterData from view state before the normal view state restoration occurs.
repeaterData = savedStateArray[ 0 ];
// Bind your repeater control to repeaterData here.
// Instruct ASP.NET to perform the normal restoration of view state.
// This will restore state to your dynamically created controls.
base.LoadViewState( savedStateArray[ 1 ] );
}
SaveViewState needs to create the savedState array that we are using above:
protected override object SaveViewState() {
var stateToSave = new List<object> { repeaterData, base.SaveViewState() };
return stateToSave.ToArray();
}
Don't forget to also bind the repeater in Init or Load using code like this:
if( !IsPostBack ) {
// Bind your repeater here.
}
This also has issues, because I have to explicitly null the session value during non postbacks in order to emulate how ViewState works.
Why do you have to explicitly null out the value (aside from memory management, etc)? Is it not an option to check Page.IsPostback, and either do something with the Session variable or not?
I have always recreated my dynamic controls in the LoadViewState event. You can store the number of controls needed to be created in the viewstate and then dynamically create that many of them using the LoadControl method inside the LoadViewState event. In this event you have access to the ViewState but it has not been restored to the controls on the page yet.
1) there's probably a way to get it to work... you just have to make sure to add your controls to the tree at the right moment. Too soon and you don't get ViewState. Too late and you don't get ViewState.
2) If you can't figure it out, maybe you can turn off viewstate for the hole page and then rely only on querystring for state changes? Any link that was previously a postback would be a link to another URL (or a postback-redirect).
This can really reduce the weight of the page and make it easier to avoid issues with ViewState.
protected override void LoadViewState(object savedState)
{
// Put your code here before base is called
base.LoadViewState(savedState);
}
Is that what you meant? Or did you mean in what order are the controls processed? I think the answer to that is it quasi-random.
Also, why can't you load the objects you bind to before Page_Load? It's ok to call your business layer at any time during the page lifecycle if you have to, with the exception of pre-render and anything after.
When creating dynamic controls ... I only populate them on the initial load. Afterwords I recreate the controls on postback in the page load event, and the viewstate seems to handle the repopulating of the values with no problems.
I have to explicitly null the session
value during non postbacks in order to
emulate how ViewState works.
I'm still foggy as to why you can't store whatever object(s) you are binding against in session. If you could store that object in session the following should work:
On first load bind your top user control to the object during OnPreInit. Store the object in session. Viewstate will automatically be stored for those controls. If you have to bind the control the first time on Page_Load that is ok, but you'll end up having two events that call bind if you follow the next step.
On postback, rebind your top user user control in the OnPreInit method against the object you stored in session. All of your controls should be recreated before the viewstate load. Then when viewstate is restored, the values will be set to whatever is in viewstate. The only caveat here is that when you bind again on the postback, you have to make 100% sure that the same number of controls are created again. The key to using Repeaters, Gridviews etc... with dynamic controls inside of them is that they have to be rebound on every postback before the viewstate is loaded. OnPreInit is typically the best place to do this. There is no technical constraint in the framework that dictates that you must do all your work in Page_Load on the first load.
This should work. However, if you can't use session for some reason, then you'll have to take a slightly different approach such as storing whatever you are binding against in the database after you bind your control, then pulling it out of the database and rebinding again on every postback.
Am I missing some obvious detail about your situation? I know it can be very tricky to explain the subtleties of the situation without posting code.
EDIT: I changed all references to OnInit to OnPreInit in this solution. I forgot that MS introduced this new event in ASP.NET 2.0. According to their page lifecycle documentation, OnPreInit is where dynamic controls should be created/recreated.