I'd like to interrogate a web form to check for values of the controls on
the form. The following codes worked when I execute it in a User Control that
has some web controls on it but I can't seem to get it to work directly on
the form itself.
using System.Web;
using System.Web.UI;
using System.Web.UI,WebControls;
foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
do something
else if (ctrl is DropDownList)
do something else
}
When the above code is done at the Page_Load() of the aspx file, the
this.Controls return System.Web.UI.LiteralControl, System.Web.UI.HTMLControls
etc in the Controls collection but not the Textboxes and other Web Controls.
Is this correct or have I done something wrong?
It'd be greatly appreciated if someone out there can enlighten me please.
KDHi Calvin,
In "this" case, "this" is a reference to the Page itself. The Form is a
Control IN the Page. All Controls inside the Form are inside the Form's
Controls Collection, not the Page's.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
"Calvin KD" <CalvinKD@.discussions.microsoft.com> wrote in message
news:2DC1CD94-23FC-47AF-BF7B-8F6C75C0BA88@.microsoft.com...
> Hi everyone,
> I'd like to interrogate a web form to check for values of the controls on
> the form. The following codes worked when I execute it in a User Control
> that
> has some web controls on it but I can't seem to get it to work directly on
> the form itself.
> using System.Web;
> using System.Web.UI;
> using System.Web.UI,WebControls;
> foreach (Control ctrl in this.Controls)
> {
> if (ctrl is TextBox)
> do something
> else if (ctrl is DropDownList)
> do something else
> }
> When the above code is done at the Page_Load() of the aspx file, the
> this.Controls return System.Web.UI.LiteralControl,
> System.Web.UI.HTMLControls
> etc in the Controls collection but not the Textboxes and other Web
> Controls.
> Is this correct or have I done something wrong?
> It'd be greatly appreciated if someone out there can enlighten me please.
> KD
Thanks so much Kevin. Is this by design or is it a bug by MS? What's really
frustrating is that the Watch Windows does not have the correct Controls
collection so I could not find out that there exists a "second level" and
this is certainly not the case for Windows forms and may be VB.Net ?
"Kevin Spencer" wrote:
> Hi Calvin,
> In "this" case, "this" is a reference to the Page itself. The Form is a
> Control IN the Page. All Controls inside the Form are inside the Form's
> Controls Collection, not the Page's.
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> Ambiguity has a certain quality to it.
> "Calvin KD" <CalvinKD@.discussions.microsoft.com> wrote in message
> news:2DC1CD94-23FC-47AF-BF7B-8F6C75C0BA88@.microsoft.com...
> > Hi everyone,
> > I'd like to interrogate a web form to check for values of the controls on
> > the form. The following codes worked when I execute it in a User Control
> > that
> > has some web controls on it but I can't seem to get it to work directly on
> > the form itself.
> > using System.Web;
> > using System.Web.UI;
> > using System.Web.UI,WebControls;
> > foreach (Control ctrl in this.Controls)
> > {
> > if (ctrl is TextBox)
> > do something
> > else if (ctrl is DropDownList)
> > do something else
> > }
> > When the above code is done at the Page_Load() of the aspx file, the
> > this.Controls return System.Web.UI.LiteralControl,
> > System.Web.UI.HTMLControls
> > etc in the Controls collection but not the Textboxes and other Web
> > Controls.
> > Is this correct or have I done something wrong?
> > It'd be greatly appreciated if someone out there can enlighten me please.
> > KD
>
0 comments:
Post a Comment