1

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

        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”类型。无法转换类型的对象

我无法理解如何消除此错误。请在这里帮助我。

4

3 回答 3

0

i have tried this works fine.

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

By The way thanks all of you for help.

于 2012-08-29T14:08:50.963 回答
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);
        }
}
于 2012-08-29T14:09:39.173 回答
0

SyndicationFeed.Items属性返回一个IEnumerable<SyndicationItem>,因此您应该将其转换为SyndicationItem

SyndicationItem item = (SyndicationItem)lvDataItem.DataItem;
于 2012-08-29T13:58:35.343 回答