如何按ID或部分ID查找VB.NETWebForms组件?

如何按ID或部分ID查找VB.NETWebForms组件?,vb.net,webforms,components,Vb.net,Webforms,Components,需要帮助的家伙知道如何通过ID获取组件,我有一个循环,其中包含一些元素,例如TextBox_1,TextBox_2,TextBox_3,(…) 我在数据库返回的数据列表中做了一个循环。我试过类似的东西 Dim count as Integer = 1 Dim TextBox As TextBox = Nothing For Each dados In MyListData TextBox = CType(Me.FindControl("TextBox_" & c

需要帮助的家伙知道如何通过ID获取组件,我有一个循环,其中包含一些元素,例如
TextBox_1
TextBox_2
TextBox_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=“我的数据”
中触发,显示TextBox是一个未定义的对象。。在即时视图中不返回任何内容

我的场景是一个使用母版页的表单,其中包含和它的内容占位符以及一些UpdatePanel组件。我的文本框位于Web表单“Content”中,该表单对应于主控中设置的主内容ContentPlaceHolder。。一开始我以为我可以找到我的元素。FindControl(如上所述)。。我失败了,我试着用我。表单,我。页面和我。控制,但什么也没有

母版页类似于:

<!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有任何关系吗?

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

为了提供最好的解决方案,我们需要更多地了解您的上下文。。。此代码在何处运行,文本框如何在ASPX标记中定义,以及
listaevolucoacao
如何与控件中的数据关联


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

<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”,这样听起来更好。这是一个使用母版页的Web表单。母版页包含和它的内容占位符以及一些UpdatePanel组件。我的文本框位于Web表单“Content”中,该表单对应于主控中设置的主内容ContentPlaceHolder。。一开始我以为我可以找到元素。FindControl。。我失败了,我试着用我。表单,我。页面和我。控件。希望这能消除这种情况。谢谢伟大的现在编辑问题以显示所有信息。我们需要这里的完整图片。感谢@Joel Coehoorn,它现在已经更新,希望它能够更准确地帮助详细解决问题。仍然需要了解原始VB.Net代码所在的方法。