control not found on postback

Discussion in 'ASP .Net' started by Igor Belagorudsky, Jul 29, 2003.

  1. Hi,

    i am trying to analyze data submitted in a form but the problem is that when
    i try to create the controls in code (which is what i want to do), it throws
    an obect not found exception on postback when i try to Page.FindControl:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (Page.IsPostBack)
    {

    Debug.WriteLine(((WebControl)Page.FindControl("testBox_1")).Attributes["hell
    o"]);
    }
    else
    {
    TextBox txt = new TextBox();
    txt.ID = "testBox_1";
    txt.Attributes.Add("hello", "hi");
    Page.FindControl("test").Controls.Add(txt);
    }
    }

    where control "test" is the form that i am submitting and all im trying to
    do is to debug.writeline "hi" on postback.
    on the other hand, if i manually put a text box in the aspx page and give it
    a hello=hi attribute, everything works like it should.
    can someone please tell me what the problem is?

    thanks,
    igor
     
    Igor Belagorudsky, Jul 29, 2003
    #1
    1. Advertising

  2. Igor Belagorudsky

    bruce barker Guest

    you have to create the dynamic control on postback also. also for it to have
    access to its postback data, you should create it during onint, as form load
    is too late, the datapostback event has already fired.

    -- bruce (sqlwork.com)


    "Igor Belagorudsky" <> wrote in message
    news:#...
    > Hi,
    >
    > i am trying to analyze data submitted in a form but the problem is that

    when
    > i try to create the controls in code (which is what i want to do), it

    throws
    > an obect not found exception on postback when i try to Page.FindControl:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (Page.IsPostBack)
    > {
    >
    >

    Debug.WriteLine(((WebControl)Page.FindControl("testBox_1")).Attributes["hell
    > o"]);
    > }
    > else
    > {
    > TextBox txt = new TextBox();
    > txt.ID = "testBox_1";
    > txt.Attributes.Add("hello", "hi");
    > Page.FindControl("test").Controls.Add(txt);
    > }
    > }
    >
    > where control "test" is the form that i am submitting and all im trying to
    > do is to debug.writeline "hi" on postback.
    > on the other hand, if i manually put a text box in the aspx page and give

    it
    > a hello=hi attribute, everything works like it should.
    > can someone please tell me what the problem is?
    >
    > thanks,
    > igor
    >
    >
     
    bruce barker, Jul 29, 2003
    #2
    1. Advertising

  3. hi,

    thanks for your response, but i have a more complicated structure
    unfortunately...

    i dont need to display the same page on postback. i have a sort of
    questionnaire setup where my page checks what questions to ask in the db,
    creates appropriate form elements and waits for submission. then it writes
    down answers and displays the next questions in the same page. so i really
    need the data from my dynamic control without having to recreate it on the
    page.. is this possible and if so at which point would i be able to check
    its data? oninit?

    thank you,
    igor

    "bruce barker" <> wrote in message
    news:O0NE%...
    > you have to create the dynamic control on postback also. also for it to

    have
    > access to its postback data, you should create it during onint, as form

    load
    > is too late, the datapostback event has already fired.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Igor Belagorudsky" <> wrote in message
    > news:#...
    > > Hi,
    > >
    > > i am trying to analyze data submitted in a form but the problem is that

    > when
    > > i try to create the controls in code (which is what i want to do), it

    > throws
    > > an obect not found exception on postback when i try to Page.FindControl:
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > if (Page.IsPostBack)
    > > {
    > >
    > >

    >

    Debug.WriteLine(((WebControl)Page.FindControl("testBox_1")).Attributes["hell
    > > o"]);
    > > }
    > > else
    > > {
    > > TextBox txt = new TextBox();
    > > txt.ID = "testBox_1";
    > > txt.Attributes.Add("hello", "hi");
    > > Page.FindControl("test").Controls.Add(txt);
    > > }
    > > }
    > >
    > > where control "test" is the form that i am submitting and all im trying

    to
    > > do is to debug.writeline "hi" on postback.
    > > on the other hand, if i manually put a text box in the aspx page and

    give
    > it
    > > a hello=hi attribute, everything works like it should.
    > > can someone please tell me what the problem is?
    > >
    > > thanks,
    > > igor
    > >
    > >

    >
    >
     
    Igor Belagorudsky, Jul 29, 2003
    #3
  4. Igor Belagorudsky

    MS News Guest

    Igor,

    I am having the same problem here.
    I am going to Try this:
    on Page postback recreate the same dynamic controls first
    Process the results
    then create the new questions
    response

    I know this is inefficient but what else to do other than some java script??

    Let us know if this works


    "Igor Belagorudsky" <> wrote in message
    news:#...
    > hi,
    >
    > thanks for your response, but i have a more complicated structure
    > unfortunately...
    >
    > i dont need to display the same page on postback. i have a sort of
    > questionnaire setup where my page checks what questions to ask in the db,
    > creates appropriate form elements and waits for submission. then it writes
    > down answers and displays the next questions in the same page. so i really
    > need the data from my dynamic control without having to recreate it on the
    > page.. is this possible and if so at which point would i be able to check
    > its data? oninit?
    >
    > thank you,
    > igor
    >
    > "bruce barker" <> wrote in message
    > news:O0NE%...
    > > you have to create the dynamic control on postback also. also for it to

    > have
    > > access to its postback data, you should create it during onint, as form

    > load
    > > is too late, the datapostback event has already fired.
    > >
    > > -- bruce (sqlwork.com)
    > >
    > >
    > > "Igor Belagorudsky" <> wrote in message
    > > news:#...
    > > > Hi,
    > > >
    > > > i am trying to analyze data submitted in a form but the problem is

    that
    > > when
    > > > i try to create the controls in code (which is what i want to do), it

    > > throws
    > > > an obect not found exception on postback when i try to

    Page.FindControl:
    > > >
    > > > private void Page_Load(object sender, System.EventArgs e)
    > > > {
    > > > if (Page.IsPostBack)
    > > > {
    > > >
    > > >

    > >

    >

    Debug.WriteLine(((WebControl)Page.FindControl("testBox_1")).Attributes["hell
    > > > o"]);
    > > > }
    > > > else
    > > > {
    > > > TextBox txt = new TextBox();
    > > > txt.ID = "testBox_1";
    > > > txt.Attributes.Add("hello", "hi");
    > > > Page.FindControl("test").Controls.Add(txt);
    > > > }
    > > > }
    > > >
    > > > where control "test" is the form that i am submitting and all im

    trying
    > to
    > > > do is to debug.writeline "hi" on postback.
    > > > on the other hand, if i manually put a text box in the aspx page and

    > give
    > > it
    > > > a hello=hi attribute, everything works like it should.
    > > > can someone please tell me what the problem is?
    > > >
    > > > thanks,
    > > > igor
    > > >
    > > >

    > >
    > >

    >
    >
     
    MS News, Jul 29, 2003
    #4
    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. William Parker
    Replies:
    1
    Views:
    3,108
    Martin
    Jun 27, 2004
  2. Pipo
    Replies:
    2
    Views:
    568
    conset23
    Dec 1, 2005
  3. Replies:
    3
    Views:
    566
  4. Trent Mick
    Replies:
    0
    Views:
    484
    Trent Mick
    Apr 3, 2008
  5. Jacob Davis
    Replies:
    0
    Views:
    509
    Jacob Davis
    Apr 15, 2008
Loading...

Share This Page