使用Listview ItemDataBound事件键入Casting问题

时间:2012-08-29 13:53:03

标签: asp.net c#-4.0

在页面加载事件我正在尝试

        protected void Page_Load(object sender, EventArgs e)
        {
     XmlReader reader = XmlReader.Create(FeedUrl);
                   SyndicationFeed feed = SyndicationFeed.Load(reader);

                   lstViewNewsFeeds.DataSource = feed.Items;
                   lstViewNewsFeeds.DataBind(); 
         }   

protected void lstViewNewsFeeds_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        try
        {
            // Instantiate listview control object.
            ListViewDataItem lvDataItem = (ListViewDataItem)e.Item;

            at this line given exception
            **DataRowView drvItem = (DataRowView)lvDataItem.DataItem;** 

            //Make sure that data item is binding with listview.
            if (lvDataItem.ItemType == ListViewItemType.DataItem)
            {

                SyndicationItem item = e.Item.DataItem as SyndicationItem;

                LinkButton linkButtonTitle = (LinkButton)e.Item.FindControl("lbNewsTitle");
                Panel panelNewsDescContent = (Panel)e.Item.FindControl("panelNewsDescription"); 

                // Make sure that linkButtonTile is referenced.
                if (linkButtonTitle != null)
                {
                   // linkButtonTitle.Text = Convert.ToString(item.Title);
                    linkButtonTitle.Text = Convert.ToString(drvItem["Title"]);
                }
                // Make sure that panel object is referenced.
                if (panelNewsDescContent != null)
                {
                    Label lblBox = new Label();
                   // lblBox.Text = Convert.ToString(item.Summary);
                    panelNewsDescContent.Controls.Add(lblBox);
                }

                //Label l = e.Row.FindControl("Label1") as Label;
                //l.Text = item.Title.Text;
                //HyperLink hl = e.Row.FindControl("HyperLink1") as HyperLink;
                //hl.Text = item.Title.Text;
                //hl.NavigateUrl = item.Links[0].Uri.AbsoluteUri;
            }
        }
        catch(Exception ee)
        {
            Response.Write(ee.Message);
        }

给出错误:

  

'System.Data.DataRowView'。无法转换类型的对象   键入'System.ServiceModel.Syndication.SyndicationItem'   'System.Data.DataRowView'。无法转换类型为

的对象

我无法理解我是如何删除此错误的。请帮帮我。

3 个答案:

答案 0 :(得分:0)

SyndicationFeed.Items Property会返回IEnumerable<SyndicationItem>,因此您应该将其转换为SyndicationItem

SyndicationItem item = (SyndicationItem)lvDataItem.DataItem;

答案 1 :(得分:0)

我试过这个工作正常。

 SyndicationItem item = e.Item.DataItem as SyndicationItem;  




 protected void lstViewNewsFeeds_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            try
            {
                // Instantiate listview control object.
                ListViewDataItem lvDataItem = (ListViewDataItem)e.Item;
                //DataRowView drvItem = (DataRowView)lvDataItem.DataItem; 
                //SyndicationItem item = (SyndicationItem)lvDataItem.DataItem;

                //Make sure that data item is binding with listview.
                if (lvDataItem.ItemType == ListViewItemType.DataItem)
                {

                    SyndicationItem item = e.Item.DataItem as SyndicationItem;

                    LinkButton linkButtonTitle = (LinkButton)e.Item.FindControl("lbNewsTitle");
                    Panel panelNewsDescContent = (Panel)e.Item.FindControl("panelNewsDescription"); 

                    // Make sure that linkButtonTile is referenced.
                    if (linkButtonTitle != null)
                    {
                       // linkButtonTitle.Text = Convert.ToString(item.Title);
                        linkButtonTitle.Text = Convert.ToString(item.Title.Text);
                    }
                    // Make sure that panel object is referenced.
                    if (panelNewsDescContent != null)
                    {
                        Label lblBox = new Label();
                        lblBox.Text = Convert.ToString(item.Summary);
                        panelNewsDescContent.Controls.Add(lblBox);
                    }

                    //Label l = e.Row.FindControl("Label1") as Label;
                    //l.Text = item.Title.Text;
                    //HyperLink hl = e.Row.FindControl("HyperLink1") as HyperLink;
                    //hl.Text = item.Title.Text;
                    //hl.NavigateUrl = item.Links[0].Uri.AbsoluteUri;
                }
            }
            catch(Exception ee)
            {
                Response.Write(ee.Message);
            }
}

顺便感谢大家的帮助。

答案 2 :(得分:0)

试试这个:

protected void lstViewNewsFeeds_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        try
        {
            //Make sure that data item is binding with listview.
            if (lvDataItem.ItemType == ListViewItemType.DataItem)
            {

                SyndicationItem item = e.Item.DataItem as SyndicationItem;

                LinkButton linkButtonTitle = (LinkButton)e.Item.FindControl("lbNewsTitle");
                Panel panelNewsDescContent = (Panel)e.Item.FindControl("panelNewsDescription"); 

                // Make sure that linkButtonTile is referenced.
                if (linkButtonTitle != null)
                {
                   linkButtonTitle.Text = Convert.ToString(item.Title);
                }
                // Make sure that panel object is referenced.
                if (panelNewsDescContent != null)
                {
                    Label lblBox = new Label();
                    lblBox.Text = Convert.ToString(item.Summary);
                    panelNewsDescContent.Controls.Add(lblBox);
                }

                Label l = e.Row.FindControl("Label1") as Label;
                l.Text = item.Title.Text;
                HyperLink hl = e.Row.FindControl("HyperLink1") as HyperLink;
                hl.Text = item.Title.Text;
                hl.NavigateUrl = item.Links[0].Uri.AbsoluteUri;
            }
        }
        catch(Exception ee)
        {
            Response.Write(ee.Message);
        }
}