Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 11th, 2006, 10:53 PM
Authorized User
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default handling dynamic load of user controls

Hi,

Once you load a control and add it to say a place holder, is that control always there and is it just a matter of displaying it by some method or do you need to load it on each page postback. What i would like to do is have say 4 or 5 user controls and just display the appropriate one when selected by a user clicking a linkbutton. The user controls also need to keep viewstate. Is this possible, or a better way, if so, a point in the right direction would be great. Thanks for any advice

Kind Regards
Scott
 
Old December 16th, 2006, 06:14 PM
Authorized User
 
Join Date: Nov 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To elaborate, I have handled this by overriding the rendering of the page. It works fine for all combinations but from aFindJob(fj) to aNewJob(nj). Hence I can go from nj to fj, fj to aAddEmp(ae) to fj,ae to nj, nj to ae, and fails only on fj to nj. It throws a failed to load view state error. the code is as follows. I cant work out why it is doing it.

        private void aFindJob_Click(object sender, System.EventArgs e)
        {
            ph1.Controls.Clear();
            Bindph("FindProject.ascx");
        }

        private void aNewJob_Click(object sender, System.EventArgs e)
        {
            ph1.Controls.Clear();
            Bindph("NewProject.ascx");
        }
        private void aAddEmp_Click(object sender, System.EventArgs e)
        {
            ph1.Controls.Clear();
            Bindph("AddEmployee.ascx");
        }

        private void Bindph(string usrctl)
        {
                UserControl uc1 = (UserControl)LoadControl(usrctl);
                this.ph1.Controls.Clear();
                this.ph1.Controls.Add(uc1);
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (IsPostBack && this.ph1.HasControls())
            {
                switch (World.GetPostBackControl(this).ID)
                {
                    case "aNewJob":
                        ph1.Controls.Clear();
                        Bindph("NewProject.ascx");
                        break;
                    case "aFindJob":
                        ph1.Controls.Clear();
                        Bindph("FindProject.ascx");
                        break;
                    case "aAddEmp":
                        ph1.Controls.Clear();
                        Bindph("AddEmployee.ascx");
                        break;
                    default:
                        Bindph("NewProject.ascx");
                        break;
                }
            }
            for (int i= 0;i<=Controls.Count -1;++i)
            {
                Controls[i].RenderControl(output);
            }
        }

thanks for any help.
Scott





Similar Threads
Thread Thread Starter Forum Replies Last Post
Load User Controls Asynchronously everest ASP.NET 2.0 Professional 3 October 29th, 2007 08:00 AM
Dynamic User Controls.... Jayahar ASP.NET 2.0 Professional 8 October 12th, 2007 01:27 AM
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
handling event for a dynamic object deepak_8282 General .NET 2 May 14th, 2007 12:42 PM
Load User Controls at Runtime Duncan VB.NET 3 November 4th, 2005 07:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.