| Author |
Message |
JD
Guest
|
Posted:
Mon Oct 31, 2005 9:51 pm Post subject:
Render SPSWC inside custom server control |
|
|
I am in the process of creating a custom server control to use when
customizing the sharepoint templates. This way I can manage my changes
all in one place (my new server control).
So, I went ahead and created a new project for my controls (assembly)
with no problems. I was also able to make my decleration of the new
control at the page level and use my new server control tag; everything
works great! The only exception is that the Sharepoint Server Controls
(SPSWC) are not rendering. The control is written to the page as
output but not processed...
I used to get this same functionality in ASP when the server didn't
process my dynamic code in the correct order.... Please keep in mind
that I do not have access to the life cycle of a web part as I would in
a custom web part; I am using a custom server control in a template,
so I am a bit confused how to invoke the rendering at the correct time.
I went ahead and tried to render it programatically but kept receiving
the all ambiguous error "Object reference not set to an instance of the
object"... I read that these controls (SPSWC) may need to be in a form
(OWSForm) to render, but that didn't work for me either; any thoughts
would be greatly appreciated.....
Here is my code from the custom server control class:
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.SharePoint.Portal
Imports Microsoft.SharePoint.WebControls
Public Class PortalHeader
Inherits Control
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Try
writer.Write("<tr><td colspan=3 width=100%>")
writer.Write("<table border=0 cellspacing=0 cellpadding=0
width=100%>")
writer.Write("<tr><td width=786><img
src=/_layouts/images/global/header/header_left.gif /></td>")
writer.Write("<td
background=/_layouts/images/global/header/header_bg.gif> </td>")
writer.Write("<td width=14><img
src=/_layouts/images/global/header/header_right.gif></td>")
writer.Write("</tr>")
writer.Write("</table>")
writer.Write("<table width=100% cellpadding=0 cellspacing=0
border=0>")
writer.Write("<tr>")
writer.Write("<td width=0% nowrap>")
writer.Write("<div class=ms-phnav1wrapper>")
writer.Write("<SPSWC:CategoryNavigationWebPart
id=HorizontalNavBar runat=server DisplayStyle=HorizontalOneLayer />")
writer.Write("</div>")
writer.Write("</td>")
writer.Write("<td width=100% nowrap align=left
class=ms-phnav1wrapper valign=top>")
writer.Write("<div class=ms-phnavtableone> </div>")
writer.Write("</td>")
writer.Write("<td width=0% class=ms-phnav1wrapper nowrap
align=left valign=top>")
writer.Write("<div class=ms-phnavtableone>")
writer.Write("<SPSWC:PageHeader id=PageHeaderID
runat=server PageContext = SitePage ShowTitle=false
HelpID=NavBarHelpHome mode=LinksOnly/>")
writer.Write("</div>")
writer.Write("</td>")
writer.Write("</tr>")
writer.Write("</table>")
writer.Write("</td>")
writer.Write("</tr>")
Catch ex As Exception
Dim strError As String = ex.Message
End Try
End Sub
End Class
================================================
The text is all output to the server, but the control is never
rendered...
Is there something special that I need to do to get Sharepoint custom
server controls (SPSWC) to render in my custom server control?
Thanks,
Joe Dorsa |
|
| Back to top |
|
 |
Daniel Larson (www.portal
Guest
|
Posted:
Tue Nov 01, 2005 9:51 pm Post subject:
RE: Render SPSWC inside custom server control |
|
|
JD,
You have succeeded in writing the tag to the output stream-- what you need
to do is use the control as a control. For example I'll just use a
literalcontrol for simplicity:
....in your render method:
LiteralControl foo = new LiteralControl("hello, y'all");
foo.RenderControl(writer);
You also may want to try NOT overriding render, but instead jsut add
controls to the controls collection and let the WebPart do the rendering.
Daniel Larson
http://feeds.feedburner.com/daniellarson
"JD" wrote:
| Quote: | I am in the process of creating a custom server control to use when
customizing the sharepoint templates. This way I can manage my changes
all in one place (my new server control).
So, I went ahead and created a new project for my controls (assembly)
with no problems. I was also able to make my decleration of the new
control at the page level and use my new server control tag; everything
works great! The only exception is that the Sharepoint Server Controls
(SPSWC) are not rendering. The control is written to the page as
output but not processed...
I used to get this same functionality in ASP when the server didn't
process my dynamic code in the correct order.... Please keep in mind
that I do not have access to the life cycle of a web part as I would in
a custom web part; I am using a custom server control in a template,
so I am a bit confused how to invoke the rendering at the correct time.
I went ahead and tried to render it programatically but kept receiving
the all ambiguous error "Object reference not set to an instance of the
object"... I read that these controls (SPSWC) may need to be in a form
(OWSForm) to render, but that didn't work for me either; any thoughts
would be greatly appreciated.....
Here is my code from the custom server control class:
Imports System
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.SharePoint.Portal
Imports Microsoft.SharePoint.WebControls
Public Class PortalHeader
Inherits Control
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Try
writer.Write("<tr><td colspan=3 width=100%>")
writer.Write("<table border=0 cellspacing=0 cellpadding=0
width=100%>")
writer.Write("<tr><td width=786><img
src=/_layouts/images/global/header/header_left.gif /></td>")
writer.Write("<td
background=/_layouts/images/global/header/header_bg.gif> </td>")
writer.Write("<td width=14><img
src=/_layouts/images/global/header/header_right.gif></td>")
writer.Write("</tr>")
writer.Write("</table>")
writer.Write("<table width=100% cellpadding=0 cellspacing=0
border=0>")
writer.Write("<tr>")
writer.Write("<td width=0% nowrap>")
writer.Write("<div class=ms-phnav1wrapper>")
writer.Write("<SPSWC:CategoryNavigationWebPart
id=HorizontalNavBar runat=server DisplayStyle=HorizontalOneLayer />")
writer.Write("</div>")
writer.Write("</td>")
writer.Write("<td width=100% nowrap align=left
class=ms-phnav1wrapper valign=top>")
writer.Write("<div class=ms-phnavtableone> </div>")
writer.Write("</td>")
writer.Write("<td width=0% class=ms-phnav1wrapper nowrap
align=left valign=top>")
writer.Write("<div class=ms-phnavtableone>")
writer.Write("<SPSWC:PageHeader id=PageHeaderID
runat=server PageContext = SitePage ShowTitle=false
HelpID=NavBarHelpHome mode=LinksOnly/>")
writer.Write("</div>")
writer.Write("</td>")
writer.Write("</tr>")
writer.Write("</table>")
writer.Write("</td>")
writer.Write("</tr>")
Catch ex As Exception
Dim strError As String = ex.Message
End Try
End Sub
End Class
================================================
The text is all output to the server, but the control is never
rendered...
Is there something special that I need to do to get Sharepoint custom
server controls (SPSWC) to render in my custom server control?
Thanks,
Joe Dorsa
|
|
|
| Back to top |
|
 |
JD
Guest
|
Posted:
Wed Nov 02, 2005 5:51 pm Post subject:
Re: Render SPSWC inside custom server control |
|
|
Thanks Daniel,
I'll give it try and let you know how it goes. BTW - Thanks for all
your input to these newsgroups; you have been very helpful!
Best Regards,
Joe Dorsa |
|
| Back to top |
|
 |
Daniel Larson (www.portal
Guest
|
Posted:
Wed Nov 02, 2005 5:51 pm Post subject:
Re: Render SPSWC inside custom server control |
|
|
Thanks Joe! I appreciate that.
Good luck, and post back if you need more help.
Daniel
"JD" wrote:
| Quote: | Thanks Daniel,
I'll give it try and let you know how it goes. BTW - Thanks for all
your input to these newsgroups; you have been very helpful!
Best Regards,
Joe Dorsa
|
|
|
| Back to top |
|
 |
JD
Guest
|
Posted:
Thu Nov 03, 2005 5:51 pm Post subject:
Re: Render SPSWC inside custom server control |
|
|
Daniel,
I went ahead and removed this line:
writer.Write("<SPSWC:CategoryNavigationWebPart
id=HorizontalNavBar runat=server DisplayStyle=HorizontalOneLayer />")
and replaced it with this line:
Dim CategoryNavigationWebPart As New
LiteralControl("<SPSWC:CategoryNavigationWebPart
id=HorizontalNavBar runat=server DisplayStyle=HorizontalOneLayer />")
CategoryNavigationWebPart .RenderControl(writer)
However, the control is never truley rendered... If I look at the
source from the client, it still looks like this (some code has been
removed for brevity):
<SPSWC:CategoryNavigationWebPart id=HorizontalNavBar runat=server
DisplayStyle=HorizontalOneLayer />
as it has never been processed / rendered by the server.
Remember I am using this as a custom server control and not as a custom
web part. Above you stated "You also may want to try NOT overriding
render, but instead just add
controls to the controls collection and let the WebPart do the
rendering"; but I am not creating a web part. Could you send me an
example of doing this from a custom server control? Am I missing
something here?
Best regards,
Joe Dorsa |
|
| Back to top |
|
 |
Daniel Larson (www.portal
Guest
|
Posted:
Thu Nov 03, 2005 5:51 pm Post subject:
Re: Render SPSWC inside custom server control |
|
|
Hey Joe
The ListeralControl was only an example-- the literal control outputs text.
(Try "hello world"... ). Following the example, declare the webpart as a new
CategoryNavigationWebPart and add it to the Controls collection, or render
it.
You ar emissing something... try first with the LiteralControl. Then replace
that code with the CategoryNavigationWebPart.
....in c# (you'll have to translate)
CategoryNavigationWebPart part = new CategoryNavigationWebPart();
this.Controls.Add(part);
.... don't override Render, or call base.Render() and your Controls
collection will render.
Daniel Larson
"JD" wrote:
| Quote: | Daniel,
I went ahead and removed this line:
writer.Write("<SPSWC:CategoryNavigationWebPart
id=HorizontalNavBar runat=server DisplayStyle=HorizontalOneLayer />")
and replaced it with this line:
Dim CategoryNavigationWebPart As New
LiteralControl("<SPSWC:CategoryNavigationWebPart
id=HorizontalNavBar runat=server DisplayStyle=HorizontalOneLayer />")
CategoryNavigationWebPart .RenderControl(writer)
However, the control is never truley rendered... If I look at the
source from the client, it still looks like this (some code has been
removed for brevity):
SPSWC:CategoryNavigationWebPart id=HorizontalNavBar runat=server
DisplayStyle=HorizontalOneLayer /
as it has never been processed / rendered by the server.
Remember I am using this as a custom server control and not as a custom
web part. Above you stated "You also may want to try NOT overriding
render, but instead just add
controls to the controls collection and let the WebPart do the
rendering"; but I am not creating a web part. Could you send me an
example of doing this from a custom server control? Am I missing
something here?
Best regards,
Joe Dorsa
|
|
|
| Back to top |
|
 |
JD
Guest
|
Posted:
Thu Nov 03, 2005 9:51 pm Post subject:
Re: Render SPSWC inside custom server control |
|
|
Daniel,
Thank you for the quick reply. This is what I did and it worked like a
charm; I was missing the portion of adding the control to the
collection... I know these threads are going to make many peoples
lives a little bit easier :)
(vb.net)
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Try
writer.Write("<tr><td colspan=3 width=100%>")
writer.Write("<table border=0 cellspacing=0 cellpadding=0
width=100%>")
writer.Write("<tr><td width=786><img
src=/_layouts/images/global/header/header_left.gif /></td>")
writer.Write("<td
background=/_layouts/images/global/header/header_bg.gif> </td>")
writer.Write("<td width=14><img
src=/_layouts/images/global/header/header_right.gif></td>")
writer.Write("</tr>")
writer.Write("</table>")
writer.Write("<table width=100% cellpadding=0 cellspacing=0
border=0>")
writer.Write("<tr>")
writer.Write("<td width=0% nowrap>")
writer.Write("<div class=ms-phnav1wrapper>")
Dim CategoryNavigationWebPart As New
Microsoft.SharePoint.Portal.WebControls.CategoryNavigationWebPart
CategoryNavigationWebPart.ID = "HorizontalNavBar"
CategoryNavigationWebPart.DisplayStyle =
Microsoft.SharePoint.Portal.WebControls.CategoryNavigationWebPartRenderStyle.HorizontalOneLayer
Me.Controls.Add(CategoryNavigationWebPart)
CategoryNavigationWebPart.RenderControl(writer)
writer.Write("</div>")
writer.Write("</td>")
writer.Write("<td width=100% nowrap align=left
class=ms-phnav1wrapper valign=top>")
writer.Write("<div class=ms-phnavtableone> </div>")
writer.Write("</td>")
writer.Write("<td width=0% class=ms-phnav1wrapper nowrap
align=left valign=top>")
writer.Write("<div class=ms-phnavtableone>")
Dim PageHeader As New
Microsoft.SharePoint.Portal.WebControls.PageHeader
PageHeader.ID = "PageHeaderID"
PageHeader.PageContext =
Microsoft.SharePoint.Portal.WebControls.PageContext.SitePage
PageHeader.ShowTitle = False
PageHeader.HelpID = "NavBarHelpHome"
PageHeader.Mode =
Microsoft.SharePoint.Portal.WebControls.PageHeaderMode.LinksOnly
Me.Controls.Add(PageHeader)
PageHeader.RenderControl(writer)
writer.Write("</div>")
writer.Write("</td>")
writer.Write("</tr>")
writer.Write("</table>")
writer.Write("</td>")
writer.Write("</tr>")
Catch ex As Exception
'Call your own exception logging
End Try
End Sub
Thanks again for all your help. With Sharepoint it is up to us to pave
the way!
Best Regards,
Joe Dorsa |
|
| Back to top |
|
 |
JD
Guest
|
Posted:
Thu Nov 03, 2005 9:51 pm Post subject:
Re: Render SPSWC inside custom server control |
|
|
Daniel,
As an update; the rendering of the CategoryNavigationWebPart control
inside my custom server control only happens if the original <SPSWC:
CategoryNavigationWebPart/> is also on the page at the same time as my
server control. If I remove it, no rendering happens....
This is very strange; any thoughts?
Joe Dorsa |
|
| Back to top |
|
 |
|
|
|
|