How to replace this HTML snippet in some text? - c#

I am using ASP.NET 4.5 and C#
I want to replace :
<div style="page-break-after:always"><span style="display:none"> </span></div>
in a string with:
<br style="page-break-after: always;" />
I have tried:
//string strContent is a string that contains HTML content
strContent = strContent.Replace("<div style='page-break-after:always'><span style='display:none'> </span></div>", "<br style='page-break-after: always;' />");
But with no success. Obviously I am missing something simple.
Help hugely appreciated.

You mixed up the " and ':
strContent = strContent
.Replace("<div style=\"page-break-after:always\">" +
"<span style=\"display:none\"> </span></div>",
"<br style=\"page-break-after: always;\" />");

Related

Tags Selectively Stripped From Html Elements from Source

We have an asp.net webform pulling markup from a database and sending to browser to render as html.
Html is being selectively stripped from html tags from only some of this markup and is instead displaying as plain text.
I have checked source and all of the tags are there so it's unlikely to be a server issue with the asp controller (I checked anyway, see below) - I have checked the validity of the html at validator.w3c.org and there is nothing the browser should be removing as invalid.
The controller on the webform is asp:literal so I thought it might have been in the wrong mode, however I have played around with this and there is no improvement. Also, some of the html was being rendered correctly anyway so this was always unlikely to be the issue.
I think it might have something to do with a network request overriding some styling as it initially renders correctly but as the page loads additional paints remove the tags from displayed page. Previews of the module in question in network tab also render correctly.
Ok, so the code in source is:
<div class='classname class'>
<div class='classname'>
<div class='classname'><img id = 'myImg' src='https://imgurl.com' alt='A Description'></div>
</div>
<div class='classname'>
<h1>A Heading of My Subject</h1>
<p class='classname'>And then some more.</p>
</div>
<div id='myId'>
<div class='classname'>
<h1> A Heading of My Subject </h1>
<p>Stripping subject from text.</p>
<h2> A Secondary Heading for More Info</h2>
<p>An abstraction of my paragraph text.</p>
<h2> Another Secondary Heading </h2>
<p>Lorem Ipsum forever!</p>
<p>And yet another paragraph.</p>
</div>
</div>
</div>
The actual html rendered is:
<div class='classname class'>
<div class='classname'>
<div class='classname'><img id = 'myImg' src='https://imgurl.com' alt='A Description'></div>
</div>
<div class='classname'>
<h1>A Heading of My Subject</h1>
<p class='classname'>And then some more.</p>
</div>
<div id='myId'>
<div class='classname'>
" A Heading of My Subject
Stripping subject from text.
A Secondary Heading for More Info
An abstraction of my paragraph text.
Another Secondary Heading
Lorem Ipsum forever!
And yet another paragraph."
</div>
</div>
</div>
It is curious that the double quotes are being added also, not sure what is going on there.
The relevant code-behind is:
Page.Title = Name + " - " + Var;
LocOutput.Append("<div class='classname'>" + Environment.NewLine);
LocOutput.Append("<div class='classname'>" + Environment.NewLine);
LocOutput.Append("<div class='classname'><img id='asd' src='" + imgurl + "' alt='" + imgdescription + "'></div>" + Environment.NewLine);
LocOutput.Append("</div>" + Environment.NewLine);
LocOutput.Append("<div class ='classname'><h1>" + Name + "</h1><p class='classname'>" + Name2 + "</p></div>" + Environment.NewLine);
LocOutput.Append("<div id = 'idNameo'><div class ='classname'>" + Content + "</div></div>" + Environment.NewLine);
if (myVar == "condition")
{
LocOutput.Append("<div id = 'idNameod2' value = '" + Var3+ "'></div>" + Environment.NewLine);
}
LocOutput.Append("</div>" + Environment.NewLine);
LocationDetails.Text = LocOutput.ToString();
The stripped away html is located entirely within the Content variable.
The control is :
<asp:Literal ID="LocationDetails" runat="server"/>
The cause in this case was some javascript stripping html as part of an insertion of data following an AJAX call:
var desc = $('#elemId').find('.more').text();
desc = desc.replace("(random data)", randomDataVar);
The method was accessing inner text but after the replacement was inserting it back into innerHTML.
I accessed innerHTML directly to solve this issue:
var desc = document.getElementsByClassName('more')[0];
desc.innerHTML = desc.innerHTML.replace("(random data)", randomDataVar);

Realex HPP request -custom field

