Simply, if I have a GridView with a SqlDataSource control declarative set as its data source, when does that data source retrieve its data and when does the binding take place in the page lifecycle?
In the 'preRender' phase - look at the ASP.NET Page Life Cycle Overview for more info.
This article about Page Cycle is pretty good.
For example, suppose you have a GridView that displays a company record in each row along with a list of the company officers in a ListBox control. To fill the list of officers, you would bind the ListBox control to a data source control (such as SqlDataSource) that retrieves the company officer data using the CompanyID in a query.
If the ListBox control's data-binding
properties, such as DataSourceID and
DataMember, are set declaratively, the
ListBox control will try to bind to
its data source during the containing
row's DataBinding event. However, the
CompanyID field of the row does not
contain a value until the GridView
control's RowDataBound event occurs.
In this case, the child control (the
ListBox control) is bound before the
containing control (the GridView
control) is bound, so their
data-binding stages are out of sync.
To avoid this condition, put the data
source control for the ListBox control
in the same template item as the
ListBox control itself, and do not set
the data binding properties of the
ListBox declaratively. Instead, set
them programmatically at run time
during the RowDataBound event, so that
the ListBox control does not bind to
its data until the CompanyID
information is available.
Related
I am trying to figure out what the correct event would be to populate a ComboBox in a Telerik RadGrid or any ASP.NET grid from the CodeBehind. When the user clicks Edit on a row the ComboBox should be populated with its items.
The only examples I have seen are using the DataSourceID property in the aspx page. I prefer doing all of my populating manually in the code behind:
ComboBox1.DataSource = colorList;
Combobox1.DataBind();
You can access child controls in a grid column in:
ItemDataBound - fired for each item (so you need to check for GridDataItem types or GridEditItem, depending on your goal)
ItemCreated - similar, but you don't have the data item object associated with each item
ItemCommand - when Edit is clicked an Edit command is invoked so you can access it.
im working on asp.net with c#, and I have a gridview with textboxes and dropdownlists.
I have data loaded from an sql database into the gridview with the GridView1_RowDataBound.
I have a linkbutton which once is clicked brings the GridView1_RowEditing event, this enables the whole row for editing. But when this happens, all the data for the corresponding row is gone. All the textboxes and dropdownlists have no value.
What can I do, so the values stay on the controls once the RowEditing event is fired?
Be sure that you are not data binding after the control has been loaded. Be sure that you are populating the data for your control only after making sure the page is not being posted.
If Not Page.IsPostBack Then
'Populate control
End If
Otherwise the control data will be reset as viewstate data is loaded on PreInit before the Load event is called.
Also, ensure you have viewstate enabled unless you are repopulating the control data.
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.
On my asp.net page, I have several DropDownLists.
I also have a Repeater.
In the ItemDataBound event I want to get the value of these DropDownLists, to change the data in the Repeater. The SelectedValue of these DropDownLists is empty.
But after the ItemDataBound, the Page_Load is executed. There I can get the value of these DropDownLists.
Is there a solution to get the value when the ItemDataBound is executed.
thanks!
Filip
You need to data-bind these drop-down lists in the Page.Load event.
There're a lot of web controls that get their state or other details during load life-cycle (I had these kind of problems long time ago).
NOTE: When I say "State" I'm not talking about ViewState.
And why don't you do that data-bind after the load event?
Can you get the drop down lists' selected values in the page PreInit event? If so, store them in view state and retrieve them from view state during the repeater's item data bound event.
If that doesn't work, try adding a selected index changed event to each drop down. When the drop downs change, set a view state variable that you can retrieve during the repeater's item data bound event. If you have values to which you are setting the drop downs during page load, such as when reading from a database, use those values to directly set the appropriate view state variables.
If I have a control on a page that has its Datasource set to a DataReader, does that control consume the reader at the time the Datasource is set, or does the datareader continue to exist until Databind has been executed?
What actually happens under the covers when Databind is executed?
Depending on the Control, DataBind() will Bind the Data to the
Control. It does this by Iterating through the DataSource and create
the Html and other Controls that are needed.
For a DropDownList, DataBind() will create the ListItem for each
record in a DataSet or each Element in an ArrayList.
Later the Render method is call on the DropDownList, which returns the
Html for a Select tag. It also creates the Html for each ListItem by
returning Option tags inside the Select tag.
For a Label, DataBind() will set the Text to the value you pulled from
the Database (for example).
If you don't call DataBind() for the specific control, you can also
make sure that your DataSource is set for a control and call
Page.DataBind(). This will go through the Controls in the Page and
call all of the DataBinds for each Control.
It should be consumed at the time DataBind is executed.
What is the control doing with the datareader during databind? Does it copy it into its internal structures and dispose of the datareader then render?
If I have 10 controls on a page and set the datasource on each to a different datareader, then called page.databind, will the datareaders exist the entire time (from the point of creation until the point where the page.databind completes it's processing)?