使控件接受泛型 [英] Making a Control Accept Generics
问题描述
大家好,
我正在尝试为我一直使用的流程创建控件.基本上,我可以有一个可用对象的列表和一个当前使用的对象的列表.然后,我将对象从一个列表移到另一个列表,非常简单.我经常在不同类型的对象上使用此过程.我现在已经厌倦了重复工作,并决定创建一个我可以随意使用的控件.我知道是因为我将它与不同的对象类型一起使用,因此应该使用通用列表来填充控件上的列表框.但是,这就是我遇到的困难.我想在任何给定页面上设置此控件,并传入各种对象列表并使其起作用.下面是一些代码,但我无法弄清楚如何使控件接受未知的对象列表.
我什至还没有返回新的当前对象列表.任何帮助将不胜感激.谢谢.
埃里克
这就是我想将控件放置在某些页面上的方式.我会为这样的控件提供价值:
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)
{
//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]已添加代码块-OriginalGriff [/edit]
[edit]Code block added - OriginalGriff[/edit]
推荐答案
使用IList
,例如
UseIList
, 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;
}
}
这篇关于使控件接受泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!