Where can I find the order of ListView events? - asp.net

I was wondering specifically about whether ItemCreated or ItemBound happens first but I can't seem to find any information on the order that ListView events fire. Can anyone point me to a resource for this information?
Note: this is for the System.Web.UI.WebControls.ListView

You can test it handling both events for a ListView and setting breakpoints. I tell you the end of the story: ItemCreated is executed first.
While ItemCreated happens on every request to the page containing the ListView, ItemDataBound only get fired if your control is bound to a data source.
It may happen that you bound your control on the first request and you don't do it on postback, in this case the ViewState information is used and the event ItemDataBound is not fired.
From RadGrid for ASP.NET AJAX (not the same control but the idea applies)
ItemCreated should be hooked when you need to modify the controls inside a grid cell. ItemDataBound should be wired in order to change the data displayed inside a grid cell and its controls.

Related

Asp.NET Page Lifecyle: do Event Handlers run before control Databinding events?

I am trying to take advantage of the page lifecycle in Asp.NET so that I don't bind to my datasources more than I actually need to.
This has led to the following problem. I have an ObjectDataSource, GridView, and a button on my page. The button adds a record to the database, which should be picked up by the data source and presented on the Grid. The problem is that the item is not showing up on the gridview until I refresh the page.
I can solve the problem by calling GridView.DataBind() in my button's event handler, but this goes against what I understand about the .NET Page Lifecycle.
Based on this article, the lifecycle should be as follows:
In addition the article states that the Databinding event is "Raised after the control's PreRender event, which occurs after the page's PreRender event."
So, my button click event should fire first, during the Event Handling phase. It should add the record the database.
Next PreRender should be called on the controls.
Finally DataBind should be called on the controls, and the Grid should update to capture the new record.
Yet this doesn't seem to happen. What am I not understanding?
I think the issue is that your viewstate is not enabled on your GridView. This is what I experienced, but then I also had to call DataBind() on the GridView from the page PreRender event if the request was a postback to get the data updated in the GridView on postback.
Edit:
It would help to understand the issue and context better if you could post the source code of your page (aspx + codebehind). How and where do you connect your GridView to your datasource? Statically in markup or dynamically? Do you make any calls to page.DataBind()? ... These things may influence the behaviour of the GridView.

Items in a Repeater are lost during a postback (callback)

Using ASP.NET 4.0 WebForms.
I have a repeater with checkboxes in it. The repeater and checkboxes have viewstate enabled. There's a button which causes a callback postback. These elements reside in a callbackpanel (similar to MS's UpdatePanel but doesn't use viewstate). The repeater is bound to a datatable during initial load and repeater shows the checkboxes. Fine so far.
During the postback (a callback), I noticed that the repeater's items' count is 0 in the page_load. Therefore I can't get any checkbox values. I can see the key/checked value entries of the checkboxes in Request.Form collection.
I think I am missing something obvious but where in the life cycle can I read the repeater's items?
Or should I get them from Request.Form?
1) Try wiring up to LoadComplete. All child controls are loaded recursively, so sometimes not everything you expect to be there will be present during the Load event.
2) Make sure your repeater is initialized during or prior to Init event. If you have some code that runs to populate it, then this must be executed in Init. During the page's lifecycle, ASP.NET tries to take the posted values and apply them to the controls. If the controls are not created soon enough during the lifecycle, then they won't exist during that step to accept the posted values. This is one of the more frustrating things with dynamic pages in ASP.NET, as you have to ensure the page is reconstructed during the postback in init. So even when you see the data in the post, ASP.NET will ignore it if the controls aren't found in its collection of controls. Also, there is some magic that happens with control IDs that it uses to map the posted values into the controls. I don't remember the details of that though because it has been several weeks since I had to delve into the harry details.
Also, make sure you aren't doing something like using if(!IsPostBack) to initialize your repeater or other controls/data. Even if it's a postback, you still need the controls to exist so they can accept the posted values.
I have not used a callbackpanel though, so I am unsure of how this will muddy the waters.
I solved my problem by overriding the DataBind method on the control which contains the repeater. That prevents DataBinds on parent controls from trickling down and binding data to the repeater when it isn't ready for that. My problem was that a parent control/page performed a DataBind which trickled down to the sub-controls, including the repepater, causing it to get cleared. So you don't have to do a DataBind directly on the repeater on every postback like #AaronLS suggests:
Also, make sure you aren't doing something like using if(!IsPostBack)
to initialize your repeater or other controls/data. Even if it's a
postback, you still need the controls to exist so they can accept the
posted values.
All my RepeaterItems were stay present through postbacks even though I only databind once.
In my case, performing a DataBind on the repeater for every postback caused the form values to be reset.

DataBound event is not available for DataList control in ASP.Net

I am working on a project in ASP.Net, in which I am using DataList control to list
the candidate data. As we know, DataBoud event is available for gridview control, which
is raises after databound.
In DataList control no such event is available. Yes, ItemDataBound event is there
which is arises on every Item bound.
Let us assume, that I want the number of items are listed in datalist, after
completing the data bound of datalist.
Thanks
Indeed the DataList control does not expose a DataBound event. The latter was introduced in ASP.NET 2 (with the BaseDataBoundControl).
In your case you can use a different event to achieve your goal. The PreRender event looks like a good candidate.
The number of items in the DataList will be equal to the number of records in the underlying DataSource. Also, you can use the PreRender event for this purpose.

Benefit of Binding a GridView in Page_PreRender vs Page_Load?

Assume I have an asp:GridView with some customer information. On that page I also have a button which allows me to add a new user to the GridView. When I add the click the user, I databind the gridview again in the button click. Correct me if I am wrong, but if the asp.net lifecycle or at least part of it is in the following order:
page_load
Hookup event handlers such as my button click
page_preRender
Does this mean that if I put the databinding for the gridview in preRender, that is the only place I need to worry about calling it. I can remove it from the button click. This is what I think, but I am not sure if my thinking is correct, so I would like some more insight as to the benefit of putting code in PreRender as opposed to PageLoad
By PreRender, you assume that most logic in the page that would affect the binding result has been completed (usually in Page_Load, but anywhere earlier really).
Controls in the .Net framework by default do their binding in the PreRender event (makes sense, bind to the datasource at the last possible moment...could be that you changed the data 400 times earlier in the lifecycle). Here's the full layout of the 2.0 lifecycle for reference.
PreRender - Before this event occurs:
The Page object calls EnsureChildControls for each control and for the page.
Each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.
The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.

Repeater ItemDataBound Complete Trigger

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).

Resources