如何将 ASP.net ajax AccordionPane 绑定到 XMLDatasource?

发布于 2024-07-06 05:23:58 字数 127 浏览 6 评论 0原文

我有一个愤怒的老板,如果我在这上面再浪费一天,他就会打我:-P 许多业力都指向 ajax 大师,他可以解决我的困境。

但更详细的是:我想要一个 AccordionPane,它从 XML 源获取一堆链接并从所述源填充自身。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2024-07-13 05:23:58

可能有更性感的方式,但这行得通。 按照您的意愿填充您的数据源。 这仅用于演示目的。 PrettyTitle() 也是如此,关键是要记住手风琴中有两种项目类型。

<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>

隐藏代码是:

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;
        }
    }
}

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文