Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Adding UserControl to Page

Adding UserControl to Page

Scheduled Pinned Locked Moved ASP.NET
helpwinformstutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stevo Z
    wrote on last edited by
    #1

    Hi, I have one default.aspx page and many .ascx user controls that are displayed inside default.aspx page. There is always only one .ascx user control in the default page (placeholder) at one time. This page is added dynamicly in Page_Load of the default.aspx :

    protected void Page_Load(object sender, EventArgs e)
    {
    this.LoadContent();
    }

    public void LoadContent()
    {
    this.placeholderContent.Controls.Clear();
    this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
    }

    during Page_Load of currently loaded user control, it might happen that depending on user action this control requires to be changed to different .ascx user control, so LoadContent (in default.aspx) is called to dump current control and load new one. Problem occurs that when a new control is loaded after an old control was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!

    zilo

    C U C 3 Replies Last reply
    0
    • S Stevo Z

      Hi, I have one default.aspx page and many .ascx user controls that are displayed inside default.aspx page. There is always only one .ascx user control in the default page (placeholder) at one time. This page is added dynamicly in Page_Load of the default.aspx :

      protected void Page_Load(object sender, EventArgs e)
      {
      this.LoadContent();
      }

      public void LoadContent()
      {
      this.placeholderContent.Controls.Clear();
      this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
      }

      during Page_Load of currently loaded user control, it might happen that depending on user action this control requires to be changed to different .ascx user control, so LoadContent (in default.aspx) is called to dump current control and load new one. Problem occurs that when a new control is loaded after an old control was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!

      zilo

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I thnk Page Load is too late to do this, you need to do it in the loadviewstate event for events to fire properly.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      S 2 Replies Last reply
      0
      • S Stevo Z

        Hi, I have one default.aspx page and many .ascx user controls that are displayed inside default.aspx page. There is always only one .ascx user control in the default page (placeholder) at one time. This page is added dynamicly in Page_Load of the default.aspx :

        protected void Page_Load(object sender, EventArgs e)
        {
        this.LoadContent();
        }

        public void LoadContent()
        {
        this.placeholderContent.Controls.Clear();
        this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
        }

        during Page_Load of currently loaded user control, it might happen that depending on user action this control requires to be changed to different .ascx user control, so LoadContent (in default.aspx) is called to dump current control and load new one. Problem occurs that when a new control is loaded after an old control was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!

        zilo

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #3

        You have to use the LoadControl method of the page class. This way, the "state" of the user control reflects the "state" of the page. For more information search with google for "dynamically add usercontrol asp.net"

        -^-^-^-^-^-^-^-^-^-^-^- no code is free of bugs

        S 2 Replies Last reply
        0
        • C Christian Graus

          I thnk Page Load is too late to do this, you need to do it in the loadviewstate event for events to fire properly.

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          S Offline
          S Offline
          Stevo Z
          wrote on last edited by
          #4

          Hi, thanx for answer, I'll try that.

          zilo

          1 Reply Last reply
          0
          • U Urs Enzler

            You have to use the LoadControl method of the page class. This way, the "state" of the user control reflects the "state" of the page. For more information search with google for "dynamically add usercontrol asp.net"

            -^-^-^-^-^-^-^-^-^-^-^- no code is free of bugs

            S Offline
            S Offline
            Stevo Z
            wrote on last edited by
            #5

            That's what I do - use LoadControl of the Page class. I'll try google that link. thank you

            zilo

            1 Reply Last reply
            0
            • U Urs Enzler

              You have to use the LoadControl method of the page class. This way, the "state" of the user control reflects the "state" of the page. For more information search with google for "dynamically add usercontrol asp.net"

              -^-^-^-^-^-^-^-^-^-^-^- no code is free of bugs

              S Offline
              S Offline
              Stevo Z
              wrote on last edited by
              #6

              Hi, I've seen many tutorials and they all have one thing in common. They are very simple and do not cover this problematics deeply enough. I did not learn anything new. I would appreciate your help if you have any experience in dynamic loading and changing user controls. thanx

              zilo

              1 Reply Last reply
              0
              • C Christian Graus

                I thnk Page Load is too late to do this, you need to do it in the loadviewstate event for events to fire properly.

                Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                S Offline
                S Offline
                Stevo Z
                wrote on last edited by
                #7

                Hi, when adding controls to my placeholder in loadviewstate , they are not displayed at all

                zilo

                1 Reply Last reply
                0
                • S Stevo Z

                  Hi, I have one default.aspx page and many .ascx user controls that are displayed inside default.aspx page. There is always only one .ascx user control in the default page (placeholder) at one time. This page is added dynamicly in Page_Load of the default.aspx :

                  protected void Page_Load(object sender, EventArgs e)
                  {
                  this.LoadContent();
                  }

                  public void LoadContent()
                  {
                  this.placeholderContent.Controls.Clear();
                  this.placeholderContent.Controls.Add(this.LoadControl([...some mechanism how to get right file...]));
                  }

                  during Page_Load of currently loaded user control, it might happen that depending on user action this control requires to be changed to different .ascx user control, so LoadContent (in default.aspx) is called to dump current control and load new one. Problem occurs that when a new control is loaded after an old control was dumped, events (button clicks) of new added control are not registred properly. When a button on new control is clicked, it doesn't invoke the registered event during next postback. Event's start working from second postback on... (if the user control doesn't change) thanx for help!

                  zilo

                  C Offline
                  C Offline
                  ChrisKo 0
                  wrote on last edited by
                  #8

                  Move it from Page_Load to Page_OnInit and hopefully that should fix your problem.

                  S 1 Reply Last reply
                  0
                  • C ChrisKo 0

                    Move it from Page_Load to Page_OnInit and hopefully that should fix your problem.

                    S Offline
                    S Offline
                    Stevo Z
                    wrote on last edited by
                    #9

                    It did not help. It behaves same way as when in Page_Load :(

                    zilo

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups