【问题标题】:How can I put my OnItemDataBound c# code in my code behind and bind it to the asp:repeater如何将我的 OnItemDataBound c# 代码放在后面的代码中并将其绑定到 asp:repeater
【发布时间】:2015-07-24 12:50:41
【问题描述】:

我是asp.net 的初学者,这是我第一次遇到asp 中继器。 它是 .ascx 页面中的中继器控件,我想绑定到 C# 代码

在 .ascx 页面中:

<asp:Repeater ID="relatedFilesRepeater" runat="server" OnItemDataBound="RelatedFilesRepeater_ItemDataBound">
      <ItemTemplate>
            <tr>
                <td width="30%">
                       <asp:Label ID="fileNameLabel" runat="server" />
                 </td>
                 <td>
                       <asp:ImageButton ID="signatureButton" runat="server" ImageUrl="~/Images/Icons/16x16/_Base Image/Symbol Check 2.png" />
                  </td>
             </tr>
       </ItemTemplate>
</asp:Repeater>

在我的 .ascx 页面顶部,我已经在 C# 中声明了我的 OnItemDatabound 函数,如下所示:

<script language="C#" runat="server">
     void RelatedFilesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
...
}

而且效果很好。但是将 c# 代码留在 .ascx 中既不美观也不干净 如何将我的OnItemDataBound 函数放在我的代码后面并将其与asp 转发器绑定。因为我试图简单地将我的 c# 代码复制粘贴到文件后面的代码中,但它不起作用。

【问题讨论】:

  • “不工作”是什么意思?
  • 可以将此功能移动到cs文件,执行此操作并解释什么不起作用并显示.ascx文件的标题
  • 我的 .ascx 页面中使用了一些标签,例如 fileNameLabel 和 fileName,但它们在我的代码隐藏页面中无法识别。所以编译时会出错。
  • 您无法直接访问这些标签。原因是它们被包裹在中继器中,因此首先您必须找到正确的中继器行,然后使用 row.findcontrol 并提供 ctrl id。
  • 好的。但您没有提到该行表示 ctrl 的父级。

标签: c# asp.net asprepeater


【解决方案1】:

您无法访问标签控件的原因是它位于转发器内部。因此,为了访问它们,您可以使用FindControl()

var myLabel = (Label)FindControl("YouLabelId");
//Do Some Work

这将适用于 &lt;asp:Repeater /&gt; 中的其余控件

延伸阅读here

【讨论】:

  • 我会试试的。非常感谢!
【解决方案2】:

您无法直接访问这些标签。原因是它们被包裹在中继器中,所以首先你必须找到正确的中继器行,然后使用 row.findcontrol 并给出 ctrl id:

 void RelatedFilesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label fileNameLabel= (Label)e.Item.FindControl("fileNameLabel");
ImageButton signatureButton= (ImageButton)e.Item.FindControl("signatureButton");
}

【讨论】:

    猜你喜欢
    • 2020-02-19
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2013-01-16
    • 1970-01-01
    相关资源
    最近更新 更多