CodeVerge.Net Beta


   Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > Asp.Net Forum > general_asp.net.web_parts_and_personalization Tags:
Item Type: Date Entered: 2/11/2005 3:30:34 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 8 Views: 45 Favorited: 0 Favorite
9 Items, 1 Pages 1 |< << Go >> >|
"EdenMachine" <
NewsGroup User
Can't Create Dynamic WebParts2/11/2005 3:30:34 PM

0

I'm having some problems creating dynamic WebParts. I've read all the other threads on this topic as well as some of Fredrik's Blogs but it doesn't seem to work for me so I'm not sure if it's because I'm using Beta 2 now or what.

Here is my code:

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

void Page_Init(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
Label myLabel = new Label();

myLabel.ID = "lblMyLabel";
myLabel.Text = "This is the body of the label that I'm using.";

GenericWebPart myWebPart = WebPartManager1.CreateWebPart(myLabel);
myWebPart.Title = "This is the title";

WebPartManager1.AddWebPart(myWebPart, WebPartZone1, 0);

WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;
}
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>My Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:WebPartManager ID="WebPartManager1" runat="server" />
<asp:WebPartZone ID="WebPartZone1" runat="server" Width="200" />
</form>
</body>
</html>
Rich
http://www.DevAndDesign.com/
"EdenMachine" <
NewsGroup User
Re: Can't Create Dynamic WebParts2/11/2005 4:30:12 PM

0

BTW, all that I get from the code above is a WebPartZone and a titlebar with the title in it. The Label's Text property does not show as the WebPart's body. I've tried many other controls and they all get the same result.

Also, sorry, I forget to wrap my code with code tags.

I got the basics of this from Fredrik's blog but swapped out the usercontrol for the dynamic label control.

http://fredrik.nsquared2.com/viewpost.aspx?PostID=223

Also, when I tried to do it with the UserControls like Fredrik did it kept telling me that the control didn't have a valid ID or something. All my controls had IDs though.
Rich
http://www.DevAndDesign.com/
"Fredrik N" <>
NewsGroup User
Re: Can't Create Dynamic WebParts2/11/2005 4:54:00 PM

0

I treid your code an got the same problem with the Label control.
/Fredrik Norm?n NSQUARED2
Microsoft MVP, MCSD, MCAD, MCT

Cornerstone

My Blog, ASP.Net 2.0 etc
"EdenMachine" <
NewsGroup User
Re: Can't Create Dynamic WebParts2/11/2005 6:49:25 PM

0

Well, I had to do this to get it to work...


<%@ 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)
{
Control myControl = LoadControl("~/Home/Controls/TestControl.ascx");
myControl.ID = "lblMyLabel";

GenericWebPart webPart = myWebPartManager.CreateWebPart(myControl);
webPart.Title = "This is the test title";

myWebPartManager.AddWebPart(webPart, WebPartZone2, 0);
}
}
</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="myWebPartManager" runat="server" />
<asp:WebPartZone ID="WebPartZone2" runat="server" PartStyle-Width="100%" Width="250" />
</div>
</form>
</body>
</html>



However, I'm still unable to dynamically create a WebPart using all C# code like I tried to do in my original post though. ANy help with that would be appreciated.

Thanks,

Rich
Rich
http://www.DevAndDesign.com/
"mharder" <>
NewsGroup User
Re: Can't Create Dynamic WebParts3/1/2005 10:35:17 PM

0

In your original code, the Label was actually being added to the WebPartZone. However, the Label.Text property is not a Personalizable property, so the property reverted to its default value (String.Empty) when it was added to the WebPartZone.

You should either use a UserControl (like your second code), or else write a derived Label that has a Personalizable Text property.
http://blogs.msdn.com/mharder

This posting is provided "AS IS" with no warranties, and confers no rights.
"EdenMachine" <
NewsGroup User
Re: Can't Create Dynamic WebParts3/2/2005 3:18:57 PM

0

Any idea why this is happening? It seems to be RE-initializing my exposed property after I override it.

Here are my debug screen-shots...

http://www.edenmachine.com/debug/

Thanks,

Rich
Rich
http://www.DevAndDesign.com/
"mharder" <>
NewsGroup User
Re: Can't Create Dynamic WebParts3/2/2005 7:34:22 PM

0

WebPartManager.AddWebPart() adds a *copy* of the WebPart parameter, not the parameter itself. When we copy the WebPart, we also copy the values of all personalizable properties, but all non-personalizable properties are reset to their default value.

To fix your code, you should change the LabelText field to a personalizable property:
[Personalizable]

public string LabelText { get; set; }
This way, the value of the property will be maintained when you add the WebPart to the page.
http://blogs.msdn.com/mharder

This posting is provided "AS IS" with no warranties, and confers no rights.
"EdenMachine" <
NewsGroup User
Re: Can't Create Dynamic WebParts3/2/2005 7:41:53 PM

0

So is the last post on this thread...

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=836954

....a better way to go about it or should I use the [Personalizable] way you suggested instead?
Rich
http://www.DevAndDesign.com/
"mharder" <>
NewsGroup User
Re: Can't Create Dynamic WebParts3/2/2005 7:55:27 PM

0

If you want the value to be persisted for future requests, the property needs to be personalizable.
http://blogs.msdn.com/mharder

This posting is provided "AS IS" with no warranties, and confers no rights.
9 Items, 1 Pages 1 |< << Go >> >|


Free Download:













problem with webparts

value cannot be null. parameter name: enumtype

how to render existing web pages via portal web parts

anonymous webpart access

add hyperlinks to webpartzone control

where do web part properties get stored?

letting other visitors see users personalised pages

select boxes disappear on refresh

webparts - declarative catalogpart

webpart database schema error

problems getting a dropdownlist.selectedvalue in a wss toolpart

'profilecommon' is ambiguous

menu verb doesnt come in my application web parts

ibuyspy for .net 2.0?

can't add properties to profile in web.config

sqltableprofileprovider sample

group profiles?

webparts + untitled title + masterpage

programmatically adding webparts to the webpart manager

database requirements

payment gateway

passing parameter to pageviewerwebpart - change pageviewerwebpart contentlink programatically

publishing a master page to make to view all the users of contentlibrary sharepoint

dynamically adding and removing webpartzones

setting up and suing the profile in asp.net 2.0

webparts, personalizable properties, and child controls

instructiontext on catalogzone doesn't render

how can i track webparts' changes

anyone tried to cache user controls being added as web parts?

web part creation

add/remove web part dynamically

appearanceeditorpart chromestate

when should i be using web parts

shared initial scope don't reflect changes to visitors

how do you pass data from one web part to another?

web parts: minimize/restore event, how to override

deny the webpart drop in webpartzone?

web part functionality for each control within a page

databound marquee problem please help.

get code from an aspx file (with webparts) and save it as a html

problem with dynamically creating webparts

use webpartmanager setpersonalizationdirty but webparts with ajax 1.0 updatepanel have problem

ok, so own up asp.net team... padding bug.

checking for part in zone

webpart catalogue

dynamic webparts

targeting a web part in declarativecatalog

possible to minimize web parts by default?

personlizeable property resetting on postback

personalizing complex properties of web controls

   
  Privacy | Contact Us
All Times Are GMT