【问题标题】:How to find VB.Net WebForms components by ID or part of it?如何通过 ID 或部分 ID 查找 VB.Net WebForms 组件?
【发布时间】:2016-09-06 17:46:05
【问题描述】:

在知道如何通过 ID 获取组件方面需要帮助,我有一个循环,其中包含一些名为 TextBox_1TextBox_2TextBox_3、(...) 的元素。

我根据从数据库返回的数据列表创建了一个循环。我尝试了类似的东西

Dim count as Integer = 1
Dim TextBox As TextBox = Nothing

    For Each dados In MyListData

       TextBox = CType(Me.FindControl("TextBox_" & count), TextBox)
       TextBox.Text = "My data"

       count = count + 1
    Next

TextBox.Text = "My data" 中触发了错误,显示 TextBox 是未定义的对象。在即时视图中返回 Nothing

我的场景是一个使用 MasterPage 的表单,其中包含它的 ContentPlaceholders 和一些 UpdatePanels 组件。我的文本框在 Web 表单“内容”内,它对应于 Master 中设置的主要内容 ContentPlaceHolder .. 一开始我以为我可以用 Me.FindControl 找到元素(如上).. 失败了,我尝试了Me.Form、Me.Page 和 Me.Control 但什么都没有..

MasterPage 是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
     <!-- My header.. -->
</head>
<body onkeydown="return(event.keyCode!=13);">
    <%--<% If (DesignMode) Then%>
        <script src="Scripts/ASPxScriptIntelliSense.js" type="text/javascript"></script>
    <% End If%>--%>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="900">
        </asp:ScriptManager>
        <div id="buttons">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:ContentPlaceHolder ID="Header" runat="server">
                    </asp:ContentPlaceHolder>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        <div id="container">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:ContentPlaceHolder ID="Content" runat="server">
                    </asp:ContentPlaceHolder>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

表单标记的结构如下:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MyProject_Master.Master" 
CodeBehind="Page.aspx.vb" Inherits="MyProject.MyForm" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
    <!-- Some static HTML -->
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">
    <!-- The structure is like: -->
    <table cellpadding="1" cellspacing="1" class="divLargura">
        <tr>
            <td><asp:Label ID="Label_1" runat="server"></asp:Label></td>
            <td>
                <asp:TextBox ID="TextBox_1" runat="server" ReadOnly="true" Width="97%" Height="18px" ClientIDMode="Static">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td><asp:Label ID="Label_2" runat="server"></asp:Label></td>
            <td>
                <asp:TextBox ID="TextBox_2" runat="server" ReadOnly="true" Width="97%" Height="18px" ClientIDMode="Static">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td><asp:Label ID="Label_3" runat="server"></asp:Label></td>
            <td>
                <asp:TextBox ID="TextBox_3" runat="server" ReadOnly="true" Width="97%" Height="18px" ClientIDMode="Static">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td><asp:Label ID="Label_4" runat="server"></asp:Label></td>
            <td>
                <asp:TextBox ID="TextBox_4" runat="server" ReadOnly="true" Width="97%" Height="18px" ClientIDMode="Static">
                </asp:TextBox>
            </td>
        </tr>
    </table>
</asp:Content>

我做错了什么?和 ReadOnly 有关系吗?

【问题讨论】:

    标签: vb.net webforms components


    【解决方案1】:

    Me.FindControl() 使用Me 对象(整个表单)作为搜索的起点。这在数据绑定控件中不起作用,在该控件中,每条记录可能都有不同的控件实例。您必须在特定行的上下文中进行搜索,这仅在某些事件中才有可能。

    要为此提供最佳解决方案,我们需要更多地了解您的上下文...此代码在哪里运行,您的 ASPX 标记中的 TextBox 是如何定义的,以及 listaEvolucaoAcao 与最终在控件中的数据?


    仍然没有足够的信息,但如果是我,我会让内容区域看起来更像这样:

    <asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">
    <table cellpadding="1" cellspacing="1" class="divLargura">
    <asp:Repeater runat="server" ID="Largura"  ... >
    <ItemTemplate>
        <tr>
            <td><asp:Label ID="rowLabel" runat="server"></asp:Label></td>
            <td><asp:TextBox ID="rowTextBox" runat="server" ReadOnly="true"
                 Value='<%# Eval("Item.PropertyName") %>' 
                 Width="97%" Height="18px"></asp:TextBox>
            </td>
        </tr>
    </ItemTemplate>
    </asp:Repeater>
    </table>
    </asp:Content>
    

    然后我会将MyListData 对象设置为转发器的数据源。

    【讨论】:

    • 感谢@Joel 的反馈! PS:我现在在示例中将我的数据列表名称更改为“MyListData”,认为这听起来更好。它是一个使用 MasterPage 的 Web 表单。 MasterPage 包含
      并将它的 ContentPlaceholders 与一些 UpdatePanels 组件结合起来。我的文本框在 Web 表单“内容”内,它对应于 Master 中设置的主要内容 ContentPlaceHolder .. 一开始我以为我可以用 Me.FindControl 找到元素.. 失败了,我用 Me.Form 尝试了, Me.Page 和 Me.Control。希望这可以清除场景。谢谢!
    • 太好了...现在编辑问题以显示所有信息。我们需要这里的全貌。
    • 感谢@Joel Coehoorn,现在更新了,希望能更准确的详细说明问题。
    • 仍然需要了解原始 VB.Net 代码所在的方法。
    猜你喜欢
    • 2015-05-17
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2021-09-04
    相关资源
    最近更新 更多