I'm trying to send a custom field to realex to use upon successful response. The field in question is a json array.
[{"InvoiceId":7,"AmountPaid":180,"SucessfulPayment":false}]
The problem I face is upon response the array is truncated to [{. I have this working by base 64 encoding the array but feel its a bit heavy and would rather send the raw json. I'm also concerned there may be a character restriction on the field length's which would get eaten up quick. If there's no restriction on length I'll probably go base 64.
Update
Code as requested.
JSON array passed into request object
[{"InvoiceId":7,"AmountPaid":180,"SucessfulPayment":false}]
realex response returning
[{
Code:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="RealexRedirectTestPay.aspx.cs"
Inherits="SmartClubOnline.RedirectTest" %>
<!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">
<title></title>
</head>
<body onload="document.form.submit();">
<div class="content">
<form name="form"
action="https://hpp.sandbox.realexpayments.com/pay" method="post">
<asp:Literal ID="MERCHANT_ID" runat="server" />
<asp:Literal ID="SECRET_ID" runat="server" />
<asp:Literal ID="CLUB_NAME" runat="server" />
<asp:Literal ID="ORDER_ID" runat="server" />
<asp:Literal ID="CURRENCY_ID" runat="server" />
<asp:Literal ID="CURRENCY" runat="server" />
<asp:Literal ID="AMOUNT" runat="server" />
<asp:Literal ID="TIMESTAMP" runat="server" />
<asp:Literal ID="MEMBER_ID" runat="server" />
<asp:Literal ID="MEMBER_USERNAME" runat="server" />
<asp:Literal ID="MEMBER_EMAIL" runat="server" />
<asp:Literal ID="INVOICE_ID" runat="server" />
<asp:Literal ID="MEMBERPURSEID" runat="server" />
<asp:Literal ID="SHA1HASH" runat="server" />
<asp:Literal ID="TempOrderNumber" runat="server" />
<asp:Literal ID="PromoRow" runat="server" />
<asp:Literal ID="DELAYEDSETTLEMENTON" runat="server" />
<asp:Literal ID="CREDITCARD_SHG_AMT" runat="server" />
<asp:Literal ID="DEBITCARD_SHG_AMT" runat="server" />
<asp:Literal ID="AUTO_SETTLE_FLAG" runat="server" />
<asp:Literal ID="MERCHANT_RESPONSE_URL" runat="server" />
<!--<input type="hidden" name="AUTO_SETTLE_FLAG" value="1" />-->
<h3>
Redirecting you to our Payment Provider</h3>
<p>
You are being securely redirected to our payment provider...Please Wait...</p>
<!-- uncomment if you want the user to click a button to continue. Also remove the onload function in the Body tag -->
<!-- <p><input type="submit" name="submit" value="continue to payment provider" /></p> -->
</form>
</div>
</body>
</html>
Server side request:
MERCHANT_ID.Text = "<input type=\"hidden\" name=\"MERCHANT_ID\"
value=\"" + merchantID + "\" />";
SECRET_ID.Text = "<input type=\"hidden\" name=\"SECRET_ID\"
value=\"" + sharedSecret + "\" />";
CLUB_NAME.Text = "<input type=\"hidden\" name=\"CLUB_NAME\"
value=\"" + CLUBNAME + "\" />";
ORDER_ID.Text = "<input type=\"hidden\" name=\"ORDER_ID\"
value=\"" + orderID + "\" />";
CURRENCY.Text = "<input type=\"hidden\" name=\"CURRENCY\"
value=\"" + currencyCode + "\" />";
CURRENCY_ID.Text = "<input type=\"hidden\" name=\"CURRENCY_ID\"
value=\"" + currencyCode + "\" />";
AMOUNT.Text = "<input type=\"hidden\" name=\"AMOUNT\" value=\""
+ amount + "\" />";
TIMESTAMP.Text = "<input type=\"hidden\" name=\"TIMESTAMP\"
value=\"" + timestamp + "\" />";
MEMBER_ID.Text = "<input type=\"hidden\" name=\"MEMBER_ID\"
value=\"" + MEMBERID + "\" />";
MEMBER_USERNAME.Text = "<input type=\"hidden\"
name=\"MEMBER_USERNAME\" value=\"" + MEMBERUSERNAME + "\" />";
MEMBER_EMAIL.Text = "<input type=\"hidden\"
name=\"MEMBER_EMAIL\" value=\"" + MEMBEREMAIL + "\" />";
INVOICE_ID.Text = "<input type=\"hidden\" name=\"INVOICE_ID\"
value=\"" + INVOICEID + "\" />";
MEMBERPURSEID.Text = "<input type=\"hidden\"
name=\"MEMBERPURSEID\" value=\"" + "0" + "\" />";
SHA1HASH.Text = "<input type=\"hidden\" name=\"SHA1HASH\"
value=\"" + hash + "\" />";
AUTO_SETTLE_FLAG.Text = "<input type=\"hidden\"
name=\"AUTO_SETTLE_FLAG\" value=\"" + AUTOSETTLEFLAG + "\" />";
DELAYEDSETTLEMENTON.Text = "<input type=\"hidden\"
name=\"DELAYEDSETTLEMENTON\" value=\"" + DELAYEDSETTLEFLAG + "\" />";
CREDITCARD_SHG_AMT.Text = "<input type=\"hidden\"
name=\"CREDITCARD_SHG_AMT\" value=\"" + CREDITCARD_SHG + "\" />";
DEBITCARD_SHG_AMT.Text = "<input type=\"hidden\"
name=\"DEBITCARD_SHG_AMT\" value=\"" + DEBITCARD_SHG + "\" />";
MERCHANT_RESPONSE_URL.Text = "<input type=\"hidden\"
name=\"MERCHANT_RESPONSE_URL\" value=\"" + _realexResponseURL + "\" />";
Failing code in realex response:
if (Request.Form["INVOICE_ID"] != null)
INVOICEID = Request.Form["INVOICE_ID"].ToString();
else
StaticOps.AddErrorToLog("FORM.INVOICE_ID : ", "VALUE NULL",
"RealexResponse");
Apologies for the delay in getting back to you, didn't see the notification of your response. On the POST in, the HPP won't accept the inverted commas and truncates at that point in the string.
Could you try url encoding the string on the way out and decoding when the response comes back? The max length of a custom field on the HPP is 255. Or you could just url encode the inverted commas. Hopefully that suits your needs. Please let me know if not.
Best,
Seán MacDomhnall
Realex Payments

Sending my parameters to the other side

Sorry if there are typos or inc clay error when I'm incredibly bad at English.
It is such that I need to send some value over the epay to receive my payment on my website.
onto hh.aspx looks like this:
<asp:Literal ID="LiteralDKK" runat="server"></asp:Literal>
<asp:Literal ID="LiteralPris" runat="server"></asp:Literal>
<asp:Literal ID="Literalwindowstate" runat="server"></asp:Literal>
<asp:Literal ID="LiteralOrderid" runat="server"></asp:Literal>
<asp:Literal ID="Literalmerchantnumber" runat="server"></asp:Literal>
on hh.aspx.cs
string dkk = "DKK";
string windowstate = "3";
string merchantnumber = "1111111";
string prisen = "10";
string ordernr = "159sg";
LiteralDKK.Text = "<input id=\"HiddenPris\" type=\"hidden\" name=\"currency\" value=\"" + dkk + "\" />";
Literalmerchantnumber.Text = "<input id=\"Hiddenmerchantnumber\" type=\"hidden\" name=\"merchantnumber\" value=\"" + merchantnumber + "\" />";
Literalwindowstate.Text = "<input id=\"Hiddenwindowstate\" type=\"hidden\" name=\"windowstate\" value=\"" + windowstate + "\" />";
LiteralPris.Text = "<input id=\"HiddenPris\" type=\"hidden\" name=\"amount\" value=\"" + prisen + "\" />";
LiteralOrderid.Text = "<input id=\"HiddenOrderid\" type=\"hidden\" name=\"orderid\" value=\"" + ordernr + "\" />";
//It must send my value onto this here page
Response.Redirect("https://ssl.ditonlinebetalingssystem.dk/integration/ewindow/Default.aspx");
My parameters which I would like to have sent to you can see how to appear here
If I had to do it in html, it looked like this,
<form action="https://ssl.ditonlinebetalingssystem.dk/integration/ewindow/Default.aspx" method="post">
<input name="merchantnumber" value="ENTER YOUR MERCHANT NUMBER HERE">
<input name="amount" value="10495"> <input name="currency" value="DKK">
<input name="windowstate" value="3"> <input type="submit" value=
"Go to payment">
</form>
The reason I do not do it is to secure me the right price and ordernr and many other things which must cross in.
The documentation here seems to provide simple ways to do this:
http://tech.epay.dk/da/betalingsvindue-integration
In English:
http://tech.epay.dk/en/payment-window-integration

Href Link Code Behind

I am trying to add a a link as part of the Product.Text but I've horribly got it wrong here, doing this on the C-Sharp code behind.
tcProduct.Text = "<div class=\"productname\">" + "<a class=\"productnameLink\" href=\"item.Product.Url\">" + item.Product.Name + "</a>" + "</div>";
The backward slashes are beginning to confuse me. Instead of a normal hyperlink i guess i should be using the .NET hyperlink instead??
I've tried this but doesn't add the hyperlink;
HyperLink productNameLink = new HyperLink();
productNameLink.Text = "<div class=\"productname\">" + item.Product.Name + "</div>";
productNameLink.NavigateUrl = item.Product.Url.ToString();
productNameLink.CssClass = "prodNameLink";
tcProduct.Controls.Add(productNameLink);
Plain string formatting should do the trick:
tcProduct.Text = string.Format("<div class=\"productname\"><a class=\"productnameLink\" href=\"{0}\">{1}</a></div>",
item.Product.Url, item.Product.Name);
However, you really better use a Repeater which is meant exactly for such things. Markup:
<asp:Repeater ID="rptProducts" runat="server">
<HeaderTemplate><h1>Products</h1></HeaderTemplate>
<ItemTemplate>
<div class="productname"><a class="productnameLink" href="<%# Eval("Url") %>"><%# Eval("Name") %></a></div>
</ItemTemplate>
</asp:Repeater>
And in the code behind:
rptProducts.DataSource = arrProducts;
rptProducts.DataBind();
Where arrProducts is the collection of your products.
What i used to do that is ASP Literal.
<asp:Literal ID="Literal2" runat="server"></asp:Literal>
Then in the code set to it with
Literal2.Text = "<a class='productnameLink' href=" + item.Product.Url + ">" + item.Product.Name + "</a>"
You can use Panel control which renders as div.
var link = new HyperLink
{
Text = item.Product.Name,
NavigateUrl = item.Product.Url.ToString(),
CssClass = "prodNameLink"
};
var panel = new Panel
{
CssClass = "productname"
};
panel.Controls.Add(link);
tcProduct.Controls.Add(panel);
Output:
<div class="productname">
<a class="prodNameLink" href="http://www.google.com">Google</a>
</div>
The Text property is supposed to be text only.
Many ways to do what you need to, but if you want to stick to using html string you could use:
tcProduct.Controls.Add(new HtmlGenericControl
{
InnerHtml = string.Format(#"<div class=""productname""><a class=""productnameLink"" href=""{0}"">{1}</a></div>", item.Product.Url, item.Product.Name )
});
Bonus, you can get rid of the '\' escape char with averbatim string literal: #"" (you need to double the quotes).

JQuery Dialog on table row

I am using c# and razor to produce a list of invoices. Each invoice is a table row and has a huge list of notes against it. To avoid a massive amount of space between rows, I want to hide the notes and allow a pop to view it. It is currently:
<td>
#foreach (var invoiceLine in invoice.InvoiceLines)
{
<p>
<strong>#invoiceLine.Date.ToShortDateString() #invoiceLine.Username</strong> <br />
#Html.Raw(invoiceLine.Notes.Replace(Environment.NewLine, "<br />"))
#Html.Raw((invoiceLine.DueDate.HasValue ? "<br /><strong>Follow up:</strong> " + invoiceLine.DueDate.Value.ToShortDateString() : ""))
#Html.Raw(invoiceLine.Completed ? "<br /><strong>Completed</strong>" : "")
</p>
}
So what I want to do is to add the popup using jquery:
$(function () {
$('#clickMe').click(function (event) {
var mytext = $('#myText').val();
$('<div id="dialog">' + mytext + '</div>').appendTo('body');
event.preventDefault();
$("#dialog").dialog({
width: 600,
modal: true,
close: function (event, ui) {
$("#dialog").hide();
}
});
}); //close click
});
Then modify my code:
<td>
<h3 id="clickMe">Open Notes</h3>
<textarea cols="1" rows="75" id="myText" style="display:none">
#foreach (var invoiceLine in invoice.InvoiceLines)
{
<p>
<strong>#invoiceLine.Date.ToShortDateString() #invoiceLine.Username</strong> <br />
#Html.Raw(invoiceLine.Notes.Replace(Environment.NewLine, "<br />"))
#Html.Raw((invoiceLine.DueDate.HasValue ? "<br /><strong>Follow up:</strong> " + invoiceLine.DueDate.Value.ToShortDateString() : ""))
#Html.Raw(invoiceLine.Completed ? "<br /><strong>Completed</strong>" : "")
</p>
}
</textarea>
</td>
First problem is, that only the first row appears. I presume because my id is the same all the way down?
How do I make the dialog open for each row?
I am a newb at c# and js btw :)
First: The textarea makes no sense at all.
Then change the parts like this. See it working in the jsfiddle.
HTML
<td>
#foreach (var invoiceLine in invoice.InvoiceLines)
{
<p>
<strong>#invoiceLine.Date.ToShortDateString() #invoiceLine.Username</strong> <br />
#Html.Raw((invoiceLine.DueDate.HasValue ? "<br /><strong>Follow up:</strong> " + invoiceLine.DueDate.Value.ToShortDateString() : ""))
#Html.Raw(invoiceLine.Completed ? "<br /><strong>Completed</strong>" : "")
<h3 class="notesClick">Open Notes</h3>
<div class="notesHtml" style="display:none">
#Html.Raw(invoiceLine.Notes.Replace(Environment.NewLine, "<br />"))
</div>
</p>
}
</td>
JS
$(function() {
$('.notesClick').click(function(event) {
var notes = $(event.currentTarget).next('.notesHtml');
$("<div>").html(notes.html()).dialog();
});
});​

Categories