Mysql 如何从数据库中检索数据以实现建议的布局?

Mysql 如何从数据库中检索数据以实现建议的布局?,mysql,asp.net,Mysql,Asp.net,我正在用ASP.NET建立一个网站。 有一个页面,用户可以选择国家/地区。 这是截图 我将国家存储在mysql表中。但在这个页面中,我使用了这样的超链接 <asp:Literal ID="Literal2" runat="server" Text="<b><font size=3 color=green>A</font></b>"></asp:Literal><br /> <asp:HyperLink ID

我正在用ASP.NET建立一个网站。 有一个页面,用户可以选择国家/地区。 这是截图

我将国家存储在mysql表中。但在这个页面中,我使用了这样的超链接

<asp:Literal ID="Literal2" runat="server" Text="<b><font size=3 color=green>A</font></b>"></asp:Literal><br />
<asp:HyperLink ID="HyperLink1" NavigateUrl="~/Categories.aspx?con=1&cou=1"    runat="server">Algeria</asp:HyperLink><br />
<asp:HyperLink ID="HyperLink2"  NavigateUrl="~/Categories.aspx?con=1&cou=2" runat="server">Angola</asp:HyperLink><br />
所以为了实现这个布局,我使用了上面的方法。我不能直接从表中加载它们并生成上面的布局,而不是使用上述方法吗?。我尝试使用树状视图控件。但该控件不会给出上述格式。我需要那个专栏布局。如果您建议我使用gridview或一些表控件,请给我一些示例。我不知道怎样才能做到这一点

请记住,我想将每个名称与绿色字母分开,例如:A表示名称以该特定字母开头。

使用两个中继器控件,一个在另一个中。一个用于显示字母表,另一个用于根据第一个字符显示国家

在烹饪过程中,我会给你一个简单的例子:-

好的,我只是使用了一个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;
        }
    }
}
希望这有帮助


如果答案有帮助,请接受并投票。

查看更新后的答案。添加从数据库检索数据的逻辑。我只是模仿数据来说明。非常感谢山姆。我很感激。