<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Web Parts and Personalization</title><link>http://forums.asp.net/145.aspx</link><description>Discussions of the new ASP.NET 2.0 portal framework.  &lt;a href="http://aspadvice.com/SignUp/list.aspx?l=167&amp;c=30" target="_blank"&gt;Email List&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/2474536.aspx</link><pubDate>Tue, 08 Jul 2008 13:32:40 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2474536</guid><dc:creator>OutOnFarm</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2474536.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=2474536</wfw:commentRss><description>&lt;p&gt;Kaan,

I also encountered the issue of having multiple web parts when adding user control programmatically. For my application, we want controls to appear for some users and not for others. I thought programmatically adding controls was the way but abandoned the effort. Instead, I&amp;#39;m now using roles so the WebPartManager turns on and off the visibility of the control.&lt;/p&gt;&lt;p&gt;&amp;nbsp;Within this thread, there is a link to Fredrik Normen&amp;#39;s Blog. He has a post (http://fredrik.nsquared2.com/viewpost.aspx?PostID=90) that describes &amp;quot;&lt;a href="http://fredrik.nsquared2.com/ViewPost.aspx?PostID=90"&gt;Show/Hide Web Parts based on Role&lt;/a&gt;&amp;quot;. I used this as a base, but instead of building a custom WebPartManager, I added the property&amp;nbsp; OnAuthorizeWebPart=&amp;quot;AuthorizingWebParts&amp;quot; to my WebPartManager. In the code-behind (C#), I have the protected method:&lt;/p&gt;&lt;p&gt;protected void AuthorizingWebParts(object sender, WebPartAuthorizationEventArgs e)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!String.IsNullOrEmpty(e.AuthorizationFilter))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Advisor&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (e.AuthorizationFilter.Equals(&amp;quot;AdvisorRole&amp;quot;))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!currentSession.EntitledToAdvisor) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.IsAuthorized = false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.IsAuthorized = true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;Some of this is specific to my application where currentSession is a custom object that I have&amp;nbsp; instantiated with user information. The EntitledToAdvisor returns a boolean.&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/807210.aspx</link><pubDate>Mon, 24 Jan 2005 15:48:57 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:807210</guid><dc:creator>Fredrik N</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/807210.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=807210</wfw:commentRss><description>The WebPart feature use a personalization feature that will save the WebParts and its settings into a data source based on the provider you use. So if you don't turn off the Personalization for the current page, the WebPart you have added will be saved and automatically added to the page next time you visit it. This could be done on a user level (if the user is authenticated) or for everyone if the mode of the WebPart feateurs is in a shared scope.
&lt;br /&gt;

&lt;br /&gt;
&lt;a target="_new" href="http://fredrik.nsquared2.com/viewpost.aspx?PostID=223"&gt;I should recommend you to read my post on my blog about creating a WebPart.&lt;/a&gt; Hopefully it will give your some more information.</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/807193.aspx</link><pubDate>Mon, 24 Jan 2005 15:27:36 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:807193</guid><dc:creator>kaanoezturk</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/807193.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=807193</wfw:commentRss><description>I am adding a webpart in init() event of the page. why adds the page on every postback a webpart to the page? When I restart the IIS, the webparts resist. Where is the information saved, that the page has 4 or 5 webparts?
&lt;br /&gt;

&lt;br /&gt;
I think, the webparts framework of asp.net has very strange behaviour. You can not simply add a custom control into a webpart or convert it. 
&lt;br /&gt;

&lt;br /&gt;
My code runs when I inherit webpart for my controls and add it  to the page with &amp;quot;this.Controls.Add()&amp;quot;. But my code doesn't run when I create the controls in Webpartzone's with &amp;quot;webpartmanager.addwebpart()&amp;quot;. in the second case, all properties of my controls get null.
&lt;br /&gt;

&lt;br /&gt;
KaaN</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/807050.aspx</link><pubDate>Mon, 24 Jan 2005 13:08:45 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:807050</guid><dc:creator>kaanoezturk</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/807050.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=807050</wfw:commentRss><description>This is Default.aspx wit 1 manager and 1 zone:
&lt;br /&gt;

&lt;br /&gt;

&lt;br /&gt;
&amp;lt;%@ Page Trace=&amp;quot;true&amp;quot; Language=&amp;quot;C#&amp;quot; CodeFile=&amp;quot;Default.aspx.cs&amp;quot; Inherits=&amp;quot;Default_aspx&amp;quot; %&amp;gt;
&lt;br /&gt;

&lt;br /&gt;
&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.1//EN&amp;quot; &amp;quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&amp;quot;&amp;gt;
&lt;br /&gt;

&lt;br /&gt;
&amp;lt;html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot; &amp;gt;
&lt;br /&gt;
&amp;lt;head runat=&amp;quot;server&amp;quot;&amp;gt;
&lt;br /&gt;
    &amp;lt;title&amp;gt;Untitled Page&amp;lt;/title&amp;gt;
&lt;br /&gt;
&amp;lt;/head&amp;gt;
&lt;br /&gt;
&amp;lt;body&amp;gt;
&lt;br /&gt;
    &amp;lt;form id=&amp;quot;form1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;
