I am including the CustomWebPartZone code as well as the CustomWebPartChrome since it really serves no other purpose than to hijack the call to the standard WebPartChrome and directs the request to CustomWebPartChrome.

 public class CustomWebPartZone : WebPartZone
{
protected override WebPartChrome CreateWebPartChrome()
{return new CustomWebPartChrome(this, base.WebPartManager);}

protected override void OnInit ( EventArgs e ) {}
}


public class CustomWebPartChrome : WebPartChrome
{
private WebPartZone thisZone;
private WebPartManager thisMgr;

public CustomWebPartChrome(WebPartZone zone, WebPartManager mgr) : base(zone, mgr)
{
thisZone = zone;
thisMgr = mgr;
}

private double duration;
protected internal double Duration
{
get { return duration; }
set { duration = value; }
}

public override void PerformPreRender()
{
base.PerformPreRender();

string js = "<script language='javascript'>\n\t" +
"var titleBar;\n";

foreach (WebPart webPart in Zone.WebParts)
{
js += ("titleBar = document.getElementById('" + GetWebPartTitleClientID(webPart) + "');\n");
js += "titlebar.onmouseover = ShowNote;\n";
js += "titlebar.onmouseout = HideNote;\n";
}

js += "</script>";
// thisZone.Page.ClientScript.RegisterStartupScript(typeof(CustomWebPartChrome), typeof(CustomWebPartChrome).FullName, js);
// thisZone.Page.ClientScript.RegisterClientScriptInclude(typeof(CustomWebPartChrome).FullName, "CustomWebPartChrome.js");
}

public void renderTitleBar ( HtmlTextWriter writer, WebPart webPart )
{
writer.AddAttribute ( HtmlTextWriterAttribute.Cellspacing, "0" );
writer.AddAttribute ( HtmlTextWriterAttribute.Cellpadding, "0" );
writer.AddAttribute ( HtmlTextWriterAttribute.Border, "0" );
writer.AddStyleAttribute ( HtmlTextWriterStyle.Width, "100%" );
writer.RenderBeginTag ( HtmlTextWriterTag.Table );
writer.RenderBeginTag ( HtmlTextWriterTag.Tr );
writer.AddAttribute ( HtmlTextWriterAttribute.Alt, "Rendering Verbs" );
writer.RenderBeginTag ( HtmlTextWriterTag.Td );
typeof ( WebPartChrome ).GetMethod ( "RenderVerbsInTitleBar", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ).Invoke ( this, new object[] { writer, webPart, 1 } );
writer.RenderEndTag ();

writer.AddStyleAttribute ( HtmlTextWriterStyle.Width, "100%" );
TableItemStyle partTitleStyle = thisZone.PartTitleStyle;
writer.AddStyleAttribute ( HtmlTextWriterStyle.WhiteSpace, "nowrap" );
writer.AddAttribute ( HtmlTextWriterAttribute.Align, "left");
writer.AddAttribute ( HtmlTextWriterAttribute.Valign, "top");
writer.AddAttribute ( HtmlTextWriterAttribute.Id, base.GetWebPartTitleClientID ( webPart ) );

writer.RenderBeginTag ( HtmlTextWriterTag.Td );
typeof ( WebPartChrome ).GetMethod ( "RenderTitleText", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance ).Invoke ( this, new object[] { writer, webPart} );
writer.RenderEndTag ();
writer.RenderEndTag ();
writer.RenderEndTag ();
}


public override void RenderWebPart ( HtmlTextWriter writer, WebPart webPart )
{
if ( webPart == null )
{
throw new ArgumentNullException ( "webPart" );
}
bool flag = true;
PartChromeType chromeType = thisZone.GetEffectiveChromeType ( webPart );

Style style = base.CreateWebPartChromeStyle ( webPart, chromeType );
if ( !style.IsEmpty )
{
style.AddAttributesToRender ( writer, thisZone );
}
writer.AddAttribute ( HtmlTextWriterAttribute.Cellspacing, "0" );
writer.AddAttribute ( HtmlTextWriterAttribute.Cellpadding, "0" );
writer.AddAttribute ( HtmlTextWriterAttribute.Border, "0" );
writer.AddAttribute ( HtmlTextWriterAttribute.Alt, "Render Web Part" );
writer.AddStyleAttribute ( HtmlTextWriterStyle.Width, "100%" );
if ( webPart.ChromeState != PartChromeState.Minimized )
{
writer.AddStyleAttribute ( HtmlTextWriterStyle.Height, "100%" );
}
writer.AddAttribute ( HtmlTextWriterAttribute.Id, base.GetWebPartChromeClientID ( webPart ) );

if ( ( webPart.Hidden ) && ( ( thisMgr != null ) && !thisMgr.DisplayMode.ShowHiddenWebParts ) )
{
writer.AddStyleAttribute ( HtmlTextWriterStyle.Display, "none" );
}
writer.RenderBeginTag ( HtmlTextWriterTag.Table );
writer.RenderBeginTag ( HtmlTextWriterTag.Tr );
writer.AddAttribute ( HtmlTextWriterAttribute.Alt, "Rendering Titlebar Cell" );
writer.RenderBeginTag ( HtmlTextWriterTag.Td );
renderTitleBar ( writer, webPart );
writer.RenderEndTag ();
writer.RenderEndTag ();

if ( webPart.ChromeState == PartChromeState.Minimized )
{
writer.AddStyleAttribute ( HtmlTextWriterStyle.Display, "none" );
}
writer.RenderBeginTag ( HtmlTextWriterTag.Tr );
if ( !flag )
{
writer.AddStyleAttribute ( HtmlTextWriterStyle.Height, "100%" );
writer.AddAttribute ( HtmlTextWriterAttribute.Valign, "top" );
}
Style partStyle = thisZone.PartStyle;
if ( !partStyle.IsEmpty )
{
partStyle.AddAttributesToRender ( writer, thisZone );
}
writer.AddStyleAttribute ( HtmlTextWriterStyle.Padding, "0px" );
writer.AddAttribute ( HtmlTextWriterAttribute.Alt, "Rendering Part Contents" );
writer.RenderBeginTag ( HtmlTextWriterTag.Td );
base.RenderPartContents ( writer, webPart );
writer.RenderEndTag ();
writer.RenderEndTag ();
writer.RenderEndTag ();
}
}

