views:

382

answers:

2

Hi,

I have a webpart that displays HTML output within the RenderWebPart method and also creates controls within the CreateChildControls both methods are declared as overridden in the webpart.

My question is how to I control the order of the display of the controls and html output?

At the moment I call EnsureChildControls() within the RenderWebPart mthod to ensure all controls within the CreateChildControls are created and then the html out is rendered.

What if I wanted to display a control on the page then html output and then another control below in that order?

+2  A: 

I would recommend moving all of your static HTML out of the Render function and into the CreateChildControls function. If you need to, you can add regular old HTML using Labels, WebControls, or even better... LiteralControls. Then you can just add them to your Controls collection.

Example:

WebControl container = new WebControl(System.Web.UI.HtmlTextWriterTag.Div);
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
foreach (Node child in this.Children)
{
    sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", url, name);
}
sb.Append("</ul>");
LiteralControl l = new LiteralControl();
l.Text = sb.ToString();
container.Controls.Add(l);
Kit Menke
A: 

Hi Nav,

I'm actually relatively new to SharePoint and I was very astounded initially that one had to create individual controls manually, without a GUI interface like any normal ASP.NET web development involves. However, I had a very very good tip that allowed me to still use a GUI interface, and use it to create webparts for Sharepoint. It involves wrapping WebUserControls in webparts.

The link I used is here: http://www.a2zdotnet.com/View.aspx?id=95

You basically create WebUserControls (.ascx file) in a website, and so you can just add your controls like any normal .aspx page. You can also have a normal code-behind file (.ascx.cs). You then drag the .ascx file onto an .aspx file, so that it will then use your WebUserControl. When your .ascx files are ready and built, you copy them to the Layouts directory in the 12-Hive of your SharePoint Server. Best to create a sub-directory in there, to avoid clashing with other files in there already.

You then need to create a separate class Library project, that will have your WebPart code on. You then tell your WebPart to use your .ascx files in the layouts directory. Something like this:

protected override void  CreateChildControls()
    {
        base.CreateChildControls();
        try
        {
            this.Controls.Clear();
            _myControl = this.Page.LoadControl("\\_layouts\\MyFolder\\WebUserControl.ascx");
            this.Controls.Add(_myControl);
        }
        catch (Exception e)
        {
            err = e.Message;
        }

The link I provided above provides more information, but basically you compile the webpart project and then add the DLL to the BIN directory of your sharepoint server (c:\inetpub\wwwroot\wss\ etc). You don't have to compile it to the GAC, by the way.

Then you add a entry to the web.config of your sharepoint server:

<SafeControl Assembly="MyWebUserControl, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" Namespace="MyWebUserControl" TypeName="*" Safe="True" />

If you didn't compile the DLL to the GAC, but the BIN instead, you just need:

<SafeControl Assembly="UserControl" Namespace="UserControl" TypeName="*" Safe="True" />

Again, the link I posted above has it written in the code, a reference to the GUID, which is only needed if you placed the DLL in the GAC. You don't need the GUID part of the code if you only placed in the BIN directory.

Hope that helps.

Ash

Ashok Jingar