使用 FindControl 定位 Literal

发布于 2024-12-25 02:38:37 字数 1629 浏览 6 评论 0原文

我有一个名为“orderReceiptTable”的 ListView,我可以从代码隐藏中正确访问它。其中有一个名为“orgName”的文字,我显然想用组织的名称填充它。

经过大量搜索后,确定 FindControl 是正确的做法。也许我使用 FindControl 的方式不正确,但我无法真正让它“找到”我的文字控件。

代码块在页面加载中被调用。

我的代码如下所示:

    Dim orgNameString As String = getOrganizationName.getOrgName(organizationID).ToString()

    Dim myOrgName As Literal = FindControl("orgName")
    myOrgName = CType(orderReceiptTable.FindControl("orgName"), Literal)

    If Not (myOrgName Is Nothing) Then
        Response.Write("I found the control!")
        myOrgName.Text = orgNameString
    End If

这是 .aspx 文件中的标记:

<asp:ListView ID="orderReceiptTable" runat="server">
            <LayoutTemplate>
                <div runat="server" id="itemPlaceholder" />
            </LayoutTemplate>

            <EmptyDataTemplate>
                <tr id="noDataDiv" runat="server"> 
                    <td class="sub" ID="itemPlaceholder" runat="server">
                        No order data was returned.
                    </td>
                </tr> 
            </EmptyDataTemplate>

            <ItemTemplate>
                <div id="itemPlaceholder" runat="server" style="border:solid 1px #000000; width:250px; float:left; padding:10px; border:solid 2px #1664B1;">
                    <div>Organization Name: <asp:Literal runat="server" ID="orgName"></asp:Literal></div>                             
                </div>
            </ItemTemplate>        
    </asp:ListView>   

I have a ListView called "orderReceiptTable" which I am able to properly access from the Code Behind. Within it is a literal called "orgName" which I obviously would like to populate with an organization's name.

After much searching it was determined that FindControl was the right course of action. Perhaps I am using FindControl improperly but I am unable to actually have it "find" my Literal control.

The code block is being called in the Page Load.

My code looks as such:

    Dim orgNameString As String = getOrganizationName.getOrgName(organizationID).ToString()

    Dim myOrgName As Literal = FindControl("orgName")
    myOrgName = CType(orderReceiptTable.FindControl("orgName"), Literal)

    If Not (myOrgName Is Nothing) Then
        Response.Write("I found the control!")
        myOrgName.Text = orgNameString
    End If

Here is the mark-up in the .aspx file:

<asp:ListView ID="orderReceiptTable" runat="server">
            <LayoutTemplate>
                <div runat="server" id="itemPlaceholder" />
            </LayoutTemplate>

            <EmptyDataTemplate>
                <tr id="noDataDiv" runat="server"> 
                    <td class="sub" ID="itemPlaceholder" runat="server">
                        No order data was returned.
                    </td>
                </tr> 
            </EmptyDataTemplate>

            <ItemTemplate>
                <div id="itemPlaceholder" runat="server" style="border:solid 1px #000000; width:250px; float:left; padding:10px; border:solid 2px #1664B1;">
                    <div>Organization Name: <asp:Literal runat="server" ID="orgName"></asp:Literal></div>                             
                </div>
            </ItemTemplate>        
    </asp:ListView>   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2025-01-01 02:38:37

仅在将一些数据绑定到模板后才会创建模板内的控件。然后,您将能够通过 ListView.Controls 属性访问它。

之前的答案可能会有所帮助:在 ListView EmptyDataTemplate 中查找控件

The controls inside the template will only be created after binding some data to it. You will then be able to access it via the ListView.Controls property.

This previous answer might help: Find control in ListView EmptyDataTemplate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文