posted on Tuesday, August 21, 2007 2:20 PM | Filed Under [ WebParts ASP.NET 2.0 ]

Comments

Gravatar
# re: Custom WebPartZone and Custom WebPartChrome
Posted by Giles
on 9/13/2007 4:34 AM
Hi - This looks brilliant. Am just getting started with web parts. I have my own controls working on the standard MS stuff. I have added your two classes to my App_code folder. How do i get one of my .ascx controls to become a custom web part instead of the normal one?

Thanks, Giles
Gravatar
# re: Custom WebPartZone and Custom WebPartChrome
Posted by 窃听器
on 9/23/2007 9:52 AM
Good article, the author thanks!
Gravatar
# re: Custom WebPartZone and Custom WebPartChrome
Posted by David Martin
on 2/5/2008 4:34 PM
Have you ever tried to make the verbs in a non menu,
i am trying to acomlish a webpartzone and chrome all done up with divs, but when i get to the verbs i can get to render only icons for my verbs.
i do not want the menu to appear, just an imagebtn for each verb.
any hints?
Gravatar
# re: Custom WebPartZone and Custom WebPartChrome
Posted by Tiger
on 6/9/2009 10:06 PM
That's good, but how can I get and render the "PartTitleStyle" ?
Gravatar
# re: Custom WebPartZone and Custom WebPartChrome
Posted by Tiger
on 6/10/2009 10:09 PM
IF I used skin file for the webpartzone,the custom webpartzone class can't get the style setting . what can I do?
Post Comment
Title *
Name *
Email
Url
Comment *