I'm developing my custom web part. Code generated by SharePoint is below:

<div id="MSOZoneCell_WebPartctl00_ctl39_TestTile" class="s4-wpcell-plain ms-webpartzone-cell ms-webpart-cell-vertical ms-fullWidth ">
<div class="ms-webpart-chrome ms-webpart-chrome-vertical ms-webpart-chrome-fullWidth ">
    <div webpartid="94347657-e02b-4104-8033-26c64532ca77" haspers="false" id="WebPartctl00_ctl39_TestTile" width="100%" class="ms-WPBody noindex " allowdelete="false" allowexport="false" style="">
        <div id="ctl00_ctl39_Test" class="cssClassAddedFromCode">
            web part content

I would like to add from code cssclass to root div with Id = MSOZoneCell_WebPartctl00_ctl39_TestTile.

When I try add my class during running method AddAttributesToRender:

writer.AddAttribute(HtmlTextWriterAttribute.Class, "cssClassAddedFromCode");

The cssclass goes to last div.

  • MSOZoneCell_WebPartctl00_ctl39_TestTile.Attributes.Add("class", "cssClassAddedFromCode") – SharePointMan Mar 16 '15 at 7:22

I'm pretty sure you can not do that from your webpart. You could do it with jQuery or similar though!


This script could be added to the page from your webpart by using ScriptManager

var addCssClassScript = "jQuery('div[id$=TestTile']').addClass('cssClassAddedFromCode');";
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "AddCssClass", addCssClassScript, true);

and hence will be run when your webpart renders

  • I hoped I can do that from server code (for example: this.Parent.CssClass), and send to client well formatted markup. – user13491 Feb 10 '14 at 12:43
  • Hopefully there is a way, but non that I know of – Robert Lindgren Feb 10 '14 at 12:59

You can do this by using something called a Control Adapter.

Here are a couple of links to get you started

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.