tags:

views:

41

answers:

1

I used ListItemCollection in Web User Control. And I like it!

sample.asx

public ListItemCollection Items {
  get {
    return this.ListBox1.Items;
  }
}

TestForSampleascx.aspx

void Add(WebUserControls.Control3 ctrl1, WebUserControls.Control3 ctrl2) {
  ListBox listbox = ctrl1.FindControl("ListBox1") as ListBox;
  if (ctrl1.Items.Count > 0) {
    foreach (ListItem li in listbox.Items) {
      if (li.Selected)
        ctrl2.Add(li.Text, li.Value);
    }
  }
}

void Remove(WebUserControls.Control3 ctrl) {
  ListBox listbox = ctrl.FindControl("ListBox1") as ListBox;
  if (ctrl.Items.Count > 0) {
    int count = ctrl.Items.Count;
    for (int i = count - 1; i > -1; i--) {
      if (listbox.Items[i].Selected)
        ctrl.Remove(listbox.Items[i].Value);
    }
  }
}

Look please! "ctrl.Items." I used it. But I want to use other control such as placeholder like:

public PlaceholderItemCollection Items {
  get {
    return this.Placeholder.Items;
  }
}

or

public PlaceholderItemCollection Controls {
  get {
    return this.Placeholder.Controls;
  }
}

How can I do that?

A: 

Why not just bind the datasource?

DataTable dt = logic_to_get_data();
ListBox1.DataSource = dt;
ListBox1.DataBind();
tsilb