Using custom css within a Sharepoint webpart as a Embedded Resource is not working, why?

回眸只為那壹抹淺笑 提交于 2019-12-13 05:41:10

问题


I've read in another question here how could I use custom CSS within my Sharepoint WebPart. So I followed this link:

http://www.synergyonline.com/blog/blog-moss/Lists/Posts/Post.aspx?ID=42

And I put my css as an embedded resource. Everything ok, I launch the page.

But my style doesn't appear.

I peek at the source code, and I find my resource:

<script src="/WebResource.axd?d=YuTREer2woiGbjiSaZdP0bLrdm6vpTygUffdwMFJr0zmn76B3vav0QRpmxzvYvKzZRnmgKpNbLHpnJf-W4rfrv-MrIZEoz6tWi5xHXTiN3lcxdP3s7ysDExW-eBQTlH8cUrMRw2&amp;t=634369276247430484" type="text/javascript"></script>

Which leads me to my css:

.webPartContainer
{
    background-color:Aqua;
}

.webPartContainer .Text
{
    font-size:xx-large;
}

And here's where it references the styles:

<td class="ms-WPBorder" valign="top">
    <div WebPartID="2109154d-6921-46ac-84d4-8ce24a926617" HasPers="false" id="WebPartctl00_m_g_2109154d_6921_46ac_84d4_8ce24a926617" width="100%" class="ms-WPBody" allowDelete="false" allowExport="false" style="" >
        <div id="ctl00_m_g_2109154d_6921_46ac_84d4_8ce24a926617">
        <div class="webPartContainer">
            <span class="Text">Hello World with container!</span>
            </div>
    </div>
    </div>
</td>

What's possibly happening ?

Thanks in advance !

EDIT: CssRegistration gives me permission errors, why ?


回答1:


The browser don't expect the content of a <script type='text/javascript'> tag to be CSS. So you should change your code to something like this:

string tempLink= "<link rel='stylesheet' text='text/css' href='{0}' />";
string location = Page.ClientScript.GetWebResourceUrl(this.GetType(), "myStyleSheet.css");
LiteralControl include = new LiteralControl(String.Format(tempLink, location));
Controls.Add(include);


来源:https://stackoverflow.com/questions/5471816/using-custom-css-within-a-sharepoint-webpart-as-a-embedded-resource-is-not-worki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!