jsp function tag to replace ' - css

Here is my code,
css/index.jsp:
<%#include file="/WEB-INF/common/css/common.jsp" %>
<style type="text/css">
body{background: #ffffff url('<c:url value='/resources/images/logo/logo_small.png'/>') no-repeat scroll center center}
</style>
index.jsp page calls the above css/index.jsp page:
<c:set var="my_css">
<c:if test="${branch == 'sitemap'}">
<c:if test="${page == 'index'}">
<%#include file="/WEB-INF/common/css/sitemap_common.jsp" %>
<%#include file="/WEB-INF/common/css/index.jsp" %>
</c:if>
</c:if>
</c:set>
<c:set var="css" value='${fn:replace(fn:replace(fn:replace(my_css,"<style type=\\\"text/css\\\">", ""),"</style>", ""),"\'", "99999")}'/>
<compress:css enabled="true">
<c:out value='${css}'/>
</compress:css>
In my project there are 100's of jsp pages that return css code. Why i'm doing this is to place my css as a link tag in head section.
In my code ' is replaced with 99999, but when i change 99999 to & #.. ; return & amp;#..; how to replace?
If there is a better option to convert my jsp to make it as a css link in html head section, please post here. You please post your suggestions too.

now i need to modify for better performance
Modify the the css with some tool or maybe even with some simple java program before you upload it to the server.
Then:
you do not need to spend time with this problem
the performance is much better (you do not need any replace stuff while runtime for every request)

Related

How to prevent blank page getting added at the end of pdf report due to page break

I am printing the pdf report where i will be looping each student and i am applying page-break after each student
And my applied page break code is as below
<style type="text/css" >
.page-break { display:block; clear:both; page-break-after: always;}
</style>
And my view is
<% #students.each do |student| %>
<%= student.first_name %>
<div class="page-break"></div>
<% end %>
Problem is i am getting empty page at last in pdf report, i am using wicked-pdf gem for pdf generation i think problem is with page-break css can anyone help me
Quick and dirty solution, but should work fine:
<% #students.each do |student| %>
<%= student.first_name %>
<% if student.id != #students.last.id %>
<div class="page-break"></div>
<% end %>
<% end %>
Also if you can show us the html container of the each loop, we can give a better answer depending on this one.
Just dont add the class that gives page break on your last loop, that way "page-break-after: always" wont take effect

asp.net code displays code itself instead of output in browser

The following code displays as it is in browser instead of expected output. I have the table in sql server 2008. I have this code in webmatrix.
<%# Page Language="VB" %>
#{
var db = Database.Open("databasename"); 'database name in sql server is 'databasename'
var selectQueryString = "SELECT column_date, sum(qty1) as quantity1,
sum(qty2) as quantity2, sum(qty3) as quantity3
from tbldaily group by column_date order by column_date";
}
<!DOCTYPE html>
<html>
<body>
<h1>First table test</h1>
<table>
<tr>
<th>at_date</th>
<th>quantity1</th>
<th>quantity2</th>
<th>quantity3</th>
</tr>
#foreach(var row in db.Query(selectQueryString))
{
<tr>
<td>#row.column_date</td>
<td>#row.quantity1</td>
<td>#row.quantity2</td>
<td>#row.quantity3</td>
</tr>
}
</table>
</body>
</html>
I feel there is mistake somewhere in this code. Can you help me to fix this? Please let me know if you want more information regarding my database structure or table that may help to understand my scenario better
Looks like you are mixing Razor syntax with WebPages. Name your file *.cshtml and remove the first line which is for WebPages. The rest of your code is valid C# razor.
I think he needs the opposite of that. Keep it .aspx.
So wherever you have # tags to mark the asp code, use aspx escapes instead
<% For i As Integer = 0 To .... %>
<tr> <td> <%= row.column_date %> </td> ......
<% Next%>

Escaping inline code block in Asp.Net template

I have a page where I wish to render the following html (a small JS template)-
<script type="text/html" id="lightbox-template">
<div id="lightbox-background"></div>
<div id="lightbox"><%= content %><div class="bottom"></div></div>
</script>
However, the Asp.NET preprocessor is picking up on the "<%=" tag and trying to interpret it. I wish to escape this tag to allow it to be rendered, preferably from the template rather than the code behind. Is this possible?
I have managed to do this via a Literal control and setting it's text in the code behind.
I ideally wanted to keep it within the aspx page. This is the best solution I could find (from here), which creates splits the closing > into a separate string
<script type="text/html" id="lightbox-template">
<div id="lightbox-background"></div>
<div id="lightbox"><%= "<%= content %" + ">" %=><div class="bottom"></div></div>
</script>
Important bit: <%= "<%= content %" + ">" %=>
This goes into aspx
<div> <%= GetContentString() %> </div>
This goes into aspx.cs
protected String GetContentString()
{
return "this is a content";
}

how to insert image into email template

