Need help formatting html from my code-behind - c#

I have the following code snippet, but I'm banging my head up against the wall trying to get the errors out of it.
I'm getting the following design time compile errors:
; expected
The name button does not exist in the current context.
Those same two messages also repeat for the DisplayReceipt.
Here is my code snippet being assigned in my code behind for html.
Can somebody please help me out?
Image_ID = "<input id='" + fuelticket.Image_ID + "' type="button" onclick='" + DisplayReceipt(fuelticket.Image_ID)"'>";

You just need to escape the quotes:
Image_ID = "<input id='" + fuelticket.Image_ID + "' type=\"button\" onclick='DisplayReceipt(" + fuelticket.Image_ID + ")'>";
Or use string.Format() to make things a bit cleaner:
Image_ID = string.Format("<input id='{0}' type=\"button\" onclick='DisplayReceipt({0})'>", fuelticket.Image_ID);

To make it work use the below code:
Image_ID = String.Format("<input id=\"{0}\" type=\"button\" onclick=\"{1}\">", fuelticket.Image_ID, DisplayReceipt(fuelticket.Image_ID));
The above looks more clear and optionally you can also use # for the string so you don't have to escape any special characters.
Image_ID = String.Format(#"<input id="{0}" type="button" onclick="DisplayReceipt({0})">", fuelticket.Image_ID));

Related

QueryString is taking first substring and discarding rest post space

I have a query string which passes 6 parameters in C# as shown below
string url = "Report.aspx?Desc=" + Desc.SelectedValue + "&PON=" + PNumber.Text + "&InsNme=" + ins.ToUpper().ToString() + "&BackTy=" + cb.SelectedValue + "&StartDate=" + txtDate.Text + "&EndDate=" + txtTodate.Text + "&Name=" + nme;
string s = "window.open('" + url + "', 'popup_window', 'width=1500,height=800,left=200,top=150,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
Now, in the above string InsNme contains a value of John Patrice Joanne. Instead of complete value of InsNme Report.aspx contains just John. How to handle this?
The spaces in the name are breaking the URL.
If you want to do it yourself, replace spaces with %20. Otherwise a simple, but not anywhere near "good" technique is:
url = "Report.aspx?";
// for each name value pair ...
url += dataLabel + "=" + System.Web.HttpUtility.UrlEncode( dataChunk ) +"&";
The utility is preferred as it will take care of other, similar issues such as literal '&' in a name.
Check this answer for better solutions.
How to build a query string for a URL in C#?

Quotes generating html entity

I'm trying to replace a bunch of consecutive
var: $("#var").val()
lines in my JS script with a simple loop in c# like this:
#foreach(var q in myList){
#(q.var + ": $('#" + q.var + "').val()," + Environment.NewLine);
}
But any symbol I try to pass (', \" or "") generates the html entity (&-#39; or &-quot;).
var: $("#var").val()
and JS errors.
With a view only solution, is it possible to fix this?
To have an official answer in this post (or for futur readers) I will put my comment as an answer, which seems to have resolved the issue.
What you should use is Html.Raw to print raw content.
#(q.var + Html.Raw(": $(\"#") + q.var + Html.Raw("\").val(),") + Environment.NewLine);

Remove specific string from given URL

I have a URL example image1-resize.jpg, and I want to delete -resize and save image1.jpg in new variable.
How can I do that?
This is what I tried to do:
str1 += "<li><a href='#pic" + counter + "'><img src='admin/temp/hotelimg/" + temp_url.ToString() + "'/></a></li>";
string stt =temp_url.replace("-resize","");
str2 += "<div id='pic" + counter + "'><img src='admin/temp/hotelimg/" + stt.ToString() + "' width='550' height='370'/></div>";
This should do your job:
temp_url.replace("-resize","");
Note: You should always search before putting questions here, since sometimes its really easy and need just small research on it.

In html / asp.net-mvc, what is the correct way to include an apostrophe inside an image tooltip

If i have an image tooltip that is being populated from a database table. I am generating this html below from my server side C# code
public string GetImage()
{
return "<img class='iconSpace' title ='" + dataIssue + "' src='/Content/Images/Icons" + size + "/information_red.png' />";
}
the issue is that if the variable dataIssue has an apostrophe in it, it only shows the characters in the string up to that point.
What is the best way to show the whole string in the tooltip given the code above?
' is not special symbol for HTML, and browser shows whole string without problems, but you can have problems with following symbols " < > & they should be escaped as:
"
<
>
&
if your browser treats HTML standard incorrectly and cut the rest of the string, you can try to escape single quote with ' - this will work for all browsers
so, according HTML standard attribute values should be surrounded by " symbol, not by ', so the problem here should be solved:
dataIssue = any_kind_of_html_escape_function_here(dataIssue);
return "<img class=\"iconSpace\" title=\"" + dataIssue + "\" src=\"/Content/Images/Icons" + size + "/information_red.png\" />";
For asp.net htmlencode function is defined here: http://msdn.microsoft.com/en-us/library/w3te6wfz.aspx
Would this work for you?
string img = "<img class=\"iconSpac\" title=\"" + dataIssue + "\" " + "scr=\"/Content/Images/Icons\"" + size + "/information_red.png\" />";
You should use HttpUtility.HtmlEncode("...") for it.
http://msdn.microsoft.com/en-us/library/73z22y6h.aspx

How do I stop this json from escaping html?

I have an ajax control that returns user comments. Its served by a c# ajax handler page and the c# matches a timespan that a user can leave in the comments:
commmentToDisplay = Regex.Replace(c.CommentText, timeSpanRegex, "<a href=\'\' onclick=\'alert(\'Flash Required\');\'>" + actualTimeSpan + "</a>");
This produces the following json:
({
"numOfPages":"1",
"pageIndex":"1",
"comments": [
{
"user":"hmladmin",
"created":"29/03/2011 16:41:20",
"id":"1",
"comment":"<a href='' onclick='alert('Flash Required');'>00:00:21</a>",
"editable":"true",
"reportable":"true"
}
]
})
Confusingly when I look at the html in firebug it comes out as:
<a );="" required="" flash="" onclick="alert(" href="">00:00:21</a>
Ive tried:
commmentToDisplay = Regex.Replace(c.CommentText, timeSpanRegex, "<a href=\'\' onclick=\'alert(\"Flash Required\");\'>" + actualTimeSpan + "</a>");
and
commmentToDisplay = Regex.Replace(c.CommentText, timeSpanRegex, "" + actualTimeSpan + "");
And multiple permutations of I just cannot work out how to get the json and c# to return an anchor tag with an alert message in the onclick event.
Can someone help me to work out how I escape this properly so this problem doesnt happen.
The problem is when you create the string of HTML and has nothing to do with JSON:
"<a href=\'\' onclick=\'alert(\'Flash Required\');\'>" + actualTimeSpan + "</a>"
should probably be:
'' + actualTimeSpan + ''
You've got nested single quotes in 'alert('Flash Required');' which won't work. You need to change one set to double-quotes then escape them (\") for JSON. e.g. 'alert(\"Flash Required\");'

Categories

Resources