form id, this.Controls ... ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

While writing code for creating aspx controls dynamically, I noticed some
strange (at least to me) behavior.
Look at this code;

// FreeQuery : System.Web.UI.Page
// void cmdDo_Click(...)

GridView grid = new GridView();
grid.DataSource = source;
this.Controls.Add(grid); // HERE

The last line fails during execution!
But if I type like so;

//...
form1.Controls.Add(grid);

.... where form1 is generated in FreeQuery.aspx file;

....
<form id="form1" runat="server">
....

no failure will happen.

-------------------------
So what happens?
Why does this.Controls.Add fail?
What's the difference between this and form1?
....

Could you please share your knowledge with us.

Thanks in advance.
 
Hello Arman,

Because "this" has the type of "ASP._Default" and not of your form

AS> Hi,
AS>
AS> While writing code for creating aspx controls dynamically, I noticed
AS> some
AS> strange (at least to me) behavior.
AS> Look at this code;
AS> // FreeQuery : System.Web.UI.Page
AS> // void cmdDo_Click(...)
AS> GridView grid = new GridView();
AS> grid.DataSource = source;
AS> this.Controls.Add(grid); // HERE
AS> The last line fails during execution!
AS> But if I type like so;
AS> //...
AS> form1.Controls.Add(grid);
AS> ... where form1 is generated in FreeQuery.aspx file;
AS>
AS> ...
AS> <form id="form1" runat="server">
AS> ...
AS> no failure will happen.
AS>
AS> -------------------------
AS> So what happens?
AS> Why does this.Controls.Add fail?
AS> What's the difference between this and form1?
AS> ...
AS> Could you please share your knowledge with us.
AS>
AS> Thanks in advance.
AS>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hello Michael ,

"ASP.<yourFormName_aspx>"

mistyped first time

MN> Hello Arman,
MN>
MN> Because "this" has the type of "ASP._Default" and not of your form
MN>
AS>> Hi,
AS>>
AS>> While writing code for creating aspx controls dynamically, I
AS>> noticed
AS>> some
AS>> strange (at least to me) behavior.
AS>> Look at this code;
AS>> // FreeQuery : System.Web.UI.Page
AS>> // void cmdDo_Click(...)
AS>> GridView grid = new GridView();
AS>> grid.DataSource = source;
AS>> this.Controls.Add(grid); // HERE
AS>> The last line fails during execution!
AS>> But if I type like so;
AS>> //...
AS>> form1.Controls.Add(grid);
AS>> ... where form1 is generated in FreeQuery.aspx file;
AS>> ...
AS>> <form id="form1" runat="server">
AS>> ...
AS>> no failure will happen.
AS>> -------------------------
AS>> So what happens?
AS>> Why does this.Controls.Add fail?
AS>> What's the difference between this and form1?
AS>> ...
AS>> Could you please share your knowledge with us.
AS>> Thanks in advance.
AS>>
MN> ---
MN> WBR,
MN> Michael Nemtsev :: blog: http://spaces.live.com/laflour
MN> "At times one remains faithful to a cause only because its opponents
MN> do not cease to be insipid." (c) Friedrich Nietzsche
MN>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top