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
DataBoundevent. In that event handler you would reference the controls programmatically viaFindControl, like so:Here, id would be the
IDof the Label whose value you were interested in. Once you have a reference to the Label you can get its value usingmyLabel.Text.