If I have an ASP.Net RadioButtonList within a TemplateField in a GridView, which is tied to a Data Control, I can get databinding on page loading like so:
<asp:RadioButtonList SelectedValue='<%#Eval("RequirementOption")%>'>
So when the gridview renders, the user can select an option for each row in the grid.
What I'm wondering is, when the save button is pressed and a postback occurs, is there a way to automatically push these selected values back through to the datacontrol so they can be saved, or do I have to manually loop through all the grid rows and exract the chosen value, like so:
For iRow As Integer = 0 To Me.myGrid.Rows.Count - 1
Dim selectedValue As String = CType(Me.myGrid.Rows(iRow).FindControl("RequirementOption"), RadioButtonList).SelectedValue
'...Load up the appropriate class and save the selected value
Next
Can it be done automatically via databinding?
NOTE: I am talking about about updating all the rows and then accessing all of them opn postback, not one row at a time via inline editing.
Yes, the selectedValue will be available as long as you specify Bind instead of Eval.
(assuming you're updating a record in the usual way)
Related
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.
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.
I have a webform that uses a dropdownlist to populate a gridview. The dropdown list is bound to a datasource. I would like to have the default value of the dropdown list be a value in the listing. I have a series of 5 values, one of which is actually a blank. The default value I am looking for would be index 2 or the value "Agency Error"
Here is the query I have for the datasource:
SELECT DISTINCT [AnnoType] FROM [AnnoType]
Is there a way to do this?
I am just trying to eliminate one of maybe 50 clicks the user needs to do in the process.
Use SelectedIndex property
dropdownlist.SelectedIndex = 2;
or SelectedValue property
dropdownlist.SelectedValue = dropdownlist.Items.FindByText("Agency Error").Value;
I have a DevExpress ASPXGridView which has been bound with a dataset.
I'm using the edit row command to edit the data in the row and return it to the database.
When the edit button is clicked, the row opens up and there is an ASPXComboBox that is bound to another dataset. The TextField and the ValueField have both been properly set in the ASPXComboBox and are displaying data correctly. The data is as shown
List item
List item
When e.NewValues("") is called the integer value field i.e 1 is returned. However, when e.OldValues("") the TextField is returned i.e. "List Item"
Is there a way of returning the the integer value when calling e.Oldvalues without having to make another call to the database?
Thanks
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.