Dynamically loaded User Controls - problem with button_click event

Discussion in 'ASP .Net Web Controls' started by Tim T, Oct 30, 2003.

  1. Tim T

    Tim T Guest

    [this is a simplified / clearer explanation of an earlier post - hopefully
    someone can help!]

    Hi,
    I have the need to use dynamically loaded user controls in a webform page.
    I have the controls loading dynamically, and that part works fine. this is
    the code used in a webform to dynamically load one of several controls:
    private void btnCategory_Click(object sender, System.EventArgs e)

    {
    Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
    ".ascx");
    PlaceHolder1.Controls.Add(myControl);
    }

    That part works fine, selecting an item from the dropdown loads the relevant
    control perfectly into the placeholder on the hosting page.

    The dynamically loaded control has its own form and button within it. when a
    user fills out the form and clicks the 'send' button, the button_click event
    within the control is supposed to do some work and add the form data to a
    DB.

    My problem is that when the button from within a dynamically loaded control
    is clicked, nothing happens for that event. All that happens is the page
    hosting the control is reloaded, and the control disappears.

    I must add that when the same control is added to a test page, everything
    works fine as planned - the click event for the button does as it is
    supposed to - so there is nothing wrong there. The problem is arising
    because this is a DYNAMICALLY loaded control sitting in a placeholder.

    I hope someone can see my exact problem and knows the solution. I'm sure it
    is something simple, but am new to .NET and am stuck.
    Any help greatly appreciated.

    Tim..
    Tim T, Oct 30, 2003
    #1
    1. Advertising

  2. Tim T

    HD Guest

    Just did add something on earlier post and saw the more recent one after
    that.

    Well does the page you are dynamically have its own form and on select you
    load a user control with its own form.

    I think there might be some issues with more than one form (I remember
    reading it a few days back when I started with web controls)



    Regards,



    HD



    "Tim T" <> wrote in message
    news:bnrtsd$2rsk$...
    > [this is a simplified / clearer explanation of an earlier post - hopefully
    > someone can help!]
    >
    > Hi,
    > I have the need to use dynamically loaded user controls in a webform page.
    > I have the controls loading dynamically, and that part works fine. this is
    > the code used in a webform to dynamically load one of several controls:
    > private void btnCategory_Click(object sender, System.EventArgs e)
    >
    > {
    > Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
    > ".ascx");
    > PlaceHolder1.Controls.Add(myControl);
    > }
    >
    > That part works fine, selecting an item from the dropdown loads the

    relevant
    > control perfectly into the placeholder on the hosting page.
    >
    > The dynamically loaded control has its own form and button within it. when

    a
    > user fills out the form and clicks the 'send' button, the button_click

    event
    > within the control is supposed to do some work and add the form data to a
    > DB.
    >
    > My problem is that when the button from within a dynamically loaded

    control
    > is clicked, nothing happens for that event. All that happens is the page
    > hosting the control is reloaded, and the control disappears.
    >
    > I must add that when the same control is added to a test page, everything
    > works fine as planned - the click event for the button does as it is
    > supposed to - so there is nothing wrong there. The problem is arising
    > because this is a DYNAMICALLY loaded control sitting in a placeholder.
    >
    > I hope someone can see my exact problem and knows the solution. I'm sure

    it
    > is something simple, but am new to .NET and am stuck.
    > Any help greatly appreciated.
    >
    > Tim..
    >
    >
    HD, Oct 30, 2003
    #2
    1. Advertising

  3. "Tim T" <> wrote in message
    news:bnrtsd$2rsk$...
    > [this is a simplified / clearer explanation of an earlier post - hopefully
    > someone can help!]
    >
    > Hi,
    > I have the need to use dynamically loaded user controls in a webform page.
    > I have the controls loading dynamically, and that part works fine. this is
    > the code used in a webform to dynamically load one of several controls:
    > private void btnCategory_Click(object sender, System.EventArgs e)
    >
    > {
    > Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
    > ".ascx");
    > PlaceHolder1.Controls.Add(myControl);
    > }
    >
    > That part works fine, selecting an item from the dropdown loads the

    relevant
    > control perfectly into the placeholder on the hosting page.
    >
    > The dynamically loaded control has its own form and button within it. when

    a
    > user fills out the form and clicks the 'send' button, the button_click

    event
    > within the control is supposed to do some work and add the form data to a
    > DB.
    >
    > My problem is that when the button from within a dynamically loaded

    control
    > is clicked, nothing happens for that event. All that happens is the page
    > hosting the control is reloaded, and the control disappears.
    >
    > I must add that when the same control is added to a test page, everything
    > works fine as planned - the click event for the button does as it is
    > supposed to - so there is nothing wrong there. The problem is arising
    > because this is a DYNAMICALLY loaded control sitting in a placeholder.
    >
    > I hope someone can see my exact problem and knows the solution. I'm sure

    it
    > is something simple, but am new to .NET and am stuck.
    > Any help greatly appreciated.


    When you say the user control has its own form, do you mean it has another
    <form> element within it? If so, do you really need that?
    --
    John
    John Saunders, Oct 30, 2003
    #3
  4. Tim T

    Adam Carden Guest

    I had the same problem however the answer is simple, control binding /
    view state preperation starts before page_load and any event.

    If you place your control loading logic in page_init sub all will work
    properly.

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Adam Carden, Oct 30, 2003
    #4
  5. Tim T

    Leo Duran Guest

    I am guessing that the problem is in the hosting form. You should post your
    Page_Load code so we can take a look at that. You most likely have a problem
    that can be solved by moving some code into a ...

    if(!(IsPostBack))
    {
    ....
    }

    section of code.

    I hope that helps a bit.

    "Tim T" <> wrote in message
    news:bnrtsd$2rsk$...
    > [this is a simplified / clearer explanation of an earlier post - hopefully
    > someone can help!]
    >
    > Hi,
    > I have the need to use dynamically loaded user controls in a webform page.
    > I have the controls loading dynamically, and that part works fine. this is
    > the code used in a webform to dynamically load one of several controls:
    > private void btnCategory_Click(object sender, System.EventArgs e)
    >
    > {
    > Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
    > ".ascx");
    > PlaceHolder1.Controls.Add(myControl);
    > }
    >
    > That part works fine, selecting an item from the dropdown loads the

    relevant
    > control perfectly into the placeholder on the hosting page.
    >
    > The dynamically loaded control has its own form and button within it. when

    a
    > user fills out the form and clicks the 'send' button, the button_click

    event
    > within the control is supposed to do some work and add the form data to a
    > DB.
    >
    > My problem is that when the button from within a dynamically loaded

    control
    > is clicked, nothing happens for that event. All that happens is the page
    > hosting the control is reloaded, and the control disappears.
    >
    > I must add that when the same control is added to a test page, everything
    > works fine as planned - the click event for the button does as it is
    > supposed to - so there is nothing wrong there. The problem is arising
    > because this is a DYNAMICALLY loaded control sitting in a placeholder.
    >
    > I hope someone can see my exact problem and knows the solution. I'm sure

    it
    > is something simple, but am new to .NET and am stuck.
    > Any help greatly appreciated.
    >
    > Tim..
    >
    >
    Leo Duran, Oct 30, 2003
    #5
  6. Tim T

    Tim T Guest

    Thanks Adam,
    doing as you suggested enables a control placed in a placeholder to work
    from within the page propertly as i need,
    But now I have a new problem. I still can't do what I am trying to achieve
    because the control I want to load into the placeholder is determined by
    what a user selects from a dropdown in the [hosting] page.

    I put the code loading the control into a Page_Init sub as suggested, it
    works when i statically Load a control by hard coding the control name in
    the LoadControl("test.ascx"), but i need it to load the control dynamically

    here is the sub:

    private void Page_Init(object sender, System.EventArgs e)
    {
    if(Page.IsPostBack)
    {
    string controlName = DropDownList1.SelectedItem.Text; //the text for
    dropdown items is the control to load
    Control myControl = LoadControl(controlName+".ascx");
    PlaceHolder1.Controls.Add(myControl);
    }
    }

    the page is posted back when a user selects form the dropdown and clicks a
    button, here is the sub that handles the button click:

    private void btnCategory_Click(object sender, System.EventArgs e)
    {
    selectecdCategory.Text = DropDownList1.SelectedItem.Text.Replace("
    ","").Replace("/","").Replace("&","");

    }

    when the user selects a categroy and clicks the button, the page posts back,
    I get this error

    I get the following error:

    "Object reference not set to an instance of an object. "

    for this line:
    string controlName = DropDownList1.SelectedItem.Text;

    So the DropDownList1 object is not initialised before the Page_Init sub
    fires.

    Can you see what i am trying to achieve? any further suggestions
    appreciated.
    Thanks to everyone for their input so far.

    Tim..





    "Adam Carden" <> wrote in message
    news:...
    >
    >
    > I had the same problem however the answer is simple, control binding /
    > view state preperation starts before page_load and any event.
    >
    > If you place your control loading logic in page_init sub all will work
    > properly.
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Tim T, Oct 31, 2003
    #6
  7. Tim T

    Tim T Guest

    Thanks HD,
    There is only one <form runat="server"> tag on the page hosting the control,
    no form tag in the user control, i meant form fields and a button

    I think i'm getting closer to solving the problem, have done what Adam
    Carden suggested and that works for loading controls into place holders, but
    i am still having trouble getting them to load dynamically. i may have to
    try a different approach soon!

    Tim

    "HD" <> wrote in message
    news:...
    > Just did add something on earlier post and saw the more recent one after
    > that.
    >
    > Well does the page you are dynamically have its own form and on select you
    > load a user control with its own form.
    >
    > I think there might be some issues with more than one form (I remember
    > reading it a few days back when I started with web controls)
    >
    >
    >
    > Regards,
    >
    >
    >
    > HD
    >
    >
    >
    > "Tim T" <> wrote in message
    > news:bnrtsd$2rsk$...
    > > [this is a simplified / clearer explanation of an earlier post -

    hopefully
    > > someone can help!]
    > >
    > > Hi,
    > > I have the need to use dynamically loaded user controls in a webform

    page.
    > > I have the controls loading dynamically, and that part works fine. this

    is
    > > the code used in a webform to dynamically load one of several controls:
    > > private void btnCategory_Click(object sender, System.EventArgs e)
    > >
    > > {
    > > Control myControl = LoadControl(DropDownList1.SelectedItem.Text +
    > > ".ascx");
    > > PlaceHolder1.Controls.Add(myControl);
    > > }
    > >
    > > That part works fine, selecting an item from the dropdown loads the

    > relevant
    > > control perfectly into the placeholder on the hosting page.
    > >
    > > The dynamically loaded control has its own form and button within it.

    when
    > a
    > > user fills out the form and clicks the 'send' button, the button_click

    > event
    > > within the control is supposed to do some work and add the form data to

    a
    > > DB.
    > >
    > > My problem is that when the button from within a dynamically loaded

    > control
    > > is clicked, nothing happens for that event. All that happens is the

    page
    > > hosting the control is reloaded, and the control disappears.
    > >
    > > I must add that when the same control is added to a test page,

    everything
    > > works fine as planned - the click event for the button does as it is
    > > supposed to - so there is nothing wrong there. The problem is arising
    > > because this is a DYNAMICALLY loaded control sitting in a placeholder.
    > >
    > > I hope someone can see my exact problem and knows the solution. I'm sure

    > it
    > > is something simple, but am new to .NET and am stuck.
    > > Any help greatly appreciated.
    > >
    > > Tim..
    > >
    > >

    >
    >
    Tim T, Oct 31, 2003
    #7
    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. Tim Thomas
    Replies:
    3
    Views:
    806
  2. Tim T
    Replies:
    7
    Views:
    516
    Adam Carden
    Nov 1, 2003
  3. Winshent
    Replies:
    1
    Views:
    655
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Oct 17, 2005
  4. james
    Replies:
    3
    Views:
    393
    James
    Jul 13, 2006
  5. Tim Thomas

    Button_Click event in user control not firing on page

    Tim Thomas, Oct 30, 2003, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    165
Loading...

Share This Page