2

我遇到了一个问题,即我将文本框动态添加到placedHolder。

占位符声明:

<asp:PlaceHolder ID="phTextBoxes" runat="server" >
  </asp:PlaceHolder>

文本框的分配

 ...if (phTextBoxes!= null) phTextBoxes.Controls.Add(txt);

但是当我构建应用程序时,我得到了错误

当前上下文中不存在名称“phTextBoxes” 我已通过使用FindControl()解决了该问题,如下所示。

PlaceHolder phtxt = (PlaceHolder)form1.FindControl("phTextBoxes");

然后将控件添加到 phtxt。错误消失了。但我想知道为什么旧的 placeHolder 会出错以及 FindControl 是如何找到它的。

4

1 回答 1

0

As @GrahamClark mentioned before, you could be missing something. There's no way not to see phTextBoxes in code behind unless it is inside another control, such as GridView, etc.. Make sure your place holder is not inside any other control.

Another thing, on the top of your Designer window (aspx or ascx file), make sure you have referenced the related class name in CodeBehind/*CodeFile*, and Inherits if necessary.

Example1: I have a usercontrol called UC1, in the ascx file I should declare the class name as follows:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Cart.ascx.cs"
 Inherits="UserControls_UC1" %>

Example2: I have a ASPX page called Default, in the aspx file I should declare the class name as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
 Inherits="_Default" %>
于 2012-04-05T11:32:21.860 回答