为什么要使用的FindControl ...? [英] Why to use FindControl...?

查看:121
本文介绍了为什么要使用的FindControl ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着我在那里动态地添加文本框到placedHolder的问题。

I was facing an issue where i was adding textboxes dynamically to a placedHolder.

占位符声明:

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

文本框的分配

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

但是,当我建立我得到错误的应用程序

But when i build the application i got the error

phTextBoxes'这个名字并不在目前的情况下存在
我以解决问题的的FindControl()象下面这样。

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

,然后加控制phtxt。
错误已经一去不复返了。
但我想知道为什么是旧的占位符给了一个错误,并做了怎样的FindControl找到了。

and then added control to phtxt. The error is gone. But i would like to know why was the old placeHolder giving an error and how did the FindControl found it.

推荐答案

由于@GrahamClark前面提到的,你可能会失去了一些东西。有没有办法不看code phTextBoxes背后,除非它是另一个控件内部,如GridView控件等。确保你的地方持有人没有任何其他的控制中。

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.

另一件事,你的设计器窗口(ASPX或ASCX文件)的顶部,请确保您已在 codeBehind / *的 codeFILE引用的相关类名的*,和继承如果必要的。

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.

例1:我叫用户控件的 UC1 ,在ASCX文件,我应该申报类名如下:

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" %>

例2:我有一个叫ASPX页面默认,在aspx文件,我应该如下声明类名称:

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" %>

这篇关于为什么要使用的FindControl ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