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

                }

            }

我想处理和查看转发器中的项目,但我的代码中有错误。这有什么帮助吗?

4

2 回答 2

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

              }
            }

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

于 2013-03-19T16:52:57.860 回答
0

您需要检查 的ItemType内部ItemDataBound事件Repeater

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             
    }
}
于 2013-03-19T16:57:08.920 回答