about FindControl ---> to find the control added by program

Discussion in 'ASP .Net' started by Edward, Jun 14, 2004.

  1. Edward

    Edward Guest

    hi, everyone,
    my Page_Load fill tblProducts with some TextBox.

    <form runat="server">
    <asp:Table id="tblProducts" runat="server"></asp:Table>
    <hr>
    <asp:Button id="Button1" onclick="Button1_Click" runat="server"
    Text="Button"></asp:Button>
    <hr>
    <hr>
    <asp:TextBox id="test" runat="server"/>

    </form>

    the method is :

    TextBox getTextBox() {
    TextBox txt = new TextBox();
    txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
    TextBoxCnt ++;

    return txt;
    }


    when I use :
    TextBox txt = (TextBox)Page.FindControl("txtNum" + Convert.ToString(i));

    in the script of Button_Click, I cannot find the control. but in
    Page_Load , I can get it.

    And in Button_Click, TextBox "test" can be find.

    How can I find the program-added controls in Button_Click?

    thanks.
     
    Edward, Jun 14, 2004
    #1
    1. Advertising

  2. Edward,

    My guess is that you may be adding the controls only on page load and not
    post back.

    For dynamically added controls to be accessible on post back they must be
    added to the page again.

    I have an example of this (in VB.Net, but you should get the idea) on my web
    site, www.aboutfortunate.com, just go to the code library (link on the top
    right of page) and search for "Dynamically add text boxes".

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Edward" <> wrote in message
    news:e4GN$...
    > hi, everyone,
    > my Page_Load fill tblProducts with some TextBox.
    >
    > <form runat="server">
    > <asp:Table id="tblProducts" runat="server"></asp:Table>
    > <hr>
    > <asp:Button id="Button1" onclick="Button1_Click" runat="server"
    > Text="Button"></asp:Button>
    > <hr>
    > <hr>
    > <asp:TextBox id="test" runat="server"/>
    >
    > </form>
    >
    > the method is :
    >
    > TextBox getTextBox() {
    > TextBox txt = new TextBox();
    > txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
    > TextBoxCnt ++;
    >
    > return txt;
    > }
    >
    >
    > when I use :
    > TextBox txt = (TextBox)Page.FindControl("txtNum" +

    Convert.ToString(i));
    >
    > in the script of Button_Click, I cannot find the control. but in
    > Page_Load , I can get it.
    >
    > And in Button_Click, TextBox "test" can be find.
    >
    > How can I find the program-added controls in Button_Click?
    >
    > thanks.
    >
    >
    >
     
    S. Justin Gengo, Jun 14, 2004
    #2
    1. Advertising

  3. Hi,

    A pretty good article is given in Microsoft KB "HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET",

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;317794

    Hope this link will be useful for u.

    bye,

    Regards,
    Kamal T.

    "Edward" wrote:

    > hi, everyone,
    > my Page_Load fill tblProducts with some TextBox.
    >
    > <form runat="server">
    > <asp:Table id="tblProducts" runat="server"></asp:Table>
    > <hr>
    > <asp:Button id="Button1" onclick="Button1_Click" runat="server"
    > Text="Button"></asp:Button>
    > <hr>
    > <hr>
    > <asp:TextBox id="test" runat="server"/>
    >
    > </form>
    >
    > the method is :
    >
    > TextBox getTextBox() {
    > TextBox txt = new TextBox();
    > txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
    > TextBoxCnt ++;
    >
    > return txt;
    > }
    >
    >
    > when I use :
    > TextBox txt = (TextBox)Page.FindControl("txtNum" + Convert.ToString(i));
    >
    > in the script of Button_Click, I cannot find the control. but in
    > Page_Load , I can get it.
    >
    > And in Button_Click, TextBox "test" can be find.
    >
    > How can I find the program-added controls in Button_Click?
    >
    > thanks.
    >
    >
    >
    >
     
    =?Utf-8?B?S2FtYWwgVC4=?=, Jun 14, 2004
    #3
  4. Edward

    Edward Guest

    Thanks, Justin.

    You are right. I put the code outside "postback=false".

    although I got the result, but I was confused about the mechanism, How can
    the textbox's value be refreshed when the second-Page_load runs ?

    Thanks from heart.

    "S. Justin Gengo" <> дÈëÏûÏ¢ÐÂÎÅ
    :...
    > Edward,
    >
    > My guess is that you may be adding the controls only on page load and not
    > post back.
    >
    > For dynamically added controls to be accessible on post back they must be
    > added to the page again.
    >
    > I have an example of this (in VB.Net, but you should get the idea) on my

    web
    > site, www.aboutfortunate.com, just go to the code library (link on the top
    > right of page) and search for "Dynamically add text boxes".
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzsche
    > "Edward" <> wrote in message
    > news:e4GN$...
    > > hi, everyone,
    > > my Page_Load fill tblProducts with some TextBox.
    > >
    > > <form runat="server">
    > > <asp:Table id="tblProducts" runat="server"></asp:Table>
    > > <hr>
    > > <asp:Button id="Button1" onclick="Button1_Click" runat="server"
    > > Text="Button"></asp:Button>
    > > <hr>
    > > <hr>
    > > <asp:TextBox id="test" runat="server"/>
    > >
    > > </form>
    > >
    > > the method is :
    > >
    > > TextBox getTextBox() {
    > > TextBox txt = new TextBox();
    > > txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
    > > TextBoxCnt ++;
    > >
    > > return txt;
    > > }
    > >
    > >
    > > when I use :
    > > TextBox txt = (TextBox)Page.FindControl("txtNum" +

    > Convert.ToString(i));
    > >
    > > in the script of Button_Click, I cannot find the control. but in
    > > Page_Load , I can get it.
    > >
    > > And in Button_Click, TextBox "test" can be find.
    > >
    > > How can I find the program-added controls in Button_Click?
    > >
    > > thanks.
    > >
    > >
    > >

    >
    >
     
    Edward, Jun 14, 2004
    #4
  5. Edward,

    Each control's properties are gathered upon page load. .NET runs through
    each control on page load and fills it's properties from the post, but it
    only loops through controls that exist. So if a control isn't recreated the
    control is never matched up with the post from the client.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Edward" <> wrote in message
    news:...
    > Thanks, Justin.
    >
    > You are right. I put the code outside "postback=false".
    >
    > although I got the result, but I was confused about the mechanism, How can
    > the textbox's value be refreshed when the second-Page_load runs ?
    >
    > Thanks from heart.
    >
    > "S. Justin Gengo" <> дÈëÏûÏ¢ÐÂÎÅ
    > :...
    > > Edward,
    > >
    > > My guess is that you may be adding the controls only on page load and

    not
    > > post back.
    > >
    > > For dynamically added controls to be accessible on post back they must

    be
    > > added to the page again.
    > >
    > > I have an example of this (in VB.Net, but you should get the idea) on my

    > web
    > > site, www.aboutfortunate.com, just go to the code library (link on the

    top
    > > right of page) and search for "Dynamically add text boxes".
    > >
    > > --
    > > Sincerely,
    > >
    > > S. Justin Gengo, MCP
    > > Web Developer / Programmer
    > >
    > > www.aboutfortunate.com
    > >
    > > "Out of chaos comes order."
    > > Nietzsche
    > > "Edward" <> wrote in message
    > > news:e4GN$...
    > > > hi, everyone,
    > > > my Page_Load fill tblProducts with some TextBox.
    > > >
    > > > <form runat="server">
    > > > <asp:Table id="tblProducts" runat="server"></asp:Table>
    > > > <hr>
    > > > <asp:Button id="Button1" onclick="Button1_Click"

    runat="server"
    > > > Text="Button"></asp:Button>
    > > > <hr>
    > > > <hr>
    > > > <asp:TextBox id="test" runat="server"/>
    > > >
    > > > </form>
    > > >
    > > > the method is :
    > > >
    > > > TextBox getTextBox() {
    > > > TextBox txt = new TextBox();
    > > > txt.ID = "txtNum" + Convert.ToString(TextBoxCnt);
    > > > TextBoxCnt ++;
    > > >
    > > > return txt;
    > > > }
    > > >
    > > >
    > > > when I use :
    > > > TextBox txt = (TextBox)Page.FindControl("txtNum" +

    > > Convert.ToString(i));
    > > >
    > > > in the script of Button_Click, I cannot find the control. but in
    > > > Page_Load , I can get it.
    > > >
    > > > And in Button_Click, TextBox "test" can be find.
    > > >
    > > > How can I find the program-added controls in Button_Click?
    > > >
    > > > thanks.
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    S. Justin Gengo, Jun 14, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    526
    John M Deal
    Nov 8, 2004
  2. =?Utf-8?B?V2ViTWF0cml4?=
    Replies:
    1
    Views:
    1,697
    Teemu Keiski
    Feb 14, 2006
  3. Craig Buchanan
    Replies:
    4
    Views:
    1,659
    Craig Buchanan
    Jul 10, 2008
  4. Craig Buchanan

    FindControl in a gridview's dynamically-added template

    Craig Buchanan, Jul 7, 2008, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    1,012
    Craig Buchanan
    Jul 10, 2008
  5. Craig Buchanan

    FindControl in a gridview's dynamically-added template

    Craig Buchanan, Jul 7, 2008, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    242
    Craig Buchanan
    Jul 10, 2008
Loading...

Share This Page