1

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
        </div>
    </div>
</div>

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.

1
  • MSOZoneCell_WebPartctl00_ctl39_TestTile.Attributes.Add("class", "cssClassAddedFromCode") Commented Mar 16, 2015 at 7:22

2 Answers 2

1

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

jQuery('div[id$=TestTile]').addClass('cssClassAddedFromCode');

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

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

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 and acknowledge you have read our privacy policy.

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