Anyone able to explain this webparts problem to me?


A

Alan Silver

Hello,

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
purpose.

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
disappear.

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



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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<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.Controls[0].ID;
}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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:WebPartZone>
<asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" Text="OK" /><br />
<asp:Literal ID="litMsg" runat="server" />
</div>
</form>
</body>
</html>
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top