Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:06:31+00:00 2026-06-04T05:06:31+00:00

I’ve some UserControls which are created dynamically, with parameters : foreach (DataRow dr in

  • 0

I’ve some UserControls which are created dynamically, with parameters :

foreach (DataRow dr in drc)
{
    question = (from ques in bdd.QUESTION where ques.ID_QUESTION == idQuestion select ques).FirstOrDefault();
    List<Object> listParams = new List<Object>();
    listParams.Add(question);
    AdminQuestion qa = (AdminQuestion)(Controller.LoadControl(Page, "~/UserControls/AdminQuestion.ascx", listParams.ToArray()));
    pnl_question_list.Controls.Add(qa);
}

This is a method I found on SoF and it works great.
This method is called when I click on a button, and all my usercontrols are correctly created.
In this usercontrol, I have a button :

<asp:Button ID="btn_validation_modify_question" runat="server" Text="Modifier" 
            CssClass="clear_left_float_left myBouton myBoutonEnvoyer" 
            OnClick="btn_validation_modify_question_Click"/>

And my method :

protected void btn_validation_modify_question_Click(object sender, EventArgs e)
{
    QUESTION q = (from m in bdd.QUESTION where m.ID_QUESTION == question.ID_QUESTION select m).FirstOrDefault();
    q.MESSAGE = txt_modify_question_message.InnerText;
    q.ID_THEME = new Guid(ddl_modify_question_theme.SelectedValue);
    bdd.ApplyCurrentValues<QUESTION>("QUESTION", q);
    bdd.SaveChanges();
}

But when I click on the button, it don’t apply the method, and reload the page, like if I didn’t have any declared method.

Is there something I don’t know? I already used this technique and everything worked…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:06:33+00:00Added an answer on June 4, 2026 at 5:06 am

    If you add controls dynamically, you’ll normally need to do it on Init or PreInit page events. Otherwise event handlers will never work.

    So, dynamically added controls must be programmatically added to the Web page on each and every page visit. The best time to add these controls is during the initialization stage of the page life cycle, which occurs before the load view state stage. That is, we want to have the control hierarchy complete before the load view state stage arrives. For this reason, it is best to create an event handler for the Page class’s Init event in your code-behind class, and add your dynamic controls there.

    Note You may be able to get away with loading your controls in the
    Page_Load
    event handler and maintaining the view state properly. It all depends on whether or not you are setting any properties of the dynamically loaded controls programmatically and, if so, when you’re doing it relative to the
    Controls.Add(dynamicControl)
    line. A thorough discussion of this is a bit beyond the scope of this article, but the reason it may work is because the
    Controls
    property’s
    Add()
    method recursively loads the parent’s view state into its children, even though the load view state stage has passed.

    Here you have more information.

    EDIT

    Just to be sure we’re talking about the same, here you have how your code should look like

    protected void Page_Init(object sender, EventArgs e)
    {
        ...
        foreach (DataRow dr in drc)
        {
            question = (from ques in bdd.QUESTION where ques.ID_QUESTION == idQuestion select ques).FirstOrDefault();
            List<Object> listParams = new List<Object>();
            listParams.Add(question);
            AdminQuestion qa = (AdminQuestion)(Controller.LoadControl(Page, "~/UserControls/AdminQuestion.ascx", listParams.ToArray()));
            pnl_question_list.Controls.Add(qa);
    
        }
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.