sharepoint rendering templates set attributes from fields - asp.net

I'm trying to write a custom rendering template for use in the DisplayForm of a list whose fields include Title, HeaderImageUrl, Body. I've got the following so far:
<SharePoint:RenderingTemplate ID="NewsListForm" runat="server">
<Template>
<h1><SharePoint:FieldValue FieldName="Title" runat="server"/></h1>
<div>
<p>
<SharePoint:UrlField FieldName="HeaderImageUrl" runat="server"></SharePoint:UrlField>
<SharePoint:RichTextField FieldName="Body" runat="server" />
</p>
</div>
</Template>
</SharePoint:RenderingTemplate>
The HeaderImageUrl field is configured to be an Image URL, but when I use the UrlField control, it renders a link.
I was thinking there might be a way to grab field values and put them in attributes, so something like this:
<asp:Image ImageUrl="<% FieldValue["HeaderImageUrl"] %>" runat="server"/>
If this kind of thing isn't possible, then is there another control in Microsoft.SharePoint.WebControls that would render an image from a URL?

All the examples I've seen of calling a URL field to be displayed as an image have been through C# code. Which is quite annoying since most of web part bits can be written without using C# code. You might also need to go down that road here... perhaps a custom field control or user control.
Even if your code above did get the value for HeaderImageUrl, it wouldn't work as Url values are (confusingly):
[the actual url specified][some wierd delimeter string][the description field of the url (usually the actual url again if your don't specify it)]
So you would need to split the string with te delimeter values and use the [0] (the first) as what you inject into your image url.
Please do post if you find a way to do this without writing C# code.
p.s. Have you considered using a DataFormWebPart which has a xsl section in which you might be able to do extractly what you need?!
Example: You can stick the following syntax into any aspx / ascx file:
<%# Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Register tagprefix="WebPartPages" namespace="Microsoft.SharePoint.WebPartPages" assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None"
NoDefaultStyle="TRUE" ViewFlag="0" Title="My List Display"
Default="FALSE" ID="dfwp_MyListDisplay"
DisplayName="My List Display">
<DataSources>
<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true"
selectcommand='<View><Query><Where><Eq><FieldRef Name="Category"/><Value Type="Text">Header</Value></Eq></Where></Query></View>' id="Utility_x0020_Navigation1"><SelectParameters><WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="My List Display"/>
<asp:Parameter Name="ListName" DefaultValue="LIST NAME" />
<asp:Parameter Name="WebUrl" DefaultValue="<% $SPUrl:~SiteCollection/ %>" />
</SelectParameters>
</SharePoint:SPDataSource>
</DataSources>
<ParameterBindings>
<ParameterBinding Name="dvt_apos" Location="Postback;Connection"/>
<ParameterBinding Name="PageURL" Location="ServerVariable(URL)" DefaultValue=""/>
<ParameterBinding Name="SiteUrl" Location="ServerVariable(SERVER_NAME)" DefaultValue=""/>
</ParameterBindings>
<datafields>#Title,Title;#URL,URL;#Category,Category;#Parent_x0020_item,Parent_x0020_item;#Sort_x0020_Order,Sort Order;#ID,ID;#ContentType,Content Type;#Modified,Modified;#Created,Created;#Author,Created By;#Editor,Modified By;#_UIVersionString,Version;#Attachments,Attachments;#File_x0020_Type,File Type;#FileLeafRef,Name (for use in forms);#FileDirRef,Path;#FSObjType,Item Type;#_HasCopyDestinations,Has Copy Destinations;#_CopySource,Copy Source;#ContentTypeId,Content Type ID;#_ModerationStatus,Approval Status;#_UIVersion,UI Version;#Created_x0020_Date,Created;#FileRef,URL Path;</datafields>
<xsl>
YOUR XSLT HERE
</xsl>
</WebPartPages:DataFormWebPart>

The following code will get you the url of an imagefield:
SPListItem item = GetItem();
string imagefieldhtml = item["HeaderImageUrl"].ToString();
ImageFieldValue imagefield = new ImageFieldValue(imagefieldhtml);
var url = imagefield.ImageUrl;
So maybe altering your to something like this will help you, or maybe help you out atleast...?
<asp:Image ImageUrl="<% (ImageFieldValue ["HeaderImageUrl"]).ImageUrl %>" runat="server"/>

Unable to test it atm (no environment) but I am quite sure I did this before.
Try these:
<SharePoint:FieldValue FieldName="HeaderImageUrl" runat="server"/>
or:
<img src="<SharePoint:FieldValue FieldName="HeaderImageUrl" runat="server"/>" />
I'll check back on this one and do an actual test by monday if those didn't work and you still need help.

Turns out that my problem was simply that I didn't have that image column set up to render as an image, rather than as a link. The builtin Microsoft.SharePoint.UrlField control worked just fine, as it turned out.

Related

Including CSS and JavaScript as Web Forms User Controls and making it as fast as possible

I would like to include CSS and JavaScript directly in ASP.Net Web Form and the first thing that I am using now is Web Forms User Control like this:
<head runat="server">
<uc1:bootstrap_css runat="server" id="bootstrap_css" />
<uc1:bootstrap_icons_css runat="server" id="bootstrap_icons_css" />
<uc1:cookieconsent_min_css runat="server" ID="cookieconsent_min_css" />
<uc1:normalize_css runat="server" ID="normalize_css" />
<uc1:splide_css runat="server" ID="splide_css" />
</head>
And uc1:bootstrap_css looks something like this:
<%# Control Language="C#"
AutoEventWireup="false"
Debug="false"
EnableTheming="false"
EnableViewState="false"
ValidateRequestMode="Disabled"
ViewStateMode="Disabled"
WarningLevel="0"
Strict="false"
Inherits="eee.css.bootstrap_css" %>
<style>
#charset "UTF-8";/*!
* Bootstrap v5.0.0-beta2 (https://getbootstrap.com/)
* Copyright 2011-2021 The Bootstrap Authors
* Copyright 2011-2021 Twitter, Inc.
....
</style>
Now I see that User Control has some properties like CompilationMode, AutoEventWireup, etc. How to set those properties so it will reduce server processing to the minimum?

Web Form code behind not creating Partial Class, page inherits "ProjectName.ClassName" instead of just "ClassName". Visual Studio 2013

So I'm trying to make an "About" page that can be shared across many web applications that we use. To do this, I'm going to be saving the "AboutBox.aspx" and "AboutBox.aspx.vb" files on our web server. Then, from each application, I'll open a pop-up window that will call the AboutBox page by its url. The Problem I'm having is after I've created the page and copied it to the server, when I try to open the page it's giving me the error:
Parser Error Message: Could not load type 'DemoWebApp.AboutBox'.
Line 1: <%# Page Language="vb" AutoEventWireup="false" CodeBehind="AboutBox.aspx.vb" Inherits="DemoWebapp.AboutBox" %>
So I'm assuming it's having an issue with "Inherits" having the project name "DemoWebApp" in front of the class name.
Also, the code behind file that was created with the web form has "Public Class AboutBox" instead of what I think they're supposed to have which is "Partial Class AboutBox"
I've tried just getting rid of "DemoWebApp." from the inherits and also just changing the code behind to being a partial class instead of public. This didn't work.
Additionally, someone else created a test web form page from an older version of Visual Studio. This web form didn't have the project title in the inherits and had a partial class instead of public. This page was copied to the server and worked just fine.
I'm using Visual Studio 2013, I'm not sure if it's an issue with this version or something else entirely.
Is there any way that, when you add a new web form file to the project, it will create the file the way I need it to?
EDIT:
Here is my AboutBox.aspx code:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="AboutBox.aspx.vb" Inherits="TestAboutBox.AboutBox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height:238px; width:486px; background-color:green">
<form id="form1" runat="server">
<div style="width:154px; height:232px; float:left;">
<asp:Image ID="LogoImage" runat="server" Height="232px" ImageAlign="Middle" Width="154px" ImageUrl="small.png"/>
</div>
<div style="width:326px; height:232px; float:right">
<asp:Label ID="LabelProductName" runat="server" Text="Product Name" Font-Size="8.25pt" Font-Names="Microsoft Sans Serif" style="margin:5px"></asp:Label>
<br />
<asp:Label ID="LabelVersion" runat="server" Text="Version" Font-Size="8.25pt" Font-Names="Microsoft Sans Serif" style="margin:5px"></asp:Label>
<br />
<asp:Label ID="LabelCopyright" runat="server" Text="Copyright" Font-Size="8.25pt" Font-Names="Microsoft Sans Serif" style="margin:5px"></asp:Label>
<br />
<asp:Label ID="LabelCompanyName" runat="server" Text="Company Name" Font-Size="8.25pt" Font-Names="Microsoft Sans Serif" style="margin:5px"></asp:Label>
<br />
<asp:TextBox ID="TextBoxDescription" runat="server" Height="113px" Width="317px" Font-Size="8.25" Font-Names="Microsoft Sans Serif" TextMode="MultiLine" BackColor="#F0F0F0" style="margin:5px" ReadOnly="True">Description :</asp:TextBox>
<input type="submit" name="OKButton" value="OK" onclick="window.close();" id="OKButton" style="height:21px;width:75px;float:right; margin:5px" />
</div>
</form>
</body>
</html>
Aaand here's my AboutBox.aspx.vb file:
Public Class AboutBox
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
NOTE: I re-made the project under the name "TestAboutBox" since "DemoWebApp" was getting all messed up with me screwing around with stuff.
When you create a new Web Forms page from a Web Application project, it will create the page as described in the question. In order to get it the right way, the Web Form page needs to be made in a Web Site project. This page can then be used elsewhere.

Concat Web.config value and string in Image control SRC

I have a web.config key :
<add key="IMGSRC" value="http://localhost" />
I want to use the value of this key along with the path of the image concatenated to in an aspx page. I'm aware that I can get to root folder by simply saying "../ImagesFolder" , but my website has parent path disabled because of security concerns. So now I need to work around it.
I need something like this (Here are a few things I tried after looking up the internet and which did not work.):
1) <img id="Img19" runat="server" alt="Admin" src='<%#ConfigurationSettings.AppSettings["IMGSCR"] %>' />
2) <img id="Img19" runat="server" alt="Admin" src='<%#ConfigurationSettings.AppSettings["IMGSCR"] + "/ImagesFolder/img.jpeg" %> ' />
3)
<img id="Img19" runat="server" alt="Admin" src="<%#ConfigurationSettings.AppSettings["IMGSCR"] %> " + "/ImagesFolder/img.jpeg" />
Also I tried this:
I declared a variable Path on Page_Load
Path = System.Configuration.ConfigurationManager.AppSettings["RootforIMG"].ToString();
and then on aspx page I tried using it as
<img id="Img19" runat="server" alt="Admin" src="<%=Path %> " + "/ImagesFolder/img.jpeg" /> but this as well is no good.
Can you try something like below?
<img id="Img19" runat="server" alt="Admin" src='<%= GetImageSource()%>' />
In code behind
public string GetImageSource()
{
return ConfigurationManager.AppSettings["IMGSCR"] + "/ImagesFolder/img.jpeg";
}
The relative path of images should work:
Relative path can be as per your page location i.e: '../IMages/img.jpg' or 'images/img.jpg'
try this :
<img id="Img19" runat="server" alt="Admin" src='ImagesFolder/img.jpeg' />
For controls with runat="server" attribute you do not need any special code to map a path relative to web site root:
<img id="Img19" runat="server" alt="Admin" src"~/ImagesFolder/img.jpeg" />
The path ~/ImagesFolder/img.jpeg will be resolved replacing ~ with the root folder of your web-site.
If to resolve path you need some kind of logic (for example you need to call a function) then you can use this:
<img src'<%= ResolveImageName() %>' />
Do not forget that URL must be proper encoded.

