Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databind Checkboxlist in Listview

Status
Not open for further replies.

stinkybee

Programmer
May 15, 2001
218
0
0
GB
I have a checkboxlist control in the insertitem template of a listview and so far I have been unable to populate it with values from my db. I can do it in the page_load event when the checkboxlist is outside the listview but have no idea how to achieve the same results when the checkboxlist is inside the listview. Here is the code for populating the checkboxlist outside of the listview control:

Code:
    protected void Page_Load(object sender, EventArgs e) 
    {
        if (!IsPostBack) 
        {
            SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["fmsConnectionString1"].ConnectionString);
            SqlCommand objCmd = new SqlCommand("select * from payment_methods", objConn);
            objConn.Open();

            cblPageLoad.DataSource = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
            cblPageLoad.DataBind();
            
        }
    }

Can anyone show me how to do the same but with the checkboxlist inside my listview control, specifically within the insertitem template.
 
you need to hook into the proper event (probably itemdatabinding, but it might be named somethingelse. in any case within that event find the control and bind to it.
Code:
 protected void myhandler(object sender, ListViewItemDataBoundEventArgs e)
{
   var cblPageLoad = e.FindControl("cblPageLoad") as CheckBoxList;
   if (cblPageLoad == null) return;

   using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["fmsConnectionString1"].ConnectionString))
   using(var command = connection.CreateCommand())
   {
      connection.Open();

      Command.CommandText = "select * from payment_methods";

      cblPageLoad.DataSource = command.ExecuteReader()
      cblPageLoad.DataBind();
   }
}
note the use of using for objects that implement IDisposable. This is key to proper resource management, especially in the case of an exception.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for the reply. I've tried hooking it up to the ItemDataBound event of my listview but I get this error:

Object reference not set to an instance of an object.

on this line

Code:
cblPageLoad.DataSource = command.ExecuteReader();

Any ideas on this?
 
did you check to make sure cblPageLoad is not null? item data bound is fired for every item, header, separator, item, footer etc. cblPageLoad only exists for itemtype of data/item.

your other option is to check the item type before finding the control.
Code:
if (e.Item.ItemType != ListViewItemType.Item) return;

var ctrl = e.Item.FindControl("my control") as CheckBoxList;
if(ctrl == null) return;

ctrl.DataSource = data;
ctrl.DataBind();

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top