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.
Related
I am using the DevExpress suite of web controls and have built a page with a fairly large entry form with textboxes, comboboxes, etc.
I am writing code to randomly generate data to populate this large form with. I call this code with a line on the Page_Load event but I realize that at the time of the call, anything that has a datasource attached to it (combobox, for example), is not databound yet.
Is there an existing event I can hook into that is called when the all of the page's controls AND datasources are bound?
The PreRenderComplete page lifecycle event is fired after all controls are loaded and databound.
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.
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.
When a page is posted back, which statement runs after page_load is done executing? Without knowing what controls are in the page. This is in VS 2008 debugger.
EDIT:
The question is about knowing which event and for which control comes next.
ASP.NET Page Lifecycle
PreInit
Init
InitComplete
PreLoad
Load
Control Events (e.g. ButtonClick)
LoadComplete
PreRender
SaveStateComplete
Render
Unload
The "next statement" is indeterminate. To put it another way, in the sense of "separation of concerns", it's none of your concern. It's the concern of ASP.NET, but not of individual controls on a page, nor of individual developers debugging a page.
I recommend that you determine what question you really needed answered, and what problem you really needed solved.
Here's an example of "why not": Consider the DataBinding event, which is raised when the Control.DataBind method is called, often from inside of Page_Load. Consider a page that contains a DataGrid control. When Control.DataBind is called, the DataBinding event is raised for the control, and Control.DataBind is then called on each control in Control.Controls, eventually causing DataBinding to be raised for those controls. When it gets to the DataGrid, the control will populate its Controls collection with one row for each row in the input data.
Each of the added controls will need to "catch up". They will go through the PreInit, Init, Load, etc. phases - everything up to DataBind.
There's no way to know ahead of time which controls will be added, so you certainly can't determine which events will be fired, and in which order. In fact, some of the control events will fire or not depending on the previous state of the controls. A SelectedIndexChanged event on a dropdown control in a template column of one of the rows may fire if the dropdown index has changed from the last postback, but not if it has stayed the same!
I need to get textbox that exits in Formview inserttemplate via javascript.
Both return null:
$get('txtTitle');
document.getElementById("txtTitle");
The problem is that formview is not rendered on form load...
As you stated, the formview contents are rendered on the server upon request rather than on page load. That said, try this code to access controls in the formview. Change the name of 'FormView' to match your unique control ID.
document.getElementById('<%=FormView.FindControl("txtTitle").ClientID%>');
If needed, Here are a few useful events you can use to register the javascript in the code behind if there are lifecycle considerations.
The ItemCreated event is raised after all rows are created in a FormView control. This can occur when the control is first rendered, or when the user navigates to another record. You can use this event to provide an event-handling method that performs a custom routine, such as adding to or modifying the contents of a row, whenever this event occurs.
Note:
The ItemCreated event occurs before the FormView control is bound to data. To modify the value of a bound field, use the DataBound event.