Control State and View State vs. Databinding - asp.net

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.

Related

asp.net webform Repeater, Update a set of textbox, Add to the List / Array

I am working on old webform asp.net application which uses Repeater.
The Repeater is a set of Name and address textbox fields. I have 3 sets of Name and Address information, basically, 3 sets of records in the object list to bind.
On the UI, I changed/update one of the Name and Address. I noticed in the list, which it iterate or loops through the repeater control
foreach (RepeaterItem item in this.NameAddressRepeaterControl.Items)
I see that an extra recod is added to the items.
Question:
I am used to fixed textboxes. When I update the textbox, I write code to take Exactly what is filled in the textbox and populate the DTO object to pass to the data layer to perform database transaction to update the database records.
When the new updated record is added to the Repeater Control list, I don't know which records is updated and which is the new records.
I am checking out OnItemDataBound and OnItemCommand to see if there is a way to get the old value from one of the field and also record the value of the new value. Is this possible?
The form contains 1 Save button and it will loop through the Repeater.Items to see what Name/Address to extract, but the new and old company exist in this list.
Instead of looping through the RepeaterControl.Items, Is there a way to extract from directly the visible Repeater control? If there are 3 repeater Name/Address control, is there a way to get all the info from each of the 3 sets of Repeater controls? The Repeater wraps around a user control, NameAddressCtrl.
I prefer not to replace the Repeater controls with fixed textboxes.
Any help is greatly appreciated.
Thanks.
I reviewed the code several time and add additional code to keep track what was add and changed in the repeated and only passed the information to be saved to the db. The solution was to write additional code. I did not change anything with the repeater behavior. Thanks for the comments and help.

ASP.NET controls disappearing from dynamically built user control

I have a curious problem with an ASP.NET user-control which is dynamically created in VB.NET. Sorry that this is a long post, but I think it’s better for me to explain carefully what I’m doing and what the problem is.
Essentially, the user-control is an HTML table used to display data from a back-end SQL Server DB. It uses a stored procedure to return a DataTable, from which it builds html rows and columns with the values stored in the InnerHtml property of the relevant table cell. This is just a passive display of the data and works perfectly.
However, the user-control also has the facility to insert a new record, triggered by an ASP button. This causes the following sequence of events;
Post-back
The user-control_Init event rebuilds all the existing rows in the table and inserts their values as described above.
The page_Init event.
The page_Load event.
The user-control_Load event.
The button_Click event then builds a new row with the same structure as the main table. But this time it inserts into each table cell a relevant ASP.NET control, such as a textbox or checkbox, holding “null” values. Each control has its ID set explicitly to a unique value. Finally, an “insert flag” is set to true.
The user then enters the data into these controls and clicks an Insert-Save button. This causes another post-back, which follows the procedure described above, except that the “insert flag” tells the user-control_Init event (2) to rebuild the insert row. Then, before the page_Load event (4) the values previously entered by the user are reinstated in the ASP.NET controls from ViewState. Finally, the Insert-Save_Click event calls a stored procedure to save these data to the DB.
All this work perfectly when there are existing rows in the html table. But – and this is the strange behaviour – it fails when there are no existing rows. The first post-back completes successfully although, obviously, event 2 skips over the rebuilding of the existing rows because there are none. However, the insert row is rebuilt correctly and the user can then enter data into the ASP.NET controls. During the second post-back (Insert-Save), the first three events described above work perfectly. But, the moment the code hits the page_Load event (4), all the ASP.NET controls disappear. The html row and cells are still present, including the literal control (index 0) in each cell. No error is thrown at this stage, but when the Insert-Save_Click event occurs and the data should be read from the ASP.NET controls – they are not available and an "index out-of-bounds" error is thrown, because the ASP.NET controls should each have an index of 1 within their cells.
I’ve spent hours (days!) debugging this and I cannot see what is causing these controls to just evaporate! Any clever programmers out there got any ideas please?

Data copied between rows on form's datagrid

Within AX 2009, I have, through compare and compile, added two new controls within a datagrid on a form, a Real edit and a combobox. I have compiled with no issues. The Allow Edit property is set to Yes on both controls.
However, on the form, if I edit one row, whether typing a new number with Real edit or combobox, and don't hit Save but hit the Down Arrow key, the data I typed on the previous records is duplicate in the next record and so on until I release the Down Arrow key, rather than just setting the focus on a new record.
The table where these fields were created doesn't exhibit this behavior. The focus simply moves to the next record and what was typed will not carry over to the next record. Only the form does this...
Has anyone seen this behavior before with AX forms?
You may have omitted to specify the data source on the grid itself?
Or if the new controls are based on Edit methods on the data source, have you got the data source parameter in the method signature?
see http://msdn.microsoft.com/en-us/library/aa637541(AX.10).aspx

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

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.

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.

Resources