Escaping inline code block in Asp.Net template - asp.net

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";
}

Related

how to show html result view in wordpress post

i want to show html code in textarea and then user can clink view resut.
code:
<textarea id="html" cols="50" rows="10"><html>
<body>
<p>
This text will appear on your webpage.
</p>
<!--
the text within these comment tags will not appear on the web page
-->
</body>
</html>
</textarea>
<div>
<button onclick="view()">View Results</button>
</div>
<script type="text/javascript">
function view()
{
var result = window.open("", "", "height=400,left=" + ((screen.width) ? (screen.width-400)/2 : 0) + ",top=" + ((screen.height) ? (screen.height-400)/2 : 0) + ",width=400");
var tmp = result.document;
tmp.write(document.getElementById('html').value);
tmp.close();
return false;
}
</script>
i try post it on wordpress post, but not working.
WordPress strips out HTML tags by default. An easy way to begin with would be to use a plugin such as https://wordpress.org/plugins/wp-coder/ to paste your code into and then use a shortcode to run it. Ideally you want to look into building your own plugins, but as a quick fix this will be ideal for you :)

ASP include file into variable

I have a template.aspx file the contains code like this:
<html>
....
<body>
<div id="wrapper">
<div id="container">
----sideMenu---
.........
<div id="body">
<%=pageContent%>
</div>
</div>
</body>
</html>
I have a file index.aspx
with the code:
<%
Dim pageContent = ""
%>
<!--#include virtual="template.aspx"-->
I want to insert in pageContent a lot of html code and asp code
without connecting strings like this:
pageContent = "<div class..." &_
" bla bla bla " & aspVariable &_
"more divs and html and asp variables"
.
.
.
actually i want a dyanmic pages i dont want write the template html codes each pages i want that only the pageContent are change.
It would be more common to create a handful of template files to be included in each page of your ASP app. Such as:
header.inc (might contain site, banner... top of page stuff)
topNav.inc (might contain code to generate the top navigation bar)
leftNav.inc (take a guess ;))
bottomCopy.inc (might have text links as site map and copyright statement.
Then, in each page, include all where they should live. For example, in my page called "theFirst.aspx", I might have:
<!-- #include file = "cacheprevent.inc" -->
<!-- #include file = "accesscheck.inc" -->
<!-- #include file = "header.inc" -->
<!-- #include file = "topNav.inc" -->
<!-- #include file = "leftNav.inc" -->
<%
' Here goes the rest of your code to generate content for this specific page
%>
<!-- #include file = "bottomCopy.inc" -->
All this assumes it is in ASP-Classic, as you've tagged your post, at least in part.
... and you could in some cases, combine the top four... if all pages would have the same layout regions.

Passing image source to aspx code behind

I'm trying to use Adobe's new Aviary/Feather SDK with an existing aspx webpage.
Here is the HTML for the Feather integration.
you will notice that i had to add https://test.mywebsite.com before the <% imgEvent.imageUrl %> . Without the website url appended to the beginning the Feather would not work properly.
ASPX page
<div class="span12">
<div id='injection_site'></div>
<asp:Image ID="imgEvent" runat="server" />
<p><input type='image' src='http://images.aviary.com/images/edit-photo.png' value='Edit photo' onclick="return launchEditor('imgEvent', 'https://test.mywebsite.com<%= imgEvent.ImageUrl.ToString()%>');" /></p>
</div>
This will result in the following
HTML page
<div class="span12">
<div id='injection_site'></div>
<img id="imgEvent" src="/image/get/webfile/80a556a0-c251-4fc5-b6e2-9061d8f90f18" />
<p><input type='image' src='http://images.aviary.com/images/edit-photo.png' value='Edit photo' onclick="return launchEditor('imgEvent', 'https://test.mywebsite.com/image/get/webfile/80a556a0-c251-4fc5-b6e2-9061d8f90f18');" /></p>
</div>
The problem seems to be that once Feather is used and the image has been affected with the desired effects, the Html is updated like so
<img id="imgEvent" src="http://featherfiles.aviary.com/2014-10-27/1d6ab55fcc2ecaf5/f304c34d77bc446aa5fb6278dcbea5da.png">
This is because i assume the file is temporarily being stored on their website for the preview. How would i go about downloading this file using the ASPX code behind method?
If i do Me.imgEvent.ImageUrl it will give me the url that was associated to the img on page_load. (Me.imgEvent.imageUrl = "/image/get/webfile/80a556a0-c251-4fc5-b6e2-9061d8f90f18")
Is there a way to pass the new img src parameter to the code behind?
You can make an ajax call to your server with the new url as a parameter. For instance, using the jQuery library, and assuming you have an aspx page with a POST handler, you could do the following:
function onSave(id, url) {
$.post( "saveImage.aspx", { newUrl: url });
}

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