Accessing Different Controls Inside FormView
By AzamSharp
Views: 9301

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!

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Formview dropdownlist
Name: joe
Date: 2/16/2007 11:59:59 AM
Comment:
Hi,

I've been stuck populating a ddl in a formview. What you don't specify is the most importatnt thing in what event do you populate the dropdownlist.
Subject: Re: Populating the DropDownList Control
Name: AzamSharp
Date: 2/17/2007 8:42:54 PM
Comment:
The DropDownList is populated inside the BindData method which is called when the page loads the first time. If you need more details I suggest that you download the sample.
Subject: using ListBox in FormView set to insert
Name: Angela
Date: 3/21/2007 2:17:11 PM
Comment:
I am new to asp.net. I'm trying to use a ListBox inside a FormView whose defaultMode behavior is set to Insert. I don't think this is possible because how do I access the selected items in the ListBox and include them as parameters to send to my stored proc with the rest of the form data?
Subject: Accesing Control In Form View
Name: SethuRaman
Date: 5/24/2007 2:50:14 AM
Comment:
It's Working Good. ThankYou
Subject: question
Name: Julie
Date: 11/8/2007 10:07:40 AM
Comment:
Do you know how to set the datasource and bind for a dropdownlist that is contained inside an edititemtemplate of a formview control. I need to do this at runtime.
Subject: formview
Name: jermaine
Date: 3/28/2008 4:09:36 AM
Comment:
first i created a asp webform whicho contains two dropdownlist. i would like to the selectedvalue of both the dropdownlist and place them as one string and then place it in a sql server database.
Subject: Thanks!
Name: Eric Barr
Date: 6/2/2008 9:57:02 AM
Comment:
I was struggling with this and the part that I didn't have was that I needed to use a FindControl call from the Row object. I did this in PageLoad and it worked. But only after I explicitly changed the mode of the FormView to edit mode like this:
MyFormView.ChangeMode(FormViewMode.Edit);



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008