SharePoint 2007 popup rich text editor displays corporate logo from master page - how can I remove this?

I am currently having problems with attempting to style the HTML rich text editor in our MOSS 2007 site definition.
I have specified a corporate logo inline on a custom master page in the body tag as follows:
<%#Master language="C#"%>
<%# Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%# Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%# Import Namespace="Microsoft.SharePoint" %>
<%# Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%# Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Register TagPrefix="wssuc" TagName="Welcome" src="~/_controltemplates/Welcome.ascx" %>
<%# Register TagPrefix="wssuc" TagName="DesignModeConsole" src="~/_controltemplates/DesignModeConsole.ascx" %>
<HTML id="HTML1" dir="<%$Resources:wss,multipages_direction_dir_value%>" runat="server" xmlns:o="urn:schemas-microsoft-com:office:office">
<HEAD id="HEAD1" runat="server">
<META Name="GENERATOR" Content="Microsoft SharePoint">
<META Name="progid" Content="SharePoint.WebPartPage.Document">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<META HTTP-EQUIV="Expires" content="0">
<SharePoint:RobotsMetaTag ID="RobotsMetaTag1" runat="server"/>
<Title ID=onetidTitle><asp:ContentPlaceHolder id=PlaceHolderPageTitle runat="server"/></Title>
<SharePoint:CssLink ID="CssLink1" runat="server"/>
<SharePoint:Theme ID="Theme1" runat="server"/>
<SharePoint:ScriptLink ID="ScriptLink1" language="javascript" name="core.js" Defer="true" runat="server" />
<SharePoint:CustomJSUrl ID="CustomJSUrl1" runat="server" />
<SharePoint:SoapDiscoveryLink ID="SoapDiscoveryLink1" runat="server" />
<asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server"/>
<SharePoint:DelegateControl ID="DelegateControl1" runat="server" ControlId="AdditionalPageHead" AllowMultipleControls="true"/>
<style>
body
{
background-image:url(/_layouts/images/corp/corpLogo.gif);
background-repeat:no-repeat;
}
</style>
</HEAD>
...
When deployed as a feature, this master page works fine for all of our standard out of the box pages and sites - the logo appears in the top left hand corner (and the code also applies a colour scheme using a separately-defined style sheet).
However, when we try to modify a piece of content using a CEWP, the rich text editor also displays this logo in the top left corner, partially obscuring the text the user needs to edit.
I have tried to amend the style in the master page to hide this logo but cannot find a way to do this. I have also looked into amending the
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\htmleditor.js
and the actual rich text editor itself at
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\RTE2PUEditor.aspx
but this has not led to any success (and I would really rather not do this anyway!!).
Can someone point me in the right direction?
I would like a custom branding (logo, css etc.) applying to all pages, but not to the pop up rich text editor. Is this possible? If so, how?
The RTE2_GenerateLaunchArguments method in HtmlEditor.js copys all of the stylesheets and styles on the page:
var styleRules=new Array();
var styleRulesUrl=new Array();
for (var i=0; i<document.styleSheets.length; i++)
{
var styleSheet_href=document.styleSheets[i].href;
if (styleSheet_href !="")
{
styleRulesUrl[styleRulesUrl.length]=styleSheet_href;
}
else
{
var rules=document.styleSheets[i].rules;
for (var j=0; j<rules.length; j++)
{
styleRules[styleRules.length]=rules[j];
}
}
}
These values are then passed and applied to the modal dialog RTE2PUEditor.aspx. It looks like the quickest work around is to use the form instead of the body tag:
form#aspnetForm
{
background-image:url(/_layouts/images/corp/corpLogo.gif);
background-repeat:no-repeat;
}
The ID of the form on RTE2PUEditor.aspx is RTE2PUEditorForm, while most of the other pages (especially those using your master page) use aspnetForm.
That said, you might want to look into setting the site logo instead of using CSS. You can set the logo manually by going to Site Settings > Title, Description, and Icon > Logo URL and Description and changing the URL. Or you can set it through code using the SPWeb.SiteLogoUrl property. Note, this requires that the SiteLogoImage control is present on your custom master page.

