By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Anyone able to explain this webparts problem to me?

P: n/a

Sorry for the long post, but I want to try and explain it clearly...

I'm using webparts for (possibly) an unusual scenario, and I'm having
problems. This could be because I'm doing something they weren't
designed for, or it could just be that I don't understand what's going
on well enough to get it to work.

The situation is that I have a page that displays products from a
database. The products can have multiple prices, say for small medium
and large, and I want the web site owner to be able to choose the order
in which the prices are displayed. This proved to be very hard, as the
reordering needs to be all client-side to avoid the site owner having to
suffer a postback for every price moved. As webparts have a built-in
function for moving them around, they seemed worth examining for this

I thought of setting up a webpartzone in design mode, and adding a label
for each price. That would allow the site owner to move the prices
around until they were in the right order. When the page was submitted,
I would read the order of the labels and set the display order of the
prices based on that.

I tried this with hard-coded labels inside the webpartzone and it worked
fine. The problem came when I tried to add the labels in the Page_Load
event. The page showed fine, but as soon as you move a webpart, they all
disappear. I cannot work out why this is happening.

I have shown the complete code for the page below, so if anyone would be
kind enough to try it, all you need to do is copy it into a new .aspx
file and run it. When the page first loads, you see three boxes, each
representing a price. As soon as you try and move one of them, they all

I would be very grateful if anyone could explain what is going on here.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">

<script runat="server">

protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
WebPartDisplayMode mode = WebPartManager1.SupportedDisplayModes["Design"];
if (mode != null) {
WebPartManager1.DisplayMode = mode;
for (int i = 0; i < 3; i++) {
Label lblVar = new Label();
lblVar.ID = "lbl" + i.ToString();
GenericWebPart lblWebPart = WebPartManager1.CreateWebPart(lblVar);
lblWebPart.ID = "lblVar_" + i.ToString();
lblWebPart.Title = "Price number " + i.ToString();
WebPartManager1.AddWebPart(lblWebPart, ItemList, i);

protected void btnOK_Click(object sender, EventArgs e) {
litMsg.Text += "Count == " + ItemList.WebParts.Count.ToString();
for (int i = 0; i < ItemList.WebParts.Count; i++) {
litMsg.Text += "<br />" + i.ToString() + " - " + ItemList.WebParts[i].Controls[0].ID;


<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:WebPartManager ID="WebPartManager1" runat="server" />
<asp:WebPartZone ID="ItemList" runat="server" BorderWidth="0" CloseVerb-Enabled="False"
CloseVerb-Visible="False" DeleteVerb-Enabled="False" DeleteVerb-Visible="False" HeaderText="Prices (variations)"
MinimizeVerb-Enabled="False" MinimizeVerb-Visible="False" Padding="0">
<ZoneTemplate />
<asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="OK" /><br />
<asp:Literal ID="litMsg" runat="server" />
Alan Silver
(anything added below this line is nothing to do with me)
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.