【发布时间】:2016-09-06 17:46:05
【问题描述】:
在知道如何通过 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 = "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