&lt;br /&gt;
        &amp;lt;asp:WebPartManager ID=&amp;quot;WebPartManager1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;
&lt;br /&gt;
        &amp;lt;/asp:WebPartManager&amp;gt;
&lt;br /&gt;
        &amp;lt;asp:WebPartZone ID=&amp;quot;WebPartZone1&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;
&lt;br /&gt;
        &amp;lt;/asp:WebPartZone&amp;gt;
&lt;br /&gt;
    &amp;lt;/form&amp;gt;
&lt;br /&gt;
&amp;lt;/body&amp;gt;
&lt;br /&gt;
&amp;lt;/html&amp;gt;
&lt;br /&gt;

&lt;br /&gt;

&lt;br /&gt;
This is the code file: In init() I create a WebPart.
&lt;br /&gt;

&lt;br /&gt;

&lt;br /&gt;
public partial class Default_aspx : System.Web.UI.Page 
&lt;br /&gt;
{
&lt;br /&gt;
    public Default_aspx()
&lt;br /&gt;
    {
&lt;br /&gt;
        Load += new EventHandler(Page_Load);
&lt;br /&gt;
    }
&lt;br /&gt;
    
&lt;br /&gt;
    void Page_Load(object sender, EventArgs e)
&lt;br /&gt;
    {
&lt;br /&gt;
    }
&lt;br /&gt;

&lt;br /&gt;
	protected override void OnInit(EventArgs e)
&lt;br /&gt;
	{
&lt;br /&gt;
		if (WebPartZone1.WebParts.Count == 0)
&lt;br /&gt;
		{
&lt;br /&gt;
			MyWebPart control = new MyWebPart();
&lt;br /&gt;
			control.MyValue = &amp;quot;kaan&amp;quot;;
&lt;br /&gt;
			control.ID = &amp;quot;id&amp;quot;;
&lt;br /&gt;
			control.Title = &amp;quot;kaan&amp;quot;;
&lt;br /&gt;

&lt;br /&gt;
			WebPartManager1.AddWebPart(control, WebPartZone1, 0);
&lt;br /&gt;
		}
&lt;br /&gt;
	}
&lt;br /&gt;
}
&lt;br /&gt;

&lt;br /&gt;

&lt;br /&gt;
and this is the webpart:
&lt;br /&gt;

&lt;br /&gt;

&lt;br /&gt;
public class MyWebPart : WebPart
&lt;br /&gt;
{
&lt;br /&gt;
	private string _myValue;
&lt;br /&gt;

&lt;br /&gt;
	public string MyValue
&lt;br /&gt;
	{
&lt;br /&gt;
		get
&lt;br /&gt;
		{
&lt;br /&gt;
			return this._myValue;
&lt;br /&gt;
		}
&lt;br /&gt;
		set
&lt;br /&gt;
		{
&lt;br /&gt;
			this._myValue = value;
&lt;br /&gt;
		}
&lt;br /&gt;
	}
&lt;br /&gt;

&lt;br /&gt;
	protected override void OnUnload(EventArgs e)
&lt;br /&gt;
	{
&lt;br /&gt;
		Context.Response.Write(this.MyValue);
&lt;br /&gt;
	}
&lt;br /&gt;

&lt;br /&gt;
	protected override void Render(HtmlTextWriter writer)
&lt;br /&gt;
	{
&lt;br /&gt;
		writer.Write(&amp;quot;dfgsdfgsdfgdfgsdfg&amp;quot;);
&lt;br /&gt;
	}
&lt;br /&gt;
}
&lt;br /&gt;

&lt;br /&gt;
If I run this code, I find 3 WebParts on the page. Although I set the MyValue in Default.aspx.cs, in OnUnLoad() it is empty!!
&lt;br /&gt;

&lt;br /&gt;
KaaN
&lt;br /&gt;

&lt;br /&gt;</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/807013.aspx</link><pubDate>Mon, 24 Jan 2005 12:18:29 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:807013</guid><dc:creator>Fredrik N</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/807013.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=807013</wfw:commentRss><description>You can access it if you use the GenericWebParts's ChildControl Property, and cast the returned control to your user control.
&lt;br /&gt;

&lt;br /&gt;
You can also create your own custom control and inherits the WebPart class, if you do so, you don't need to &amp;quot;turn&amp;quot; the control into a Generic webpart.</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/807008.aspx</link><pubDate>Mon, 24 Jan 2005 12:14:19 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:807008</guid><dc:creator>kaanoezturk</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/807008.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=807008</wfw:commentRss><description>Hi,
&lt;br /&gt;

&lt;br /&gt;
I try to do the same with my custom webcontrols.
&lt;br /&gt;

&lt;br /&gt;
I have problem with the custom properties of my controls. When I create a GenericWebPart of my control and add it to a WebPartZone, then the control loses it's all properties that are set before. I cannot access them anymore. What is the best solution for using custom controls as webParts?
&lt;br /&gt;

