form id, this.Controls ... ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arman Sahakyan

    #1

    form id, this.Controls ... ?

    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.P age
    // void cmdDo_Click(... )

    GridView grid = new GridView();
    grid.DataSource = source;
    this.Controls.A dd(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.A dd fail?
    What's the difference between this and form1?
    ....

    Could you please share your knowledge with us.

    Thanks in advance.


    --
    ======
    Arman
  • Michael Nemtsev

    #2
    Re: form id, this.Controls ... ?

    Hello Arman,

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

    ASHi,
    AS>
    ASWhile writing code for creating aspx controls dynamically, I noticed
    ASsome
    ASstrange (at least to me) behavior.
    ASLook at this code;
    AS// FreeQuery : System.Web.UI.P age
    AS// void cmdDo_Click(... )
    ASGridView grid = new GridView();
    ASgrid.DataSour ce = source;
    ASthis.Controls .Add(grid); // HERE
    ASThe last line fails during execution!
    ASBut if I type like so;
    AS//...
    ASform1.Control s.Add(grid);
    AS... where form1 is generated in FreeQuery.aspx file;
    AS>
    AS...
    AS<form id="form1" runat="server">
    AS...
    ASno failure will happen.
    AS>
    AS-------------------------
    ASSo what happens?
    ASWhy does this.Controls.A dd fail?
    ASWhat's the difference between this and form1?
    AS...
    ASCould you please share your knowledge with us.
    AS>
    ASThanks 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


    Comment

    • Michael Nemtsev

      #3
      Re: form id, this.Controls ... ?

      Hello Michael ,

      "ASP.<yourFormN ame_aspx>"

      mistyped first time

      MNHello Arman,
      MN>
      MNBecause "this" has the type of "ASP._Defau lt" 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.P age
      AS>// void cmdDo_Click(... )
      AS>GridView grid = new GridView();
      AS>grid.DataSou rce = source;
      AS>this.Control s.Add(grid); // HERE
      AS>The last line fails during execution!
      AS>But if I type like so;
      AS>//...
      AS>form1.Contro ls.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.A dd 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---
      MNWBR,
      MNMichael Nemtsev :: blog: http://spaces.live.com/laflour
      MN"At times one remains faithful to a cause only because its opponents
      MNdo 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 Nietzsche


      Comment

      Working...