473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Adding WebParts via code behind?


I'm working on a project where the user of a site will receive custom
content, depending on a set of parameters. The content will all be
contained in UserControls (.ascx), that will be used as webparts on a
page.

We need to add the webparts dynamically to the site, depending on the
status of the user: this involves a personalisation of a travel
website, so a user can be "before" a trip, "during" a trip and "after"
a trip, and depending on that status he'll see different content. (This
is just one of the parameters, actually.) Of course, this all needs to
integrate with the built-in personalisation offered by Profiles.

However, all my experiments with adding UCs/WebParts via code behind
have failed miserably: adding the works, but whenever the user clicks
on a button inside a webpart, the postback causes the webparts to
multiply.

I've looked around, but so far I haven't yet found a working example of
what I want to do. Can anyone help me out? Or am I trying to do
something that is impossible?

Tools & technologies: ASP.NET 2.0 / C# / Visual Studio 2005

--
BVH

Sep 4 '06 #1
3 2098
The WebPart Framework is designed to declaratively do the work for you,
storing user choices in profile. When you dynamically add bits, you can end
up with dupes in one of two ways.

1. The user has an item in his profile and you have also added that item
or
2. The web part/control is held in ViewState and you dynamically add one,
thinking this is the only way to change a control's state.

In either case, you now have two objects (one created originally either by
the user's choice or your initial load on the page (and into ViewState) plus
the one you added when the form posted back).

User Controls can act a lot like web parts, although they have fewer moving
parts.

Another thing: Page_Load is for loading a page. While this sounds like
"well, duh" I find so few people really get the lifecycle. If you are
loading a page and then allowing the controls to reside in ViewState, you
need to make sure the initial load is ONLY in Not is Postback. In most
cases, the pattern for page load is this (pseudocode):

Page_Load

If this is not a postback
'Load elements necessary for page view
Else
'Only load things necessary for EVERY
' postback here (i.e. 100% of the time)
End

END

You handle all other manipulation in the events. If you have a Page_Load
that has a huge amount of code, you are probably working out a huge
anti-pattern. That is not wise.
What I would suggest is taking a step back and running through a full debug
cycle on a page that is "failing" (by your spec -- i.e. adding additional
web parts, etc.). Set watches on the number of controls, breakpoints in
different methods, etc. and watch what is happening. Turn on and off
ViewState. This exercise will not directly solve your issue, but it will
truly enlighten you as to what Microsoft is doing for you.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside the box!
*************************************************
"Bart Van Hemelen" <ba************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>
I'm working on a project where the user of a site will receive custom
content, depending on a set of parameters. The content will all be
contained in UserControls (.ascx), that will be used as webparts on a
page.

We need to add the webparts dynamically to the site, depending on the
status of the user: this involves a personalisation of a travel
website, so a user can be "before" a trip, "during" a trip and "after"
a trip, and depending on that status he'll see different content. (This
is just one of the parameters, actually.) Of course, this all needs to
integrate with the built-in personalisation offered by Profiles.

However, all my experiments with adding UCs/WebParts via code behind
have failed miserably: adding the works, but whenever the user clicks
on a button inside a webpart, the postback causes the webparts to
multiply.

I've looked around, but so far I haven't yet found a working example of
what I want to do. Can anyone help me out? Or am I trying to do
something that is impossible?

Tools & technologies: ASP.NET 2.0 / C# / Visual Studio 2005

--
BVH

Sep 4 '06 #2

Cowboy (Gregory A. Beamer) wrote:
The WebPart Framework is designed to declaratively do the work for you,
storing user choices in profile. When you dynamically add bits, you can end
up with dupes in one of two ways.

1. The user has an item in his profile and you have also added that item
or
2. The web part/control is held in ViewState and you dynamically add one,
thinking this is the only way to change a control's state.
I now do this in Page_Load:

if ( WebPartZone1.WebParts.Count)
{
// add UCs as WebParts here
}

which seems to have solved the problem: no duplicate webparts,
postbacks work as they should etc. The state of those webparts -- i.e.
if they are minimized or not -- also seems to get saved in the Profile.

(Of course, it also means that I cannot put any WebParts on the page
unless I do it via this method, but that's a price I'm willing to pay.)
You handle all other manipulation in the events. If you have a Page_Load
that has a huge amount of code, you are probably working out a huge
anti-pattern. That is not wise.
I realise that, but other factors are more important to my employers:
timely delivery, for at least a cost as possible. I'm particularly
interested in knowing how I "should" solve this problem, i.e. how
Microsoft wants me to solve this problem, since I do want to do this
the proper way.

However, after having gone through numerous examples I still haven't
encountered any that resemble what I need to do, which is serve users
with a custom list of webparts that depends on a number of factors
which will be derived from an existing framework.

--
BVH

Sep 5 '06 #3

Bart Van Hemelen wrote:
Cowboy (Gregory A. Beamer) wrote:
The WebPart Framework is designed to declaratively do the work for you,
storing user choices in profile. When you dynamically add bits, you can end
up with dupes in one of two ways.

1. The user has an item in his profile and you have also added that item
or
2. The web part/control is held in ViewState and you dynamically add one,
thinking this is the only way to change a control's state.

I now do this in Page_Load:

if ( WebPartZone1.WebParts.Count)
{
// add UCs as WebParts here
}

which seems to have solved the problem: no duplicate webparts,
postbacks work as they should etc. The state of those webparts -- i.e.
if they are minimized or not -- also seems to get saved in the Profile.
It seems I spoke too soon. I just wrote some quick code to check if
this would work, and after cleaning it up -- i.e. inserting the proper
titles for each fo the uc-webparts for instance -- and retesting it, of
course I got the webparts I expected, plus all the old ones that were
saved in the profile.

Considering that the website is going to be very dynamic in the future,
we must have 100% control of the uc-webparts the user will have access
to.

So I need to either a) find the proper way of adding uc-webparts
dynamically, or b) figure out a way to remove the improper ones from
the profile.

--
BVH

Sep 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Barbara Alderton | last post by:
I am working with SharePoint 2003. I am using (using C#) an existing webpart as a child control (server control). I need to access/manipulate the HTML that the control outputs prior to rendering...
2
by: fa | last post by:
Hi there, I have created an .aspx file using Asp.Net (2.0) C#, which contains two webparts. How can i pass data/parameter between these two, so that when user chooses an item from WebPart1, I can...
2
by: Paal Berggreen | last post by:
How is a WebPart programmatically added to a WebPartZone, in code-behind? I would intuitively think that something like this is the way to go: this.WebPartManager1.Zones.Controls.Add(myPart); ...
1
by: Astera | last post by:
I seem to be having a problem using web parts after the beta 2 release. Any time I try to have the WebPartManager on any page, I get the exception listed below. I've gone through the steps listed...
4
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
3
by: Q. John Chen | last post by:
I have an INTRAnet site that need to add some content management feature. And I thought that I can use the WebParts. Since this is an internal site, I use Windows authentication so the use don't...
0
by: Ryan | last post by:
Hi everyone, I have enabled my webparts site to use ajax by simply using updatepanels. This worked fine. Now I tried to enable drag and drop for the webparts so I added some stuff to the web...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
3
by: clintonG | last post by:
Why don't we see more WebParts being used on the web? SharePoint gets all the fun? And what are the prevailing opinions be they what they may about the use of WebParts rather than WPF when the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.