Link to home
Start Free TrialLog in
Avatar of SegFault
SegFault

asked on

ListBox datasource works fine in Win Forms, but Broken in Web Forms

Hi all.

I have the following problem

I have a listbox which is bound to a datatable with two columns(about 25 rows).

In C# win forms, it works just like it says it should, but if i move the code to Web Forms, the listbox comes back empty. I have checked, and the table definitely has data.

I'm new to Web Forms though, so its entirely possible I'm missing something obvious.

Any hints?

-------------------------
Win Forms Code
-------------------------
      try {
        this.dirsearchGroups.SearchRoot = de;
        SearchResultCollection rs1 = this.dirsearchGroups.FindAll();
        dtGroups = new DataTable("groups");
        dtGroups.Columns.Add("group", System.Type.GetType("System.String"));
        dtGroups.Columns.Add("path", System.Type.GetType("System.String"));

        foreach(SearchResult rs in rs1) {
          dtGroups.Rows.Add(new object[2] {rs.Properties["cn"][0].ToString(), rs.Path.ToString()});
        }
        this.lbGroups.DataSource = dtGroups;
        this.lbGroups.DisplayMember = "group";
        this.lbGroups.ValueMember = "path";
snip--->

**WORKS LIKE A CHARM**

------------------------------
Web Forms
------------------------------
      dirsearchGroups.SearchRoot = root;
      dirsearchGroups.Filter = "(&(objectClass=group)(cn=*))";
      dirsearchGroups.PropertiesToLoad.Add("cn");

      SearchResultCollection rs1 = this.dirsearchGroups.FindAll();
      dtGroups = new DataTable("groups");
      dtGroups.Columns.Add("group", System.Type.GetType("System.String"));
      dtGroups.Columns.Add("path", System.Type.GetType("System.String"));


      foreach(SearchResult rs in rs1) {
        dtGroups.Rows.Add(new object[2] {rs.Properties["cn"][0].ToString(), rs.Path.ToString()});
      }
      Response.Write("dtGroups count " + dtGroups.Rows.Count.ToString());

      this.lbGroups.DataSource = dtGroups;
      this.lbGroups.DataTextField = "group";
      this.lbGroups.DataValueField = "path";

**THIS RETURNS AND EMPTY "ListBox" (select box on the HTML rendered form)

Can somebody clue me in please?  I'm sure its something I'm not doing..
ASKER CERTIFIED SOLUTION
Avatar of RiverGuy
RiverGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SegFault
SegFault

ASKER

Here is how I did it

I'm using a DirectoryResults collection, so I just iterated through that collection and added the items.

foreach(SearchResult rs in rsCollection) {
  lbGroups.Items.Add(new ListItem(rs.Properties["cn"][0].ToString(), rs.Path.ToString());
}

worked like a charm.