如何像占位符一样使用listitemCollection

时间:2009-04-05 18:31:17

标签: c# .net

我在Web用户控件中使用了ListItemCollection。我喜欢它!

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

请看! “ctrl.Items。”我用过它。但我想使用其他控件,如占位符,如:

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

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

为什么不绑定数据源?

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