Accessing different controls inside the FormView control

FormView is a template databound control which can contain other controls inside it. It is a common practice to access different controls inside the FormView control. In the code below I have populated the DropDownList, CheckBoxList and the ListBox control and all of these controls reside inside the FormView control. Take a look at the complete code below:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!Page.IsPostBack)
        {
            BindData();        
        }
    }

    
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(); 

        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();

        CheckBoxList checkboxList = (CheckBoxList)row.FindControl("cblNames");
        checkboxList.DataSource = GetNames();
        checkboxList.DataTextField = "FirstName";
        checkboxList.DataValueField = "UserID";
        checkboxList.DataBind();

        ListBox lbList = (ListBox)row.FindControl("lbNames");
        lbList.DataSource = GetNames();
        lbList.DataTextField = "FirstName";
        lbList.DataValueField = "UserID";
        lbList.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; 
    }



    
private string ConnectionString
    {
        
get return @"Server=localhost;Database=School;
        Trusted_Connection=true"; }
    }

    
protected void btnSelect_Click(object sender, EventArgs e)
    {
        DisplaySelectedDropDownListValue();
        DisplaySelectedCheckBoxListValues();
        DisplaySelectedListBoxValues(); 

    }

    
private void DisplaySelectedListBoxValues()
    {
        
string genString = String.Empty; 

        FormViewRow row = fv1.Row;

        ListBox lb = (ListBox)row.FindControl("lbNames");

        
foreach (ListItem item in lb.Items)
        {
            
if (item.Selected)
            {
                genString += item.Text + ",";
            }
        }

        Label3.Text = genString.TrimEnd(
','); 
    }

    
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(
','); 
    }

    
private void DisplaySelectedDropDownListValue()
    {
        FormViewRow row = fv1.Row;

        DropDownList ddl = (DropDownList)row.FindControl("ddlNames");
        Label1.Text = ddl.SelectedItem.Text; 
    }


}

powered by IMHO 1.3

Print | posted @ Monday, May 22, 2006 11:34 PM

Twitter