Bind a Text Box to a Field Selected by SQLDataSource (VB.NET) - asp.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.

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.

Using GROUP BY with SQLDataSource

I have a SQLDataSource in an ASP.NET Web Form, bound to a GridView and would like to perform a GROUP BY on the underlying data after it has been bound.
I would like the grouped data to appear in a separate GridView.
How would I accomplish this task?
would like to perform a GROUP BY on the underlying data after it has
been bound.
You cannot do that. Once the data is bound, it is bound. The same data will be displayed in the gridview however it was pulled in query with out without grouping. You cannot change it afterwards.
I am thinking you want something different as your question suggests. You want to be able to group on the fly depending on what field the user select. For that you will need different set of controls. May be a dropdownlist and a Gridview Combination? That will be a different scenario.
You can also bound sql datasource on the fly in C# code rather than in ASP.NET code. In that you would be able to achieve it with some additional controls like if this button is clicked, group by this field or if that button is clicked, group by that field.

Is it possible to have a SQLDataSource with a parameter that is based only upon the GridView that is binding to it?

I have a scenario where I want to put four identical Gridviews on the same page. (They will be on different tabs in an Ajax TabControl.) They show the same source data, but there are four corresponding groups of source data in a common underlying table. So I want to show Group 1 on Tab 1, Group 2 on Tab 2, etc. These Gridviews contain complicated controls, so I would prefer to use the same data source for all of them to avoid unnecessary repetition. The Insert and Update commands are completely identical.
So in theory I could build the Select command in such a way that I could filter the data based on the GridView that is binding to the SQLDataSource. The problem is that if I use the same SQLDataSource for all the Gridviews, I cannot find a way to have each GridView tell the SQLDataSource which one is calling it. I am thinking maybe this is not possible, because the SQLDataSource binds first before it knows what is binding to it, but I'm not sure. Can anyone think of a way to do this?
You can change the parameter value dynamically using OnSelecting event of SQLDataSource. This can be done in server side code.
Create a property which holds your current gridview unique key, which is causing SQLDataSource to fetch data from SQL database.
Assign this property unique gridview key on DataBinding event of gridview.
Based on this property change the parameter in OnSelecting event of SQLDataSource.
Let me know if I am missing something.

What is the use of InsertVisible property of BoundField in a GridView

Can any one explain to me what the use of "InsertVisible" property of BoundField in a GridView is and at what conditions we should use it? I have seen the description on msdn.com but I am not able to understand exactly.
Thanks in advance.
The GridView control is not designed for inserting data. Thus, this explanation is more applicable to a DetailsView or a FormView.
When you put a databound control into "insert mode", it turns all of the BoundFields into user input controls (TextBoxs, CheckBoxs, etc) by default. InsertVisible allows you to change this as follows:
InsertVisible = true: A user-input control will be generated. This allows a user to enter a value for that field into your datasource.
InsertVisible = false: A user input control is not to be generated, thus the user is not given the opportunity to enter a value.
This is especially useful if you plan to programatically fill out that field (with an auto-generated ID or some calculated value).
Here is the MSDN article as a reference (I realize you mention that you read the article, this is just for the sake of completeness).

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 .

Resources