如何将xml文件节点绑定到datalist中的linkbutton [英] how to bind xml file nodes to linkbutton in datalist

查看:66
本文介绍了如何将xml文件节点绑定到datalist中的linkbutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码





 protected void BindDataList()
{
// ------------------------------从XML中回复所有相册------------ ------------ //
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath(〜/+XML / album.xml));
XmlNodeList nodelist = xmldoc.GetElementsByTagName(Album);
ArrayList listItems = new ArrayList();
//获取XML
中的所有专辑(int i = 0; i< nodelist.Count; i ++)
{
string anode = nodelist [i] .Attributes [ 名称]值。

listItems.Add(阳极);
}
dtlist.DataSource = listItems;
dtlist.DataBind();

}

// aspx页面(设计页面)
< asp:DataList ID =dtlistrunat =serverRepeatColumns =3CellPadding = 10 >
< itemtemplate>
< asp:LinkBut​​ton ID =LinkBut​​ton3runat =serverCommandArgument ='<%#((System.Xml.XmlNode)Container.DataItem).Attributes [Name]。Value%> 'text ='<%#((System.Xml.XmlNode)Container.DataItem).Attributes [Name]。Value%>'>

< / itemtemplate>







调试时显示错误在链接按钮的设计视图中

//这是我得到的错误

无法将'System.String'类型的对象强制转换为'System.Xml'。 XmlNode'。

//我哪里出错了,请你帮忙

解决方案

这给出了正确的输出.. 。





 受保护  void  BindDataList()
{


DataTable dt = new DataTable();

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath( 〜/ + XML / album.xml));
XmlNodeList nodelist = xmldoc.GetElementsByTagName( Album);
ArrayList listItems = new ArrayList();
dt.Columns.Add( 名称 typeof string ));
// 获取XML中的所有相册

for int i = 0 ; i < nodelist.Count; i ++)
{
DataRow dtrow = dt.NewRow();
dtrow [ 名称] = nodelist [i] .Attributes [ 名称]。值;

dt.Rows.Add(dtrow);
}

dtlist.DataSource = dt;
dtlist.DataBind();
}



// aspx页



 <   asp:DataList     ID   =  dtlist    runat   =  server    RepeatColumns   =  3    CellPadding   =  5 >  
< ItemTemplate >

< asp:LinkBut​​ton ID = LinkBut​​ton3 runat = 服务器 CommandArgument =' <% #Eval( 名称%> ' 文字 =' < ;% #Eval( 名称%> ' > < ; / asp:LinkBut​​ton >

< / ItemTemplate >
< span class =code-keyword>< ItemStyle BorderColor = Brown BorderStyle = dotted BorderWidth = 3px Horizo​​ntalAlign = 中心 VerticalAlign = Bottom / >
< / asp:DataList >


here is my code


  protected void BindDataList()
    {
        //------------------------------Retrive all Albums from XML ------------------------//
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/" + "XML/album.xml"));
        XmlNodeList nodelist = xmldoc.GetElementsByTagName("Album");
        ArrayList listItems = new ArrayList();
        //fetch all albums in XML
        for (int i = 0; i < nodelist.Count; i++)
        {
            string anode = nodelist[i].Attributes["Name"].Value;

            listItems.Add(anode);
        }
        dtlist.DataSource = listItems;
        dtlist.DataBind();

     }

//aspx page(design page)
    <asp:DataList ID="dtlist" runat="server" RepeatColumns="3" CellPadding="5">
                    <itemtemplate>
                      <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%#((System.Xml.XmlNode)Container.DataItem).Attributes["Name"].Value %>' Text='<%#((System.Xml.XmlNode)Container.DataItem).Attributes["Name"].Value%>'>
                       
                    </itemtemplate>




while debugging its shows me error in desgin view for linkbutton
// This is the error i get
Unable to cast object of type 'System.String' to type 'System.Xml.XmlNode'.
//where i am going wrong, can u please help

解决方案

This give proper output...


protected void BindDataList()
{


DataTable dt = new DataTable();
     
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/" + "XML/album.xml"));
        XmlNodeList nodelist = xmldoc.GetElementsByTagName("Album");
        ArrayList listItems = new ArrayList();
        dt.Columns.Add("Name", typeof(string));
        //fetch all albums in XML

        for (int i = 0; i < nodelist.Count; i++)
        {
            DataRow dtrow = dt.NewRow();
            dtrow["Name"] = nodelist[i].Attributes["Name"].Value;
          
            dt.Rows.Add(dtrow);
        }
       
        dtlist.DataSource = dt;
        dtlist.DataBind();
}


// aspx page

<asp:DataList ID="dtlist" runat="server" RepeatColumns="3" CellPadding="5">
                <ItemTemplate>

                <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%#Eval("Name") %>' Text='<%#Eval("Name") %>'></asp:LinkButton>

                </ItemTemplate>
                <ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" />
            </asp:DataList>


这篇关于如何将xml文件节点绑定到datalist中的linkbutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