Hi All,
I’m trying to create a control for a process I use all the time. Basically, I can have a list of available objects and a list of currently used objects. I then move objects from list to list, very simple. I use this process a lot with varying types of objects. I’m now tired of duplicating my work and have decided to create a control I can just use at will. I figure because I use this with varying object type it should use generic lists to populate the listboxes on the control. But, this is where I’m having difficulties. I want to set this control on any giving page and pass in various object lists and have it work. Below is some code but I cannot figure out how to make the control accept an unknown object list.
I have not even gotten to returning the new current object list. Any help would be greatly appreciated. Thanks.
Eric
This is how I would like to place the control on some page. I’d supply value to a control like this:
<xxx:AvailCurLists ID="ACM1" runat="server" Name="Etester"
DataText="PropertyName"
DataValue="PropertyID"
CurrentAssignments='<%#CurrentAssignments() %>'
AvailableAssignments='<%#AvailableAssignments() %>' />
On the control side thing look like this:
<div class="label"><%#Eval("Name")%></div>
<br />
<table>
<tr align="center">
<td>
<asp:Label ID="lblAvailable" runat="server" CssClass="programsLabel" Text="Available Malls"></asp:Label>
</td>
<td></td>
<td></td>
<td></td>
<td>
<asp:Label ID="lblCurrent" runat="server" CssClass="programsLabel" Text="Current Malls"></asp:Label>
</td>
</tr>
<tr align="center">
<td>
<asp:ListBox ID="lbAvailable" runat="server"
CssClass="items" SelectionMode="Multiple"
Height="200px" DataTextField='<%#DataText %>'
DataValueField='<%#DataValue %>' DataSource='<%#AvailableAssignments %>'>
</asp:ListBox>
</td>
<td></td>
<td></td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add Value -->" OnClick="btnAdd_Click" />
<br />
<br />
<asp:Button ID="btnRemove" runat="server" Text="<-- Remove Value" OnClick="btnRemove_Click" />
</td>
<td>
<asp:ListBox ID="lbCurrent" runat="server"
CssClass="items" SelectionMode="Multiple"
Height="200px" DataTextField='<%#DataText %>'
DataValueField='<%#DataValue %>' DataSource='<%#CurrentAssignments %>'>
</asp:ListBox>
</td>
</tr>
</table>
Code Behind so far:
public partial class AvailableCurrent : System.Web.UI.UserControl
{
private string _name = null;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private string _dataText = null;
public string DataText
{
get
{
return _dataText;
}
set
{
_dataText = value;
}
}
private string _dataValue = null;
public string DataValue
{
get
{
return _dataValue;
}
set
{
_dataValue = value;
}
}
private List<T> _AvailableAssignments = null;
public List<T> AvailableAssignments
{
get
{
return _AvailableAssignments;
}
set
{
_AvailableAssignments = value;
}
}
private List<T> _CurrentAssignments = null;
public List<T> CurrentAssignments
{
get
{
return _CurrentAssignments;
}
set
{
_CurrentAssignments = value;
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (lbAvailable.SelectedItem != null)
{
var items = (from ListItem li in lbAvailable.Items
where li.Selected == true
select li).ToList();
foreach (ListItem li in items)
{
lbCurrent.Items.Add(new ListItem(li.Text, li.Value));
lbAvailable.Items.Remove(li);
}
}
}
protected void btnRemove_Click(object sender, EventArgs e)
{
if (lbCurrent.SelectedItem != null)
{
var mItems = (from ListItem li in lbCurrent.Items
where li.Selected == true
select li).ToList();
foreach (ListItem li in mItems)
{
lbAvailable.Items.Add(new ListItem(li.Text, li.Value));
lbCurrent.Items.Remove(li);
}
var items = (from ListItem li in lbAvailable.Items
orderby li.Text
select li).ToList();
lbAvailable.Items.Clear();
foreach (ListItem li in items)
lbAvailable.Items.Add(new ListItem(li.Text, li.Value));
}
}
}
[edit]Code block added - OriginalGriff[/edit]