1

Let's say I have a page layout with a asp.net control dropdown menu that enables you to choose two different values "red" or "blue".

Can I use a separate SP control with a runat="server" so that it would set background-color: dropdown choice here and dynamically, based on what the user chooses in Edit Mode, serve up a page that already has the property set (i.e., I don't want the javascript to be running on the client side for the color change)?

  • background of which HTML element are willing to set? Entire page? – Denis Molodtsov May 25 '18 at 0:14
  • anything. let's say a div with an id on it. my question isn't really about how to do it in javascript, but whether this would actually work and the javascript in the "runat" server would actually be running on the server rather than being client side js. – knox-flaneur May 25 '18 at 0:36
2

runat="true"attribute has nothing to do with JavaScript. The purpose of this attribute is to make a certain control available in C# via the object model by it's ID

For example, you've got a 'control'

<span runat='server' id='span'>Whatever</span>

During any postback, in your asp.net page's lifecycle you can access and manipulate your control using C# like so:

span.InnerHtml = "I'm red now!";
span.Attributes.Add("class", "red");
span.Attributes.Add("style", "background-color:red");

This is possible because you marked your control as runas="server"

In the example below, myFunction will run in the browser regardless whether you have runas attribute set or not.

<button onclick="myFunction()" runas="server" ID='but'>Click me</button>

If you are using SharePoint Designer and SharePoint Online there is no question about taking any advantage of runas attribute.

Allowing inline C# code inside aspx pages

Disclamer. There is a special place in hell for SharePoint developers who follow this approach. Use it if you absolutely need to. Obviously, you can only do it if you have access to the server.

In your SharePoint Application's web.config:

<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
   <PageParserPaths>
      <PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" /> 
   </PageParserPaths>
</SafeMode>

Once this is done, you can add inline C# code in right inside any aspx page

<html>
...

<script runat=server>
// C#
protected String GetColor()
{
  return "red";
}
</script>

<span style="background-color:<% =GetColor()%>" >. </span>

...
</html>
  • Ah I see. Is there a way to access the C# code behind a page layout if we don't have access via Visual Studio, only Site Collection Administrator privileges and SharePoint Designer? – knox-flaneur May 25 '18 at 1:21
  • You've marked your post with both SharePoint Online and SharePoint-server. Which environment is it? – Denis Molodtsov May 25 '18 at 1:30
  • Sharepoint onprem server 2013 – knox-flaneur May 25 '18 at 3:09
  • Also, I'm assuming that allowing server-side scripting is a security risk? What about for a public-facing anonymous access publishing site? – knox-flaneur May 25 '18 at 3:12
  • It's a security risk since anyone who can upload a file can upload an aspx page with inline C# that could do anything starting from giving more permissions to a certain user to dropping databases. All they have to do is wait until an admin with enough permissions visits this page. – Denis Molodtsov May 25 '18 at 3:36

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.