Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

XML
<xxx:AvailCurLists ID="ACM1" runat="server" Name="Etester"
                             DataText="PropertyName"
                             DataValue="PropertyID"
                             CurrentAssignments='<%#CurrentAssignments() %>'
                             AvailableAssignments='<%#AvailableAssignments() %>' />


On the control side thing look like this:

XML
<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)
        {
            //multi-move...
            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);
            }

            //sorting...
            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]
Posted
Updated 28-Mar-11 4:40am
v2

1 solution

Use IList, eg.
private IList _AvailableAssignments = null;
        public IList AvailableAssignments
        {
            get
            {
                return _AvailableAssignments;
            }
            set
            {
                _AvailableAssignments = value;
            }
        }

        private IList _CurrentAssignments = null;
        public IList CurrentAssignments
        {
            get
            {
                return _CurrentAssignments;
            }
            set
            {
                _CurrentAssignments = value;
            }
        }
 
Share this answer
 
Comments
ehwash 28-Mar-11 16:49pm    
I've tried something like this along the way:

private IList<t> _AvailableAssignments = null;
public IList<t> AvailableAssignments
{
get
{
return _AvailableAssignments;
}
set
{
_AvailableAssignments = value;
}
}

private IList<t> _CurrentAssignments = null;
public IList<t> CurrentAssignments
{
get
{
return _CurrentAssignments;
}
set
{
_CurrentAssignments = value;
}
}

I keep getting the Error message "The type or namespace name 'T' could not be found..." I cannot get the compiler to recognize the type <t>.

I noticed you don't have a type in the IList. I cannot get this to work either.
Xmen Real 28-Mar-11 21:21pm    
"I keep getting the Error message "The type or namespace name 'T' could not be found..." I cannot get the compiler to recognize the type ."
how you setting those properties ?

"I noticed you don't have a type in the IList. I cannot get this to work either."

why not ?

you can get type like this

IList list = new List<string>();
Type t = list.GetType();
Type t2 = typeof(List<string>);
if (t == t2)
{ }//matched
ehwash 28-Mar-11 21:56pm    
Sorry I printed the reply wrong. It should have been as follows:

private IList <T> _AvailableAssignments = null;
public IList <T> AvailableAssignments...

This is where the error message appear upon compile (for the IList<T>). At work tomorrow once back at work I try to incorporate a string for T. Thanks for the help.

Edit: Wow it took me a minute to figure out how to post "<T>"
Xmen Real 31-Mar-11 8:32am    
why you writing <T>...simply use System.Collection.IList
ehwash 1-Apr-11 12:09pm    
I've been busy working on other things, so, I'm just getting back to this. So all I got to say is... Thanks X W.K. that's what I looking for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900