input losing src

If I use the following code without runat="server" the input's src works fine and I see the image coming through.
<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" /></div>
url is https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image
But if I put the runat="server" in, for some reason, I get this for the url:
<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" runat="server" /></div>
url is http://localhost/%3C%=lButtonImageUrl%20%%3E
You cannot use the <%= %> syntax with server controls (including standard HTML elements with runat="server"). You have two choices:
Access the control in the code behind (as an HtmlInputControl) and assign the src attribute using the Attributes property: imageControl.Attributes["src"] = value;
Assign the attribute using the databinding syntax (src="<%# %>") and call imageControl.DataBind() from the code behind
Maybe I'm missing something. But runat server tag does not support code expression.
When you add runat="server to that html tag, Asp.Net converts it from string to HtmlControl - in this case of type HtmlInputImage. You can see this happen by adding:
<%= testButton.GetType() %>
Then the only thing you need to do is set the Src-property, which, contrary to other comments, you CAN do in inline aspx - no need for a code-behind file:
<%
testButton.Src = "/content/logo.png";
%>
<input id="testButton" type="image" runat="server" src="" onserverclick="RedirectTest" />
You need to set the Src-property BEFORE the actual input, which is a bit non-intuitive, the reason is that the code is run at render-time, so if the setting of Src-property is after the control, it is too late.
if jQuery is an option than you could try this:
<script type="text/javascript">
$(function() { $('#<%=testButton.ClientID %>').attr('src', '<%=TestButtonImageUrl %>'); });
</script>
...
<div><input id="testButton" runat="server" type="image" onserverclick="RedirectTest" /></div>
Update:
Another option is to create a HttpHandler with processing like this
public void ProcessRequest(HttpContext context)
{
var testButtonImageUrl = "https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image";
context.Response.Redirect(testButtonImageUrl);
}
add in web.config path to handle image.img or whatever and update aspx
<div><input id="testButton" runat="server" type="image" src="image.img" onserverclick="RedirectTest" /></div>

Resources