如果是Repeater中的逻辑(aspx页面)

时间:2014-02-21 13:02:18

标签: asp.net vb.net webforms eval repeater

我的网站上有一个转发器,然后我需要执行以下代码:

<% if(Eval("TypeID") = 5) then %>
    <%# Html.BtnDigital(Eval("ID").ToString(),Eval("Title").ToString())%>
<% end if %>

基本上:如果数据绑定项的属性为5,我需要做一些特殊的事情。

我无法通过添加新属性来更改数据绑定项(不幸的是),我现在仅限于VB.NET。

当我没有if语句时,按钮显示正常,但是当我有if语句时,我收到以下错误(对不起Danish):

System.InvalidOperationException: Databindingmetoder som Eval(), XPath() og Bind() kan kun bruges i forbindelse med et databundet kontrolelement.
   ved System.Web.UI.Page.GetDataItem()
   ved System.Web.UI.TemplateControl.Eval(String expression)
   ved ASP.controls_guidesection_ascx.__Render__control19(HtmlTextWriter __w, Control parameterContainer) i C:\Users\larsholdgaard\Documents\Work\SAXO.com\Saxo.Websites.Base\src\Saxo.Website.Base\Controls\GuideSection.ascx:linje 75
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved ASP.views_masterpages_saxomasterpage_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) i C:\Users\larsholdgaard\Documents\Work\SAXO.com\Saxo.Websites.Base\src\Saxo.Website.Base\Views\Masterpages\SaxoMasterPage.Master:linje 74
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Control.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   ved System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
   ved System.Web.UI.Page.Render(HtmlTextWriter writer)
   ved System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   ved System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   ved System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

谢谢:)

2 个答案:

答案 0 :(得分:2)

解决此类情况的一个好方法是对Repeater的ItemDataBound事件作出反应。 检查CodeBehind中的条件要容易得多。

例如,如果您想根据项目的值更改ItemTemplate中控件的可见性,您可以添加以下代码(以下代码是一个示例,除其他外;我假设有ItemTemplate中ID为“txtValue”的TextBox:

Private Sub Repeater_OnItemDatabound(Object sender, RepeaterItemEventArgs e)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim myItem = TryCast(e.Item.DataItem, MyDataItemClass)
        Dim txtBox = TryCast(e.Item.FindControl("txtValue"), TextBox)
        If (txtBox IsNot Nothing) Then
            txtBox.Visible = myItem.TypeId = 5
           txtBox.Text = myItem.SomeInterestingText
        End If
    End If
End Sub

答案 1 :(得分:1)

您需要做的是检查ItemDataBound事件中的值。这些将在每个项目绑定时触发。在这甚至你做检查,如果你有匹配做你的改变。

请参阅http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemdatabound(v=vs.110).aspx

msdn页面中的示例还显示了如何使用它们来区分页眉和页脚行以及备用行模板。在创建每一行时会触发偶数。