I have the following string:
string text = #"" + reportName + "<br/><br/>";
When this string gets written to my page, it looks like this:
DR - 3: Debt Payment History
Can someone help explain why the period id being escaped? I'm fumbling with how to escape this properly.
Thanks.
I'm not sure exactly what your output should look like but
String.Format and escaping are your friend.
string text = String.Format("<a href='http://raustdsx0700.real.local:7782/analytics/saw.dll?PortalPages&PortalPath={0}&Page={1}&P0=1&P1=eq&P2=Project.\"Project Name\"'' target='_blank'>{1}</a><br/><br/>",dashboardURL,reportName);
If you are intent on using quotes in your URL, then use single-quotes for your tag attributes.
string text = "<a href='http://raustdsx0700.real.local:7782/analytics/saw.dll?PortalPages&PortalPath=" + dashboardURL + "&Page=" + reportName + "&P0=1&P1=eq&P2=Project.\"Project Name\"' target='_blank'>" + reportName + "</a><br/><br/>";
Or better, url-escape the quote (%22):
string text = "<a href='http://raustdsx0700.real.local:7782/analytics/saw.dll?PortalPages&PortalPath=" + dashboardURL + "&Page=" + reportName + "&P0=1&P1=eq&P2=Project.%22Project Name%22' target='_blank'>" + reportName + "</a><br/><br/>";
You need to urlencode your string, check this out: http://msdn.microsoft.com/en-us/library/zttxte6w.aspx
Related
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#?
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.
I have made a code like this:
TextWriter tw = File.CreateText(#"D:\output.txt");
tw.Write(#"{""lon"":" + grid.point[0].ToString("###.####") + ",");
tw.Write(#"""latt"":" + grid.point[1].ToString("###.####") + ",");
char c = '"';
////improve the last line pls
tw.Write(#"""color"":" + c.ToString() + "#" + grid.Color.ToString("X").Substring(2) + c.ToString() + "},\n");
With the above code, I had successfully made a JSON format look like below:
{"lon":121,"latt":40.5025,"color":"#3EC1FF"},
Now My question is:
How to improve the string format without using the c.toString()?
You can use the format overload of the Write method e.g. Write(String, Object[]).
Something like this:
tw.Write("\"color\":\"#{0}\"}},\n", grid.Color.ToString("X").Substring(2));
StringBuilder jsonBuilder=new StringBuilder();
jsonBuilder.AppendFormat("{0}", "{");
jsonBuilder.AppendFormat("\"Lat\":{0:0.000},", 584.25689);
jsonBuilder.AppendFormat("\"Lon\":{0:0.000},", 784.25689);
jsonBuilder.AppendFormat("Color:{0}","\"#3EC1FF\"");
jsonBuilder.AppendFormat("{0}", "}");
string json = jsonBuilder.ToString();
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
I am trying to save a number of images and I'd like to use the DateTime to have distinct and identifiable Filenames.
So I create a String with the correct Path, add the datetime to it and remove the spaces, dots and colons.
String imagePath = "D:\\Patienten\\" + username;
imagePath += "\\"+DateTime.Now.ToString();
Console.WriteLine("WithFilename: " + imagePath);
imagePath.Replace(" ", "");
Console.WriteLine("Without \" \" : " + imagePath);
imagePath.Replace(".", "");
Console.WriteLine("Without \".\": " + imagePath);
imagePath.Replace(":", "");
Console.WriteLine("Output format: " + imagePath);
imagePath += ".png";
image.Save(imagePath);
According to the console output the String doesnt change at all.
Meaning all the Output Strings from Console.Writeline are identical.
I am using c# in visual Studio Express 2010 in case that makes a difference.
Can anyone find an Error here?
Thanks in advance!
Strings are immutable, the modified string will be a new string that is returned from the function
e.g.
imagePath = imagePath.Replace(" ", "");
Why strings are immutable
Why not just use DateTime.ToString() with a format and drop the dividers using that? Would be more efficient than performing several String.Replace() yourself:
string imagePath = "D:\\Patienten\\" + username + "\\" + DateTime.Now.ToString("yyyyMMdd hhmmssfff") + ".png";
You should use:
imagePath = imagePath.Replace(" ", ""); You should assign returned value
From the documentation (emphasis mine):
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It is supposed to work like that. Use
imagePath = imagePath.Replace(" ", "");
instead.