I'm trying to use the PasswordRecovery of ASP.NET.
Everything works fine, however I am using Email template. Within this email I'm trying to insert an image as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<img alt="blabla" src="/Images/blabla-logo.png" align="middle"/><br/><br/>
bla bla:<%Password%><br /><br />
</body>
</html>
As I said, the email is being sent fine but the image is not inserted. I tried: src="~/Images/blabla-logo.png", but with no success.
Idea anyone?
Thanks a lot,
Assaf.
For email you should not give relative path like "/Images/blabla-logo.png" the only works for the internal website pages, instead of this you should give the complete path like
http://youserver/youapp/Images/blabla-logo.png
I will suggest you not to include image using the path instead of this try embedding the image in your email. You can achieve this by converting your images to base64 string and set the base64 string as the source of the image.
You can use OnSendingMail event to modify your email message. Let's assume your template look like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<img alt="blabla" src="{0}" align="middle"/><br/><br/>
bla bla:<%Password%><br /><br />
</body>
</html>
You PasswordRecovery markup should look like this:
<asp:PasswordRecovery ID="prPasswordRecovery" runat="server" OnSendingMail="prPasswordRecovery_SendingMail">
<MailDefinition BodyFileName="~/passwordRecoveryEmailTemplate.txt" IsBodyHtml="true" Priority="High" Subject="bla bla"/>
</asp:PasswordRecovery>
Last thing to do is to write prPasswordRecovery_SendingMail method in code behind:
protected void prPasswordRecovery_SendingMail(object sender, MailMessageEventArgs e)
{
e.Message.Body = String.Format(e.Message.Body, ResolveClientUrl("~/Images/blabla-logo.png"));
}
That should do it.
try adding a tilde "~", an id and runat="server". The tilde only gets changed to the root path when runat="server" is applied. Otherwise, the serverside code has no knowledge of the control and doesn't parse it and apply the path insertion
<img alt="blabla" src="~/Images/blabla-logo.png"
align="middle" id="img" runat="server"/>
Have you tried using AlternateView?
One example is here.

Using embedded standard HTML forms with ASP.NET

I have a standard aspx page with which I need to add another standard HTML form into and have it submit to another location (external site), however whenever I press the submit button the page seems to do a post back rather than using the sub-forms action url.
A mock up of what the form relationships is below. Note in the real deployment the form will be part of a content area of a master page layout, so the form needs to submit independantly from the master page form.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<form id="subscribe_form" method="post" action="https://someothersite.com" name="em_subscribe_form" >
<input type="text" id="field1" name="field1" />
<input id="submitsubform" type="submit" value="Submit" />
</form>
</div>
</form>
</body>
</html>
It's an interesting problem. Ideally you only want the 1 form tag on the page as other users have mentioned. Potentially you could post the data via javascript without having 2 form tags.
Example taken from here, modified for your needs. Not 100% sure if this will work for you but I think this is how you'll have to approach it.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function postdata()
{
var fieldValue = document.getElementById("field1").value;
postwith("http://someothersite.com",{field1:fieldValue});
}
function postwith (to,p) {
var myForm = document.createElement("form");
myForm.method="post" ;
myForm.action = to ;
for (var k in p) {
var myInput = document.createElement("input") ;
myInput.setAttribute("name", k) ;
myInput.setAttribute("value", p[k]);
myForm.appendChild(myInput) ;
}
document.body.appendChild(myForm) ;
myForm.submit() ;
document.body.removeChild(myForm) ;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<input type="text" id="field1" name="field1" />
<asp:Button ID="btnSubmitSubscribe" runat="server" Text="Submit" OnClientClick="postdata(); return false;" />
</div>
</div>
</form>
</body>
</html>
If javascript is not a viable option - you can use .Net's HttpWebRequest object to create the post call in code behind. Would look something like this in the code behind (assuming your text field is an asp textbox:
private void OnSubscribeClick(object sender, System.EventArgs e)
{
string field1 = Field1.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="field1="+field1 ;
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://someotherwebsite/");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
If you add an ASP.NET button to the form, and set its PostBackUrl property to the external site, then all the form data will be posted to that URL.
There is a very nice tricky solution for this problem.
You can insert a </form> tag before your <form> to close the asp.net form which causes the problem. Do not forget to add a <form> tag after your html form. It may cause the editor to give you an exception, but do not worry, it will work.
Nested forms are not possible in HTML according to the W3C. You can achieve your intended result using JavaScript or with jQuery as explained by Peter on a blog called My Thoughts.
In my experience, Appetere Web Solutions has the best solution. Simple and elegant...and it's not a hack. Use the PostBackUrl.
I just tried it and everything works as expected. I didn't want to use Javascript because I didn't want to include it in my Master Page for every page that uses it.
I had the same situation as Ross - except that my input types were all of the "hidden" varitey.
Cowgod's answer got me thinking about nested forms within my .aspx page. I ended up "un-nesting" my 2nd form OUT of the main .aspx form ( ) and placed it (along with my js script tags) just under the body tag - but before the main .aspx form tag.
Suddenly, everything was submitting as it was supposed to. Is this a hack?
ASP.NET allows you to have multiple forms on one page, but only one can be runat=server. However I don't think you can nest forms at all.
You might have to make a new master page, one without a form tag on it so the form will work on that one page only. This is not a good solution, unless you can place the form outside the master pages' form, and use javascript to submit the second form, but that's hardly better. There really is no good solution for what you are trying to achieve, and if so I'd like to hear it. I don't think you can do a POST call from a code-behind, can you? I'm sure there's some way. But that's probably the only solution: from code.

Resources