页面中有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,什么原因?
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,什么原因?
{
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";
}
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里则没有执行。
如果需要找Panel里的TextBox这样就找不到了,
foreach (Control p in panel1.Controls)
{
if (p is TextBox)
{
MessageBox.Show(p.Name);
}
}
if (p.GetType() == typeof(Panel))
http://user.qzone.qq.com/2399258/infocenter?ptlang=2052
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都没有?