我有一个通用列表,例如:
public class Person
{
public string Name {get;set;}
public string loginId {get;set;}
public string ContactNo {get;set;}
public string City {get;set;}
public string Country {get;set;}
}
填写列表后,我将列表绑定到Repeater控件。在我的转发器控件的HeaderTemplate中,我将LinkButton定义为:
<HeaderTemplate>
<tr>
<th width="100px">
<asp:LinkButton ID="lnk1" runat="server" CommandName="Sort" CommandArgument="Name" Text="Name"></asp:LinkButton>
</th>
<th width="150px">
<asp:LinkButton ID="lnk2" runat="server" CommandName="Sort" CommandArgument="loginId" Text="loginId"></asp:LinkButton>
</th>
<th width="100px">
<asp:LinkButton ID="lnk3" runat="server" CommandName="Sort" CommandArgument="City" Text="City"></asp:LinkButton>
</th>
</tr>
</HeaderTemplate>
当用户点击任何链接时,它会触发ItemCommand事件。在我的ItemCommand方法中,我检查命令,如果是排序,则根据CommandArgument
对列表进行排序。
这是我的命令事件:
protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Sort")
{
string str= e.CommandArgument.ToString();
//var lst=ListPerson.OrderBy(c=>c.[PropertyName from CommandArgument ????]);
var lst=ListPerson.OrderBy(?????)
}
}
NOTE:ListPerson is my previously populated list of type Person.
感谢任何帮助。