2013-03-19 40 views
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; 
        } 

       } 

      } 

我想处理和查看中继器中的项目,但在我的代码中有错误。在这方面的任何帮助?ItemDataBound与中继器如何处理

回答

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

       } 
      } 

如果我不理解/回答你的问题,你可能想扩大你的原始问题,一些更多的细节和解释。

+0

此答案有帮助吗?你对此有任何疑问吗? – 2013-03-28 16:45:28

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    
    } 
} 
+1

他的代码中的for循环也会导致问题。 – 2013-03-19 18:23:21