Introduction:
FormView control is a new control introduced in ASP.NET 2.0.
FormView is a template based control which means that you can embed different
controls inside the FormView. In this article I will explain that how
you can populate and access different types of web controls which reside inside
the FormView.
Database Design:
In this article I will be using a custom database. The
database is called "School" and it contains a single table called
"Users". Take a look at the database diagram below to get the clear idea:
Creating the User Interface:
The first thing that we need to do is to implement the user
interface. Take a look at the screen shot below which gives you an idea of the
user interface.

As, you can see from the screen shot above the FormView contains the
DropDownList, CheckBoxList and the ListBox controls. All
the controls are populated with some dummy data. Let's take a look at how these
controls are populated. But first we need to assign a data source to the
FormView control. Take a look at the code below which assigns the data source to
the FormView control. The HTML code for the FormView control is given below:
| <asp:FormView
ID="fv1"
runat="server">
<ItemTemplate>
<table>
<tr>
<td>Name:</td>
<td>
<asp:DropDownList
ID="ddlNames"
runat="server"
/>
</td>
</tr>
<tr>
<td>
Select a name from CheckBoxList:
<td>
</tr>
<tr>
<td>
<asp:CheckBoxList
ID="cblNames"
runat="server"
/>
</td>
</tr>
<tr>
<td>
Select a name from ListBox:
</td>
<td>
<asp:ListBox
ID="lbNames"
runat="server"
/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
|
Populating the FormView Control:
You must be wondering that why we are assigning the FormView
with the data source. The reason is that if we don't assign the data source then
the FormView won't even display. FormView requires a data source in order to
display on the webform.
| private
void BindData()
{
SqlConnection
myConnection = new
SqlConnection(ConnectionString);
SqlDataAdapter
ad = new
SqlDataAdapter("SELECT
* FROM Users", myConnection);
DataSet ds =
new
DataSet();
ad.Fill(ds);
fv1.DataSource = ds;
fv1.DataBind();
} |
Populating the DropDownList Inside the FormView Control:
The first thing that we need to do is to populate the
DropDownList inside the FormView control. Check out the code below:
| FormViewRow
row = fv1.Row;
// Populating the DropDownList
Control inside the FormView control
DropDownList
ddlList = (DropDownList)
row.FindControl("ddlNames");
ddlList.DataSource = GetNames();
ddlList.DataTextField = "FirstName";
ddlList.DataValueField = "UserID";
ddlList.DataBind(); public
DataSet GetNames()
{
SqlConnection myConnection =
new
SqlConnection(ConnectionString);
SqlDataAdapter ad =
new
SqlDataAdapter("SELECT * FROM Users",
myConnection);
DataSet ds =
new
DataSet();
ad.Fill(ds);
return ds;
} |
Populating the CheckBoxList Inside the FormView Control:
Now, that we have populated the DropDownList now let's see how
we can populate the CheckBoxList control. Take a look at the code below:
| CheckBoxList
checkboxList = (CheckBoxList)row.FindControl("cblNames");
checkboxList.DataSource = GetNames();
checkboxList.DataTextField = "FirstName";
checkboxList.DataValueField = "UserID";
checkboxList.DataBind(); |
As, you can see from the above code that the technique of
populating the CheckBoxList control is also very similar to the DropDownList.
You can also populate the ListBox control in a similar way.
Accessing Different Controls Inside the FormView:
Now, that we have populated the controls which reside inside
the FormView control we can start accessing them. Let's first see that
how we can access the DropDownList control. Take a look at the code below
which extracts the selected value from the DropDownList control.
|
private void
DisplaySelectedDropDownListValue() {
FormViewRow
row = fv1.Row;
DropDownList
ddl = (DropDownList)row.FindControl("ddlNames");
Label1.Text = ddl.SelectedItem.Text;
} |
The FormViewRow object represents a single row of the
FormView control. We used the FindControl method of the
FormViewRow to locate the DropDownList. Once, spotted we get the
reference to the DropDownList and assigns the SelectedItem.Text
property to the Label control.
Next, we will see that how we can access the CheckBoxList
control.
CheckBoxList control is different from the
DropDownList in a sense that it allows the user to select multiple items.
Let's see that how we can iterate through the ListItem collection of the
CheckBoxList.
| private
void
DisplaySelectedCheckBoxListValues() {
string
genString = String.Empty;
FormViewRow
row = fv1.Row;
CheckBoxList
cbl = (CheckBoxList)row.FindControl("cblNames");
foreach (ListItem
item in
cbl.Items)
{
if (item.Selected)
{
genString += item.Text + ",";
}
}
Label2.Text = genString.TrimEnd(',');
} |
Once, again we are using the FormViewRow object which,
contains the CheckBoxList. Once, the CheckBoxList control is found
we iterate through the ListItem collections using a foreach loop.
The ListItem.Selected indicates that if the item was selected by the user. If
item was selected we simply concatenates the item text to a string. It is a
good idea to use the StringBuilder when doing concatenation. This is because the
strings are immutable which means each time you concatenate a string it will
create a new string in the memory hence, killing the performance of the
application.
The code for the ListBox control can be found in the
article download.
I hope you liked this article, happy programming!