I have a bunch of static links currently within a div, however Im after changing the order of the links on page load.
I've considered using a literal and a loop through the links in code behind but im stumped. Maybe a repeater... I need a push in the right direction please!
Im fairly new to this so any help would be much appreciated. Thanks
(c# or vb.Net)
ok so lets say that you would store the links in a Links.txt file because there could be hundreds of links for example..
1. Create the .txt file
2. Make note of the file location.
3. Save the file with the links in them
4. use this code in your project to load the links
List<string> strUrlLinks = new List<string>(File.ReadAllLines(FilePath + FileName.txt));
in code you can then hover over strUrlLinks and you will see the Links
to access the individual links do it based on it's [Index] position
I know you asked for an answer in c# or vb.net and may have some technical restraints on this, but to create this functionality in jQuery is almost trivial considering the links are already hardcoded on the page.
See the example below which sorts in descending order on the link text.
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="linksContainer">
Line 1
Line 3
Line 2
</div>
<script type='text/javascript'>
$(window).load(function(){
var orderDivLinks = function(desc) {
$('div#linksContainer').children().detach().sort(function(a,b) {
var compA = $(a).text();
var compB = $(b).text();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
}).appendTo(document.body);
}
orderDivLinks(0);
});
</script>
</body>
</html>
Related
I am generating a pdf, and my employer asked me to add a footer to every single page in the pdf. The pdf gets generated with Rotativa, with a html page that turns into a pdf.
My only problem is, how am i gonna do this. I thought about some ideas, plain css code, although there are more then enough options, none of them really worked out for me. Especially because i can have 1 page, or i can have 10, depends on the amount of information that has to be put onto it.
So the footer has to be responsive to the amount of pages that will be generated.
Does anyone have any ideas for me? I want to get this worked out really bad, but i just have no clue how to.
Greetings
Lorenzo Otto
If I have not misunderstood your question, you could simply use an HTML page as a footer, to display the page footer, involving some javascript to display the current page and total number of pages. I'am actually using this approach for several web applications using Rotativa that generate PDF files based on MVC views with dynamic content.
Supposing we create a Footer.html (use the name you prefer here), this could be the HTML file for the PDF footer, including javascript code to generate page number info:
<!DOCTYPE html>
<html>
<head>
<script>
function subst() {
var vars = {};
var x = window.location.search.substring(1).split('&');
for (var i in x) { var z = x[i].split('=', 2); vars[z[0]] = unescape(z[1]); }
var x = ['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection'];
for (var i in x) {
var y = document.getElementsByClassName(x[i]);
for (var j = 0; j < y.length; ++j) y[j].textContent = vars[x[i]];
}
}
</script>
</head>
<body onload="subst()">
<div style="font-family: Helvetica; font-size:0.75rem; text-align: right; width:100%;">
<span class="page"></span> / <span class="topage"></span>
</div>
</body>
</html>
*For sure, you can change the footer div style as you wish, in order to suit your desing needs.
And on the other side, guessing the footer HTML page (called Footer.html before) is located in a folder called Shared, inside Views folder, you could this to prepare the required custom switches before calling Rotativa:
string customSwitches = "--footer-html " + Server.MapPath("~/Views/Shared/Footer.html");
So, if you are using an MVC action to generate a PDF based on a View called MyPdfView.cshtml, you can make uso of ViewAsPdf, passing it your custom switches, this way:
return new ViewAsPdf("MyPdfView")
{
FileName = "MyPdfView.pdf",
CustomSwitches = customSwitches
};
And that's all. Please let me know if this worked for you.
There are many similar questions already, but for some reason they do not solve my issue.
My project is to save a dynamically created page (or part of the page). The user will have the option to drag objects on screen and leave them in any order and then for this order to be saved to disc as an image.
My own research shows that I can use Draggable from JQuery and this is easy to implement. It is the saving part where I am stuck.
I followed a few links, but the image does not appear to be saving to disc and I don't think it works in IE! However, I'm happy with FireFox and so using the toDataUrl() sounds like the best approach.
Since I couldn't get it work I decided to change tactic slightly and pass the value to the code behind (C#)
The save part of my code looks like
<script>
function SaveMe() {
var canvas = document.getElementById("mycanvas");
var image = canvas.toDataUrl();
document.getElementById("hideMe").value = image;
}
</script>
When I debug in FireBug, I never reach document.getElementById("hideMe").value = image;. I step through but can never get past var image = canvas.toDataUrl(); Is this expected behavior as I assume not but there is no error message?
The body of my HTML is
<body>
<script type="text/javascript">
$(document).ready(function () {
$('.popup_click').show(0).draggable();
});
</script>
<form id="form1" runat="server">
<div class="popup_click">
<div id="popup_title">Title</div>
</div>
<canvas id="mycanvas" class="canvas"></canvas>
<asp:HiddenField ID="hideMe" runat="server" />
<asp:Button runat="server" OnClick="ClickMe" text="Click" OnClientClick="SaveMe()"/>
</form>
</body>
What am I doing wrong? I'm working locally in debug mode, Visual Studio 2013.
Not absolutely sure, but I also had a problem with todataurl() in the past and this link helped me
Not able to get data url when using images in kinetic js
This is the function I always use when wanting to export a canvas to image, it puts the image into a popup window so the user can do what they want with it (I hope this helps):
function imageCanvas(evt){
var imageCanvas = document.createElement('canvas');
imageCanvas.width = document.documentElement.clientWidth;
imageCanvas.height = document.documentElement.clientHeight;
var imageCanvasContext = imageCanvas.getContext("2d");
imageCanvasContext.fillStyle = document.getElementById("body").style.backgroundColor;
imageCanvasContext.fillRect(0,0,imageCanvas.width,imageCanvas.height);
imageCanvasContext.drawImage(mainCanvas,0,0,imageCanvas.width,imageCanvas.height,0,0,imageCanvas.width,imageCanvas.height);
var dataURL = imageCanvas.toDataURL("image/png");
var imagePopup = window.open("","fractalLineImage","left=0,top=0,width="+(imageCanvas.width/2)+",height="+(imageCanvas.height/2)+",toolbar=0,resizable=0");
imagePopup.document.write("<title>Export Image</title>");
imagePopup.document.write("<img id='exportImage'"
+" alt=''"
+" height='"+ imageCanvas.height+"'"
+" width='"+ imageCanvas.width+"'"
+" style='position:absolute;left:0;top:0'/>");
imagePopup.document.close();
var exportImage = imagePopup.document.getElementById("exportImage");
exportImage.src = dataURL;
}
I've been trying to add the plus one button on our company's product page. We have a multi - subdomain website which has language translated content for that particular subdomain. The user's language preference is remembered via cookies.
Now when when I hit the +1 button, and try to share the page on google+ I do not see the translated description come up on it. It somehow grabs the "English" description. When I try to look at my "MetaDescription" tag it is in the foreign language.
What I've been guessing is that google was trying to call the URL I was trying to share and crawling it instead of crawling the very page I was clicking the +1 button on. What would be the best way to make google detect the language setting on the page i want to share?
To get the API to load in the different languages, you should specify the lang in the config. Do this BEFORE the plusone.js can load. That should make the button and it's screens display with that language.
Here's an example:
<html>
<head>
<title>+1 Demo: Async render</title>
<link rel="canonical" href="http://www.example.com" />
</head>
<body>
<g:plusone></g:plusone>
<script type="text/javascript">
window.___gcfg = {
lang: 'zh-CN'
};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
</body>
</html>
However, for the share part of it, can you be sure that when Google scrapes the page you're sharing that it is picking up the correct language? Since the user agent Google is using might not specify a language, your page is being rendered to Google's scrapers as the default language. Maybe you should have a canonical URL for each language for your page that google can scrape correctly.
I use asp.net 4 and c#.
I have a Web User Control inside a Web From page.
When I include the Web User Control I would like also include programmatically some script within the tags for the final generated page.
Any idea how to do it?
Maybe ScriptManager could help on this?
Please provide me a sample of code I'm pretty new at developing thanks for your time on this!
Peter's answer will add to the page, but not the head element. Take a look at this article:
http://weblogs.asp.net/johnkatsiotis/archive/2008/08/08/add-scripts-to-head-dynamically.aspx
It provides a nice clean way to add a script to the head element using an extension method.
I realize that this is an old question, but there is a much simpler way.
Try This:
Page.Header.Controls.Add(
new LiteralControl(
"<script>alert('Literal Added to <Head>.');</script>"
)
);
If you want to add the script at a particular index of the <head> you can use
AddAt(index, new LiteralControl(...)) where index 0 equals the top of the <head>
Also, joelmdev mentioned in a comment you need to add runat="server" in your head tag e.g. <head id="head1" runat="server">
Take a look at RegisterClientScriptBlock of the ClientScriptManager.
From MSDN:
...
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(csType, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> function DoClick() {");
csText.Append("Form1.Message.value='Text from client script.'} </");
csText.Append("script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
}
...
I have a web site released at facebook platform, i am using C# .Net 2008, the problem is that i am loading an array elements from code behind in javascript arary and i am loading elelment element using the following javascript code :
ArList = new Array('<%=ListOfWords[0]%>','<%=ListOfWords[1]%>','<%=ListOfWords[2]%>','<%=ListOfWords[2]%>');
the problem that when i call an element from the array as follows :
document.getElementById("WordDiv").innerHTML = ArList [0];
The element is not set with the value inspite the array in the code behind has values and i don't know why the value of the array element is not set ? and in some cases i found it loaded with value and everything go right so may be it's a problem in rendering so the value of code behind is not seen in client side ? or where is the problem come ?
and when i traced the application in IE i found the status bar report a javascript error then disappered and say done and when the yellow allert appears in the status bar i have clicked it and noticed the message says : object expected.
The problem now is that in onload event of the body tag i call a function in the javascript that intialize the javascript array with values from code behind array and the problem is that in some cases the function is not entered as i traced it by putting an alert in the begining of the function and i found that when the javascript array is not filled the code of the function is not entered as the alert is not showed so i don't know how to force the DOM to enter this function which i call it in the tag here sample of the code:
javascript code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body onload="IntializeArr(); return false;">
<form id="form1" runat="server" >
..........Some Controls...............
</form>
<script type="text/javascript" language="javascript">
var TList;
var BList;
function IntializeArr()
{
TList = new Array('<%=ListofT[0]%>','<%=ListofT[1]%>','<%=ListofT[2]%>','<%=ListofT[3]%>','<%=ListofT[4]%>','<%=ListofT[5]%>','<%=ListofT[6]%>','<%=ListofT[7]%>','<%=ListofT[8]%>','<%=ListofT[9]%>');
BList = new Array('<%=ListOfB[0]%>','<%=ListOfB[1]%>','<%=ListOfB[2]%>','<%=ListOfB[3]%>','<%=ListOfB[4]%>','<%=ListOfB[5]%>','<%=ListOfB[6]%>','<%=ListOfB[7]%>','<%=ListOfB[8]%>','<%=ListOfB[9]%>');
}
</script>
</body>
</html>
C# Code :
public string[] ListOfB = new string[15];
public string[] ListofT = new string[15];
And the code behind array is filled from data returned from database and they are filled i have traced them and each time and they are filled and i found that the problem from javascript i don't know if it is from facebook platform or from my code but i think that it is not from my code as the problem that i call a function in the onload of the tag and the function is not entered and this is the problem so can any one help me please
Hope that i will find a solution as i got depressed
If the IntializeArr() function is not being called, it is most likely a javascript error in the page, and most likely caused by some invalid javascript characters in the ListofT or ListofB arrays.
You should be escaping any single quotes or newlines.
Easiest way to check is to just "view source" in the browser once the page has loaded. Look down to your script and find what values were put it and you should see if there were any errors.
Maybe ListofWords[0] contains an apostrophe?
Are you outputting the JS array creation to the page inside <script> tags?
You have to emit that javascript so that the browser can run it - then ArList will be available on the page to other javascript.
Agree, more code example is needed.
Usually I solve something like this by using Sys.Application.add_init() or $(document).ready(). However there are other possible solutions. Use an AJAX request to get the array. Or something like:
function setInnerHtml() {
if (typeof(ArList) != "undefined") {
document.getElementById("WordDiv").innerHTML = ArList[0];
} else {
setTimeout("setInnerHtml()", 40);
}
}