使用两个Repeater
控制一个在另一个。一个显示字母,另一个显示基于第一个字符的国家。
会给你和例如在某一时刻(即熟:-))
好吧,我只是用一个DataList和Repeater只是有轻松多列。你可以用中继器做一些努力。
首先有您的标记如下
<asp:DataList ID="Categories" runat="server" ItemStyle-Width="150" RepeatColumns="4"
onitemdatabound="Categories_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="hlCategory" NavigateUrl="#" runat="server" ForeColor="#0099CC"><%# Container.DataItem %></asp:HyperLink><br />
<asp:Repeater ID="rptCountries" runat="server">
<ItemTemplate>
<asp:HyperLink ID="hlCountry" NavigateUrl="~/Categories.aspx?con=1&cou=1" runat="server"><%# Eval("Name") %></asp:HyperLink><br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:DataList>
那么后面
using System;
using System.Web.UI.WebControls;
using CountryDisplay;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
/// <summary>
/// Create a property to hold all your countries retrieved from the database
/// I assume you have you'll have one row for each country
/// </summary>
public CountryCollection AllCountries
{
get
{
if (ViewState["AllCountries"] != null)
{
return (CountryCollection)ViewState["AllCountries"];
}
return new CountryCollection();
}
set
{
ViewState["AllCountries"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAllCountriesFromDatabase();
char[] alphabet = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'S' }; // Get this from DB, I'm just mocking for illustration purpose.
Categories.DataSource = alphabet;
Categories.DataBind();
}
}
/// <summary>
/// Gets all Countries from database.
/// </summary>
private void GetAllCountriesFromDatabase()
{
AllCountries = new CountryCollection();
/* At this point you should know how to retrieve your data from DB and fill the AllCountries collection
E.g.
AllCountries = DalService.GetAllCountriesFromDatabase(); // DalService could be your Data Access layer and GetAllCountriesFromDatabase() is one of it's methods
I'll be creating some dummy logic to fill the collection for demo purpose from this point onwards
*/
// Add countries to the collection
Country country = new Country("America");
country.ID = 1;
AllCountries.Add(country);
country = new Country("Australia");
country.ID = 2;
AllCountries.Add(country);
country = new Country("Sri Lanka");
country.ID = 3;
AllCountries.Add(country);
country = new Country("India");
country.ID = 4;
AllCountries.Add(country);
country = new Country("Canada");
country.ID = 5;
AllCountries.Add(country);
}
protected void Categories_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the hlCategory control in the current DataListItem.
char cCategory = (char)e.Item.DataItem;
Repeater rptCountries = (Repeater)e.Item.FindControl("rptCountries");
if (!char.IsWhiteSpace(cCategory) && rptCountries != null)
{
rptCountries.DataSource = AllCountries.FindAll(a => a.Name.StartsWith(cCategory.ToString()));
rptCountries.DataBind();
}
}
}
}
}
的代码和一些模型类
using System;
using System.Collections.Generic;
namespace CountryDisplay
{
[Serializable]
public class CountryCollection : List<Country> { }
[Serializable]
public class Country
{
public int ID { get; set; }
public string Name { get; set; }
public Country(string name)
{
this.ID = 0;
this.Name = name;
}
}
}
希望这有助于!
接受并投票答案,如果它有帮助。
来源
2014-09-29 06:02:51
Sam
查看更新后的答案。添加从数据库中检索数据的逻辑。我只是嘲笑了数据的插图。 – Sam 2014-09-29 07:26:55
非常感谢你sam。我很感激。 – Sylar 2014-09-30 14:00:55
不用担心。如果它有帮助,请给一个投票。干杯! – Sam 2014-09-30 22:41:47