页面中有5个panel,每个panel下有若干个textbox
 protected void Button1_Click(object sender, EventArgs e)
    {
        
        string stra = "NON!";
        foreach (Control p in this.Controls)
        {
            if (p is Panel)
            {
                
                stra = AllTextBox(p);
            }
        }
        Response.Write(stra);
    } protected string  AllTextBox(Control  ThePage)
    {
         return "OK";
    }
运行时不会调用AllTextBox(),所以总是输出NON!而不是OK,什么原因?

解决方案 »

  1.   

    你跟踪了吗?你把Response.Write(stra);放到foreach里面
      

  2.   

    stra = AllTextBox(p);这里做个断点,看看执行不
      

  3.   

    在winform + C#2005环境,测试了一下,代码没问题。private void button13_Click(object sender, EventArgs e)
    {
        string stra = "NON!";
        foreach (Control p in this.Controls)
        {
            if (p is Panel)
            {            stra = AllTextBox(p);
            }
        }
        
        MessageBox.Show(stra);}
    protected string AllTextBox(Control ThePage)
    {
        return "OK";
    }
      

  4.   

      改if     if(p.GetType().ToString   ()=="System.Web.UI.WebControls.Panel") 
      

  5.   

    这样试了一下:    {
            
            string str = "";
            foreach (Control p in this.Controls)
            {
                if (p is Panel)
                {                str = AllTextBox(p);
                    
                }
                Response.Write("there");
            }输出5个"there"(其实有6个panel),但是把Response.Write("there");放在if里则没有执行。
      

  6.   

    我试过了,代码没问题
    如果需要找Panel里的TextBox这样就找不到了,
    foreach (Control p in panel1.Controls)
                {
                    if (p is TextBox)
                    {
                        MessageBox.Show(p.Name);
                    }
                }
      

  7.   

    if (p is Panel) 改為
    if (p.GetType() == typeof(Panel))
      

  8.   

    这个或许会给你一点帮助
    http://user.qzone.qq.com/2399258/infocenter?ptlang=2052
      

  9.   

    彻底疯掉,新建了个网页,所有代码如下:using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;public partial class test03 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
            foreach ( Control c in this.Controls  )
            {
                Response.Write(c.GetType().ToString()+"<br>");
            }    }
    }
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test03.aspx.cs" Inherits="test03" %><!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 runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:Panel ID="Panel1" runat="server">
            </asp:Panel>
        
        </div>
        <asp:Panel ID="Panel2" runat="server">
        </asp:Panel>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        </form>
    </body>
    </html>
    结果是:
    System.Web.UI.LiteralControl
    System.Web.UI.HtmlControls.HtmlHead
    System.Web.UI.LiteralControl
    System.Web.UI.HtmlControls.HtmlForm
    System.Web.UI.LiteralControl
    压根就没Panel啊。而且怎么连form都没有?
      

  10.   

    在winform + C#2005环境,测试了一下,代码没问题。