Read input value from dynamic created input controls

Discussion in 'ASP .Net' started by Melissa, Dec 25, 2008.

  1. Melissa

    Melissa Guest

    Hi all,

    I need help on reading input value from dynamic created input controls. What
    is the control ID to use after input is posted back to the server? My code
    below does not get anything although the input values were displayed
    correctly in the Textbox on the screen after posted back.

    Thanks in advanced!

    Here is the sample code:

    if (!IsPostBack)
    {
    TextBox oTextBox;
    for (int i=0; i<5; i++)
    {
    oTextBox = new TextBox();
    oTextBox.ID = "test" + i.ToString();
    Form.Contronls.Add(oTextBox);
    }
    }
    else
    {
    for (int i=0; i<5; i++)
    {
    // Request Does NOT find these fields???
    Response.Write(Request["test" + i.ToString()].ToString());
    }
    }
     
    Melissa, Dec 25, 2008
    #1
    1. Advertising

  2. Melissa

    Melissa Guest

    Yes, Mark. They are in Page_Load. I will try Page_Init. Thanks you.


    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Melissa" <> wrote in message
    > news:...
    >
    > > Here is the sample code:

    >
    > I'm presuming that the code is in Page_Load...? If so, move it to
    > Page_Init()
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    >
     
    Melissa, Dec 25, 2008
    #2
    1. Advertising

  3. Hi Melissa,

    > I need help on reading input value from dynamic created input controls.
    > What
    > is the control ID to use after input is posted back to the server? My code
    > below does not get anything although the input values were displayed
    > correctly in the Textbox on the screen after posted back.
    >
    > Thanks in advanced!
    >
    > Here is the sample code:
    >
    > if (!IsPostBack)
    > {
    > TextBox oTextBox;
    > for (int i=0; i<5; i++)
    > {
    > oTextBox = new TextBox();
    > oTextBox.ID = "test" + i.ToString();
    > Form.Contronls.Add(oTextBox);
    > }
    > }



    You should not add the controls like this.
    Create a class-level variable of type TextBox and override the method
    CreateChildControls()

    Example:

    public class MyPageOrCustomControl
    {
    private System.Collections.Generic.List<TextBox> tboxes = new
    System.Collections.Generic.List<TextBox>();

    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    DoMyWork();
    }

    private void DoMyWork()
    {
    foreach(TextBox tb in tboxes)
    {
    Response.Write(tb.ID);
    }
    }

    protected override void CreateChildControls()
    {
    for(int i = 0; i < 5; i++)
    {
    tbox = new TextBox();
    tbox.ID = "test" + i.toString();
    Controls.Add(tbox);
    }
    }

    }


    --
    Happy Hacking,
    Gaurav Vaish
    http://blogs.mastergaurav.com
    http://eduzine.edujini-labs.com
    ---------------------------
     
    Gaurav Vaish \(a.k.a. MasterGaurav\), Dec 26, 2008
    #3
    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. =?Utf-8?B?U2hpanUgUG95aWxpbA==?=

    Problem with Event handlers for Dynamic created Controls in DatGri

    =?Utf-8?B?U2hpanUgUG95aWxpbA==?=, May 10, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    603
  2. Ronald
    Replies:
    2
    Views:
    849
    Robbe Morris [C# MVP]
    Jan 17, 2006
  3. =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?=

    Linking events from controls, when the controls have been created

    =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?=, Jul 16, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    591
    =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?=
    Jul 16, 2007
  4. abhishek dave via .NET 247

    dynamic loaing of the controls on the dynamically created page

    abhishek dave via .NET 247, Jun 16, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    212
    Victor Garcia Aprea [MVP]
    Jun 17, 2004
  5. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    469
    Eidolon
    May 4, 2004
Loading...

Share This Page