How do I bind an ASP.net ajax AccordionPane to an

2019-09-05 21:00发布

问题:

I've got an angry boss that will beat me down if I waste another day on this :-P Many karma points to the ajax guru who can solve my dilemma.

But more detail: I want to have an AccordionPane that grabs a bunch of links from an XML source and populate itself from said source.

回答1:

There might be a sexier way, but this works. Populate your data source however you wish. This was just for demo purposes. Ditto for PrettyTitle() Key is to remember there are two item types in the accordion.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Accordion Binding</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="AjaxScriptManager" runat="server">
    </asp:ScriptManager>
    <div>
        <cc1:Accordion ID="AccordionControl" runat="server" 
            onitemdatabound="AccordionControl_ItemDataBound">
            <Panes></Panes>
            <HeaderTemplate>
                <asp:Label ID="HeaderLabel" runat="server" />
            </HeaderTemplate>
            <ContentTemplate>
                <asp:Literal ID="ContentLiteral" runat="server" />
                <asp:HyperLink ID="ContentLink" runat="server" />
            </ContentTemplate>
        </cc1:Accordion><asp:xmldatasource runat="server" ID="RockNUGTwitter" ></asp:xmldatasource>
    </div>
    </form>
</body>

</html>

And codebehind is :

Using System;
using System.Web.UI.WebControls;
using System.Xml;

namespace Ajaxy
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Fill();
        }

        private void Fill()
        {

            PopulateDataSource();
            AccordionControl.DataSource = RockNUGTwitter.GetXmlDocument().SelectNodes("//item");
            AccordionControl.DataBind();

        }

        private void PopulateDataSource()
        {
            XmlDocument RockNugTwitterRSSDocument = new XmlDocument();
            RockNugTwitterRSSDocument.Load("http://twitter.com/statuses/user_timeline/15912811.rss");
            RockNUGTwitter.Data = RockNugTwitterRSSDocument.OuterXml;
        }

        protected void AccordionControl_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
        {
            XmlNode ItemNode = (XmlNode)e.AccordionItem.DataItem;
            if(e.AccordionItem.ItemType == AjaxControlToolkit.AccordionItemType.Content)
            {
                HyperLink ContentLink = (HyperLink)e.AccordionItem.FindControl("ContentLink");
                ContentLink.NavigateUrl = ItemNode.SelectSingleNode("link").InnerText;
                Literal ContentLiteral = (Literal)e.AccordionItem.FindControl("ContentLiteral");
                ContentLiteral.Text = ItemNode.SelectSingleNode("title").InnerText;
                ContentLink.Text = "Link";
            }
            else if(e.AccordionItem.ItemType == AjaxControlToolkit.AccordionItemType.Header)
            {
                Label HeaderLabel = (Label) e.AccordionItem.FindControl("HeaderLabel");
                HeaderLabel.Text = PrettyTitle(ItemNode.SelectSingleNode("title").InnerText);
            }
        }

        private string PrettyTitle(string FullItem)
        {
            string PrettyString = FullItem.Replace("RockNUG: ", "");
            string[] Words = PrettyString.Split(' ');
            const int MAX_WORDS_TOSHOW = 4;
            int WordsToShow = MAX_WORDS_TOSHOW;
            if(Words.Length < MAX_WORDS_TOSHOW)
            {
                WordsToShow = Words.Length;
            }
            PrettyString = String.Join(" ", Words, 0, WordsToShow);
            if (Words.Length > WordsToShow)
            {
                PrettyString += "...";
            }
            return PrettyString;
        }
    }
}