How does Databind consume a DataReader - asp.net

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

Related

Which life-cycle event should I execute my code in order for it to be after a Gridview is completely loaded?

I have a quick question. I have a gridview that would generate a large result from a relatively long load time. I want to wait until the gridview is completely populated and loaded until I execute a certain code. Would I use this under Page_Load? I was looking at these life-cycles, and it looks like it could possibly fall into others as well, such as LoadComplete.
If the code is related to processing the GridView after it's complete, then the most obvious event would be the OnDataBound event.
Event Description
This method notifies a server control that any data binding logic
associated with the control has completed.
If you are explicitly populating the GridView with a DataBind, then do it after you call the DataBind.
If the GridView is populated "automatically" with a datasourceid= in the aspx page, then you can do whatever in the page load event.

Call Dynamic Control's Event if exists

The scenario:
I have some JSON data which I'm using to load stored-data into fields on my form. One of these fields is a DropDownList. The DropDownList happens to be in a child, of a child ASCX control, which I'm accessing from a parent ASPX page. When this DropDownList has its SelectedIndexChanged, it makes other fields visible on the form.
I'm using one of my functions to find this control, which is working successfully, but when setting the SelectedValue of my DropDownList control, the SelectedIndexChanged event is not firing. Meaning some fields aren't loading, resulting in some JSON data not being loaded and lost.
I have seen a suggestion of simply calling ddl_SelectedIndexChange(sender, args) function, but the page I'm calling dynamically loads hundreds of child controls depending on the current request, so was wondering if there is a way of invoking the SelectedIndexChanged event (if it exists) for a control, without having to search and manually call the ddl_SelectedIndexChanged() function. Is it possible?
DirectCast(WebUtils.ControlFinder(upMain, f.fieldClientID), DropDownList).SelectedValue = f.fieldData.ToUpper()
I hope it makes sense. Sorry if I haven't made this clear enough.
I ended up using Reflection to Invoke the properties event that I required, worked a treat.

Binding for user controls embedded in page

I have an ASP.NET page where I call this.DataBind() to bind the controls. I also have various user controls embedded. One has a drop down list, the bind statement gets called for it 2x but the sender the first time is not the drop down list.
Am I using the databind incorrectly? I use databind to get the properties of my page bound to a datasource so that I can use those properties in the declarative code.
In my DropDownList, I added if (sender == dropDownList) which solved the problem
Make sure that none of your user controls are themselves calling .DataBind()
.

Get object from Formview with javascript

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.

Why does GridView reset its values to what it was before user edits?

If I bind GridView (via DataSourceID attribute) to SqlDataSource and set SelectCommand and UpdateCommand attributes, then everything works perfectly.
But if we manually call GridView.DataBind inside Page_Load(), then SqlDataSource doesn’t perform any updates, even though SqlDataSource.Updating and SqlDataSource.Updated events do fire when GridView’s Update button is clicked. I think this is due to the fact that GridView resets to what it was before the user edits:
a) Why does GridView reset its values if we manually call DataBind() inside Page_Load()?
b) Since Update operation doesn’t work when manually calling DataBind, I would then assume that Delete operation also wouldn’t work, but it does. Why?
cheers
I believe Page_Load runs before your changes take place, thus you bind the old data before you run the updates
Wrap the bind in a If Not IsPostBack when under Page_Load, i believe that will fix your problem.
You will need to assign the GridView with source and also databind it. Something along the lines of the following code:
Page_Load
if(!Page.IsPostBack)
{
gv1.DataSource = GetData();
gv1.DataBind();
}

Resources