&lt;br /&gt;
BaseControl control = BaseControl.CreateControl(...); // it is the base class for my controls
&lt;br /&gt;
control.ID = &amp;quot;myControl&amp;quot;;
&lt;br /&gt;
control.ControlConfiguration = controlConfig;
&lt;br /&gt;

&lt;br /&gt;
GenericWebPart gwp = wpManager.CreateWebPart(control); // after this point, the properties of the control aren't accessible anymore
&lt;br /&gt;
wpm.AddWebPart(gwp, wpZone, 0);
&lt;br /&gt;

&lt;br /&gt;
KaaN</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/805382.aspx</link><pubDate>Fri, 21 Jan 2005 14:13:34 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:805382</guid><dc:creator>Fredrik N</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/805382.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=805382</wfw:commentRss><description>You must set the ID to your user control:
&lt;br /&gt;

&lt;br /&gt;
Control control = LoadControl(&amp;quot;webparts\EventsCalendar.ascx&amp;quot;)
&lt;br /&gt;
control.ID = &amp;quot;EventsCalendar1&amp;quot;
&lt;br /&gt;
gw = WPM_Home.CreateWebPart(control)</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/805356.aspx</link><pubDate>Fri, 21 Jan 2005 13:41:05 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:805356</guid><dc:creator>sirosoft</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/805356.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=805356</wfw:commentRss><description>I now get an error
&lt;br /&gt;

&lt;br /&gt;
The specified control of type 'EventsCalendar_ascx' does not have an ID. 
&lt;br /&gt;

&lt;br /&gt;
Description: An unhandled exception occurred during the execution of the current web Request. Please review the stack trace for more information about the error and where it originated in the code. 
&lt;br /&gt;

&lt;br /&gt;
Exception Details: System.ArgumentException: The specified control of type 'EventsCalendar_ascx' does not have an ID.
&lt;br /&gt;

&lt;br /&gt;
Source Error: 
&lt;br /&gt;

&lt;br /&gt;

&lt;br /&gt;
Line 21:     Private Sub DP_Controls_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DP_Controls.SelectedIndexChanged
&lt;br /&gt;
Line 22:         Dim gw As System.Web.UI.WebControls.WebParts.GenericWebPart
&lt;br /&gt;
Line 23:         gw = WPM_Home.CreateWebPart(LoadControl(&amp;quot;webparts\EventsCalendar.ascx&amp;quot;))
&lt;br /&gt;
Line 24:         gw.ID = &amp;quot;EventsCalendar1&amp;quot;
&lt;br /&gt;
Line 25:         WPM_Home.AddWebPart(gw, WP_Panel1, 0)
&lt;br /&gt;
 
&lt;br /&gt;
How do I set the Id for the control.
&lt;br /&gt;

&lt;br /&gt;
I tried gw.ID = &amp;quot;String&amp;quot;
&lt;br /&gt;

&lt;br /&gt;
Thanks
&lt;br /&gt;</description></item><item><title>Re: Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/803046.aspx</link><pubDate>Wed, 19 Jan 2005 13:01:32 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:803046</guid><dc:creator>Fredrik N</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/803046.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=803046</wfw:commentRss><description>You hva to turn your user control into a Generic WebPart, you can do that by using the WebPartManager's CreateWebPart method:
&lt;br /&gt;

&lt;br /&gt;
GenericWebPart gw = MyWebPartManager.CreateWebPart(LoadContorl(&amp;quot;myUserContorl.ascx&amp;quot;));
&lt;br /&gt;

&lt;br /&gt;
MyWebPartManager.AddWebPart(gw, MyWebPartZone, 0);
&lt;br /&gt;</description></item><item><title>Programmatically adding webparts to the webpart manager</title><link>http://forums.asp.net/thread/803018.aspx</link><pubDate>Wed, 19 Jan 2005 12:17:09 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:803018</guid><dc:creator>sirosoft</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/803018.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=145&amp;PostID=803018</wfw:commentRss><description>I am trying to add a webpart to a webpartzone programmatically.
&lt;br /&gt;

&lt;br /&gt;
My webpart is a user control(.ascx) file with ID &amp;quot;Event&amp;quot;
&lt;br /&gt;
My webpartZone is ID &amp;quot;Zone1&amp;quot;
&lt;br /&gt;

&lt;br /&gt;
I am trying to use webpartmanager.addwebpart(Event,Zone1,0) to add the webpart to Zone1
&lt;br /&gt;

&lt;br /&gt;
Value of type 'EventsCalendar_ascx' cannot be converted to 'System.Web.UI.WebControls.WebParts.WebPart'.	
&lt;br /&gt;

&lt;br /&gt;
My preference is to write my webparts as user controls(.ascx) as opposed to custom controls(.vb) yet it does not appear that I can then programmatically add a webpart to a zone.
&lt;br /&gt;

&lt;br /&gt;
I understand that I could use the CatalogZone with the Page and Declataritive zones to facilitate the process but I cannot find away to customize the template for these controls.  If there is a way to customize the functionality and look of these controls I could use that instead.
&lt;br /&gt;

&lt;br /&gt;
Please help.
&lt;br /&gt;

&lt;br /&gt;
Thanks
&lt;br /&gt;</description></item></channel></rss>