QueryString is taking first substring and discarding rest post space - c#

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#?

Related

How to give url properly to anchor tag in asp.net using jquery?

I have written jquery in .net application as
Function UploadComplete(sender, args) {
var filename = args.get_fileName();
var contentType = args.get_contentType();
var folder = "~/Uploads/";
var text = "Size of " + filename + " is " + args.get_length() + " bytes";
if (contentType.length > 0) {
text += "and content type is '" + contentType + "'.";
text += "<a href='" + folder + filename + "'" + filename + "</a>";
}
document.getElementById('lblStatus').innerText = text;
}
Now my issue is I am not able to give path accurately in the line
text += "<a href='" + folder + filename + "'" + filename + "</a>";
Please help me!!!
A few issues to sort out:
1) "~/"
"~/" as a relative path is for use server-side in .Net. Javascript (really your browser) does not know what to do with that URL.
As a little trick, you can inject an application root URL using this Razor code #(Url.Content("~/")) but that means your function needs to be in a razor page and not a separate JS file.
If your JS is "elsewhere", inject the path as a Javascript variable (e.g. window.rootUrl = "#(Url.Content("~/"))") using a small script section on the page.
2) Missing '>'
You are missing a closing > in your generated anchor.
3) innerHTML, not innerText
You need to set the innerHTML property, or you will get raw text. You will probably need to tweak the formatting of the output (line breaks or spans/paragraphs) to make it look pretty (e.g. you have no space before the anchor at the moment).
Put it all together and you get something like:
function UploadComplete(sender, args) {
var filename = args.get_fileName();
var contentType = args.get_contentType();
var folder = '#(Url.Content("~/"))Uploads/';
var text = 'Size of ' + filename + ' is ' + args.get_length() + ' bytes';
if (contentType.length > 0) {
text += 'and content type is "' + contentType + '". ';
text += '' + filename + '';
}
document.getElementById('lblStatus').innerHTML = text;
}
As I said in comment, I strongly recommend using the single quote as a string delimiter in jQuery/JavaScript so that any HTML strings have double-quotes on attributes.
4) jQuery?
You tagged the question with jQuery too, which would shorten the last line to:
$('#lblStatus').html(text);
Apologies for any typos, I typed all this off the top of my head and did not verify it.

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

Trying to separate whole number and decimal asp.net c#

I'm looking for guidance can someone help me out?
I need to separate the value in duration into a whole number and the decimal.
EDIT: I have a textbox in one page. If someone enters 1.5 and it gets stored in duration, I would like to retrieve that in another page and I would like to store the whole number in a textbox and the decimal number will select a value from a dropdownlist based on the response.
Sorry I added duration twice by accident.
context.Response.Write(dr["Title"].ToString()
+ '|' + dr["CourseId"].ToString() + '|' + dr["duration"].ToString()
+ '|' + dr["Code"].ToString() + '|'
+ dr["Category"].ToString() + School.NewLine);
If the value you are trying to split is a number, try this:
decimal number = 12.34;
int wholePart = decimal.Truncate(number);
decimal fractionPart = number - wholePart;
If is is a string, CLandry's answer should work, duration[0] would be the whole part and duration[1] would be the fraction part.
var duration = dr["duration"].ToString().Split(CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator);
int durationWhole = Int32.Parse(duration[0]);
int durationDecimal = Int32.Parse(duration[1]);
Duration will be a string array. duration[0] is the part of the number before the decimal, and duration[1] is the part after.
The split is being done with the decimal separator of the culture using it, so it should work globally as well.
Based on the edit in your question, I've updated the answer to give you numerical results as well. Now you can use the numbers or strings as needed.
context.Response.Write(dr["Title"].ToString()
+ '|' + dr["CourseId"].ToString() + '|' + dr["duration"].ToString().Split('.')[0]
+ '|' + dr["duration"].ToString().Split('.')[1] + '|' + dr["Code"].ToString() + '|'
+ dr["Category"].ToString() + School.NewLine);
You could use Substrings and IndexOf:
{ context.Response.Write(
dr["Title"].ToString() + '|' +
dr["CourseId"].ToString() + '|' +
dr["duration"].ToString().Substring(0, dr["duration"].ToString().IndexOf(".")) + '|' +
dr["duration"].ToString().Substring(dr["duration"].ToString().IndexOf("."), dr["duration"].ToString().Length()) + '|' +
dr["Code"].ToString() + '|' +
dr["Category"].ToString() +
School.NewLine);
}

String woes in C# - having a . next to a "

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

Categories

Resources