具有转发器的ItemDataBound如何处理

时间:2013-03-19 15:17:10

标签: c# repeater

   protected void rptLastPromotion_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
                HtmlAnchor aView = (HtmlAnchor)e.Item.FindControl("aDescription");
                Label lbldescriptionlink = (Label)e.Item.FindControl("lblDescription");
                Label lbldescriptionNoLink = (Label)e.Item.FindControl("lblDescription2");
                HiddenField hfIsNewTab = (HiddenField)e.Item.FindControl("hfNewTab");

                for (int i = 0; i <= rptLastPromotion.Items.Count; i++)
                {
                    if (!String.IsNullOrEmpty(aView.HRef))
                    {
                        lbldescriptionlink.Visible = true;
                        lbldescriptionNoLink.Visible = false;
                        if (Convert.ToBoolean(hfIsNewTab.Value) == true)
                        {
                            aView.Target = "_blank";
                        }
                    }
                    else
                    {
                        lbldescriptionlink.Visible = false;
                        lbldescriptionNoLink.Visible = true;
                    }

                }

            }

我想处理并查看转发器中的项目,但我的代码中有错误。对此有何帮助?

2 个答案:

答案 0 :(得分:0)

您的问题非常模糊,但我相信您的问题可能是您没有检查转发器项目ItemType。这样做的标准方法是:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

此外,无需循环转发器的项目。 (for (int i = 0; i <= rptLastPromotion.Items.Count; i++))这就是ItemDataBound事件的用途。

所以你的代码现在看起来像这样。

protected void rptLastPromotion_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                HtmlAnchor aView = (HtmlAnchor)e.Item.FindControl("aDescription");
                Label lbldescriptionlink = (Label)e.Item.FindControl("lblDescription");
                Label lbldescriptionNoLink = (Label)e.Item.FindControl("lblDescription2");
                HiddenField hfIsNewTab = (HiddenField)e.Item.FindControl("hfNewTab");

                    if (!String.IsNullOrEmpty(aView.HRef))
                    {
                        lbldescriptionlink.Visible = true;
                        lbldescriptionNoLink.Visible = false;
                        if (Convert.ToBoolean(hfIsNewTab.Value) == true)
                        {
                            aView.Target = "_blank";
                        }
                    }
                    else
                    {
                        lbldescriptionlink.Visible = false;
                        lbldescriptionNoLink.Visible = true;
                    }

              }
            }

如果我不理解/回答您的问题,您可能需要更多详细信息和解释来扩展原始问题。

答案 1 :(得分:0)

您需要查看ItemTypeItemDataBoundRepeater事件。

protected void rptLastPromotion_ItemDataBound(object sender,System.Web.UI.WebControls.RepeaterItemEventArgs e)  
{  
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
    {
          // write your logic here             
    }
}