How can I bind data to an ASP.NET Web Form report? - asp.net

I have a report 'defined' as a Web Form (aspx). It is divided into what I have classed as sections, where each section has a heading, optional description, and then either tables (GridViews), charts (user controls with asp:Chart controls), or details (in e.g. HTML definition lists). Currently the report is explicitly populated, i.e. code in Page_Load assigns the DataSource property and calls DataBind on GridViews and Charts, explicitly assigns scalar values to label controls for detail sections.
I would like to build a single data set type object (initially an untyped DataSet, but eventually any object graph) and assign this to a DataSource property on the whole report, and call the report's DataBind method. This should delegate data binding to report sections, e.g. I set the DataMember property on my report's PropertyDetails section to "PropertyDetails", so that the PropertyDetails section renders report values from the "PropertyDetails" data table in my big report data set.
How should I structure my Report page (or Control) to facilitate this? I would ideally like a declarative structure similar to the GridView or DataList control, but without a repeating element, i.e. each report section is different, unlike 'rows' in a GridView or DataList; in fact more like Columns in a GridView.

After wrapping your data into a DataSet simply inherit your Report from the BaseDataBoundControl class.
Then you can DataBind to it directly.

Related

Control State and View State vs. Databinding

Suppose I have a custom control like a Table with a varying number of rows in it. What would be the advantages of using databinding (like in a GridView) if I went that route? What would be the advantages if I used the control state to keep track of the number of rows?
Edit: Example.
Each TableRow in the Table object represents an employee. Each TableCell contains a TextBox input control. The user can add another blank employee (row) with a button under the Table. Information entered must be maintained through postbacks.
Using the states method, the number of rows can be saved into the control state. Since control state is loaded before view state, the number can be restored before the view state is restored (providing objects to take the view state and post values).
Binding with a GridView? I don't know much about how this works. From what I've seen, you can XML-serialize the data and save it to the Value property of a Hidden control.

Where is the link between a Data Source and DataSourceID?

When I attach a Site Map data source type to a site map control, I specify an ID for the data source.
Where in the code (or config files) is that ID associated with the corresponding Web.sitemap file?
Is it that there can be only one site map data source and the ID is actually redundant information?
Cheers.
DataSourceID is used when the source data is provided by another control of type DataSourceControl in the page, for instance a SqlDataSource control.
DataSource is used to provide the data directly. You should use either, but not both.
Look the DataSource is a control ,you can find it in the Data section of your tool box(ex:sqldatasource,objectdatasource,...etc).
You can set the datasource of your control(gridview for example)through one of two ways:
The first one is to drag the specific datasource control from the
toolbox and drop it in your page,then set the DataSourceID property
of your control by the id of the datasource control you has dragged.
The second one is through the code behind, you can set the
DataSource property of your control then call the DataBind().
You can't use both of the two ways at the same time. but you can solve this problem.if you wanna to use the both (each one under specific case or condition)then first you should set the other one by null before you can use the other one.
like this:
gv1.DataSource = null;
gv1.DataSourceID = ObjectDataSource1.ID;
gv1.DataBind();
and vice versa .

Bind a Text Box to a Field Selected by SQLDataSource (VB.NET)

I'm looking for the easiest way to bind data from a SqlDataSource to textboxes dropped in Visual Studio 2008.
For example, I have 4 textboxes currently that have Address, City, State, Zip. I also have a SqlDataSource on the page fetching the ID of the record and selecting those 4 fields based on ID.
How am I able to quickly bind each box to those particular fields selected? I would think this would be really straight forward - but seems it's not. Seems like the answer is funneled towards having to create a GridView or some type of control.
Be gentle...I'm a nub :)
In general you are correct, if you want to use databinding you'll need to use an appropriate control. For this example I'd suggest using a FormView - it is designed to display the results from a single database record and uses templates, meaning you'll have complete control over the output. This article is probably a good place to start: FormView Control: Step by Step.
To read the values bound to the FormView in the code-behind class you would need to create an event handler for the FormView's DataBound event. In that event handler you would reference the controls programmatically via FindControl, like so:
Dim myLabel As Label = CType(FormViewID.FindControl("id"), Label)
Here, id would be the ID of the Label whose value you were interested in. Once you have a reference to the Label you can get its value using myLabel.Text.

ASP.NET Nested Data Bound Controls

Say I have a Form View with a Repeater inside of it, and then a Data List nested inside of the Repeater. If I had an Update Command in the DataSource for the Form View would I be able to associate bound values within the Data List to parameters in the Form Views' DS?
You can always go up using .NamingContainer up to the container that has the data you want, and access through .DataItem.
Something around the lines:
((ContainerType)Container.NamingContainer.NamingContainer).DataItem ...

ASP.NET Listbox databound to List of custom objects. Does it keep the actual objects?

in ASP.NET,
I've bound a listbox control to a List of custom class (consisting of a TimeSpan and Int members).
the ToString() function just displays them both as a long string.
Now I want to save the modified listbox into the DB again, I need to use the custom class objects again.
Does the ListBox save the actual custom objects once its databound? or only the string representations?
If so, how do I get them from the LB?
Googling doesn't seem to help much, but it seems to me that a databound ListBox contains objects called ListItems, which have a Text and a Value property. The databinding figures out which is which from your custom class and displays them.
Short answer: no, it does not look like the ListBox is storing the actual class.
Once you've created the values into a collection which you are binding to your listbox then you could use viewstate to persist them between postbacks.
e.g.
ViewState["TimespanItems"] = myListHere;
myListHere = (myListTypeHere) ViewState["TimespanItems"];

Resources