【问题标题】:Passing the current Item to a function将当前项目传递给函数
【发布时间】:2013-07-09 13:13:45
【问题描述】:

我有一个中继器:

<asp:Repeater runat="server" ID="RepeaterCategorie">
    <ItemTemplate>
        <%#((isBlocked()) ? "true" : "false") %>
    </ItemTemplate>
</asp:Repeater>         

我在 .cs 上调用函数的地方。我想将当前项目(我的意思是,当前项目在数据源列表中迭代)传递给该函数。如果不通过 isBlocked 函数传递引用,我该怎么做?

【问题讨论】:

  • 您为什么要作为中继器的一部分这样做?为什么不在页面上显示之前做这个处理?
  • 因为属性不在我迭代的对象内。

标签: c# .net webforms repeater


【解决方案1】:

HTML

<asp:Repeater runat="server" ID="RepeaterCategorie"
    OnItemDataBound="RepeaterCategorie_ItemDataBound">
    <ItemTemplate>
        <asp:Label runat="server" Id="lblBool"></asp:Label>
    </ItemTemplate>
</asp:Repeater>  

CS

protected void RepeaterCategorie_ItemDataBound(
    object sender, RepeaterItemEventArgs e) 
{
    if (e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem) 
    {
        var lblBool = (Label)e.Item.FindControl("lblBool");
        lblBool.Text = isBlocked(sender, e) ? "true" : "false";
    }
} 

【讨论】:

  • 正如我所说,without passing the reference through the isBlocked function
  • @markzzz:抱歉,错过了那部分,因为没有什么意义;)您能否解释一下您为什么要这样做。这不仅仅是好奇,它可能会帮助我提供解决方案。
  • 比如在.cs上的函数上使用object sender, EventArgs e:它应该知道谁调用了那个函数并传递了对象!
  • @markzzz:使用itemdatabound 事件。我会说,这是你能得到的越近。
  • @markzzz:更新的答案。将sendere 传递给isBlocked,但我不是100% 如果这是你正在寻找的东西,因为你的动机仍然隐藏在我身上。
猜你喜欢
  • 2020-07-01
  • 1970-01-01
  • 2014-03-23
  • 2013-08-17
  • 1970-01-01
  • 2011-10-25
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
相关资源
最近更新 更多