Html ahref tag in stringbuilder - asp.net

I am using html
strBody.Append("<span style=\"font-family:Arial;font-size:10pt\"> Hi " + Name + ",<br/><br/> Welcome! <br/><br/>");
strBody.Append("<tr><td style=\"font-weight:bold\">");
strBody.Append("documents for reference are shared in the Account Induction Portal ");
strBody.Append("</td><td>");
strBody.Append("Visit W3Schools<br/><br/>");
strBody.Append("</td><td>");
strBody.Append("</td></tr>");
strBody.Append("<tbody/></table><br/>");
Here href got error i cant include that in string bulider append without error.Pls help on this

you have two sets of parenthesis you must small quote for the url!
strBody.Append("<a href='http://www.w3schools.com'>Visit W3Schools</a><br/><br/>");
or escape like
strBody.Append("Visit W3Schools<br/><br/>"

You don't escape the quotes (") in the following line:
// Replace this
strBody.Append("Visit W3Schools<br/><br/>");
// with either this:
strBody.Append("Visit W3Schools<br/><br/>");
// or use single quotes inside the string:
strBody.Append("<a href='http://www.w3schools.com'>Visit W3Schools</a><br/><br/>");

Related

Use Replace function to remove "{ characters from string

I would like to remove "{ and replace it with {. The following is the line of code that I'm currently using.
var MyString = DataString.Replace(#""{", "");
The following is the error message I'm getting
Please advise
Thanks
You need to escape the quote that you want to replace using two quotes, so for your example:
var MyString = DataString.Replace(#"""{", "{");
Also see How to include quotes in a string for alternatives to use quotes in strings.
If you are expecting JSON data then what you really need is a JSON Parser for that. And if you just want to replace "{ to { then you simply need to escape and replace the string like below:
// Suppose the variable named str has a value of Hello"{ wrapped in double quotes
var strReplaced = str.Replace("\"{", "{");
Console.WriteLine($"strReplaced: {strReplaced}");
// This will result in strReplaced: Hello{

asp.net querystring

I have the following page querystring:
register.aspx?id="jSmith"
I have the following code to retrieve the value of ID
string qString = string.IsNullOrEmpty(Request.QueryString["id"]) ? string.Empty : HttpUtility.UrlDecode(Request.QueryString["id"]);
When I view the value of qString I get something like
"\"jSmith\""
so when I do the following:
if (qString == "jSmith")
{
........
}
it does not not execute the if condition. What do I need to do s that it does not have the quotes.
The code is correct.
The problem is that you are passing to the page "jSmith" with the double quotes as part of the string.
Try invoke the page this way
register.aspx?id=jSmith
That is because the correct way to give the path in this case would be register.aspx?id=jSmith, without the quotes. If you need spaces, or other special characters, in your ID, these should be URL encoded (and will be decoded by your code), but not enclosed in quotes.
For example, if your id was the string john smith, the URL would become register.aspx?id=john+smith, since + is the URL encoding of a space.
You don't need to put quotes around values in querystring, by definition they're all strings...
Your querystring should look like :
register.aspx?id=jSmith
You do not need the quotation marks in your querystring.
It should read
register.aspx?id=jSmith
You should look for
if (qString == "\"jSmith\"")
the \ is escaping the extra "
or you could perform a replace to remove the extra "
use
Response.Redirect("Qstring.aspx?name= smith");
and on the page Qstring.aspx load event
string s=Request.QueryString["name"].ToString();
gives u "smith" in s variable

how to pass parameter inside Strigbuilder class object?

I am using
string strurl = "Reports/ReportFilter.aspx";
and bind a tag as
AnchorLeftMenuLinks.Append(" href='javascript:OpenDialogue(" + strurl + ");' ");
but it return error as "undefined object AuditReports" as runtime it become like
href="javascript:OpenDialogue(Reports/ReportFilter.aspx);"
but when i add single quotes manually in firebug like
href="javascript:OpenDialogue('Reports/ReportFilter.aspx');"
it works fine.
can anyone suggest me that how to add single quotes in code.Yhankx in advance.
Try this
AnchorLeftMenuLinks.Append(" href='javascript:OpenDialogue(\"" + strurl + "\");' ");
Try:
var javascript = string.Format("href='javascript:OpenDialouge('{0}');'", strurl);
AnchorLeftMenuLinks.Append(javascript);
or:
AnchorLeftMenuLinks.AppendFormat("href='javascript:OpenDialouge('{0}');'", strurl);
Reason behind it was Javascript String because In JavaScript, a string is started and stopped with either single or double quotes. This means that the string was being chopped to: javascript:OpenDialogue( and your function's syntax was being incorrect and thus it was not working.
Thus it was mandatory to place a backslash (\)before each double quote in strurl. This turns each double quote into a string literal.
There are some other special characters also which needed to be placed using \
\' - single quote
\" - Double Quote
\\ - BackSlash
\n - new Line
\t - tab

How to encode the plus (+) symbol in a URL

The URL link below will open a new Google mail window. The problem I have is that Google replaces all the plus (+) signs in the email body with blank space. It looks like it only happens with the + sign. How can I remedy this? (I am working on a ASP.NET web page.)
https://mail.google.com/mail?view=cm&tf=0&to=someemail#somedomain.com&su=some subject&body=Hi there+Hello there
(In the body email, "Hi there+Hello there" will show up as "Hi there Hello there")
The + character has a special meaning in [the query segment of] a URL => it means whitespace: . If you want to use the literal + sign there, you need to URL encode it to %2b:
body=Hi+there%2bHello+there
Here's an example of how you could properly generate URLs in .NET:
var uriBuilder = new UriBuilder("https://mail.google.com/mail");
var values = HttpUtility.ParseQueryString(string.Empty);
values["view"] = "cm";
values["tf"] = "0";
values["to"] = "someemail#somedomain.com";
values["su"] = "some subject";
values["body"] = "Hi there+Hello there";
uriBuilder.Query = values.ToString();
Console.WriteLine(uriBuilder.ToString());
The result:
https://mail.google.com:443/mail?view=cm&tf=0&to=someemail%40somedomain.com&su=some+subject&body=Hi+there%2bHello+there
If you want a plus + symbol in the body you have to encode it as 2B.
For example:
Try this
In order to encode a + value using JavaScript, you can use the encodeURIComponent function.
Example:
var url = "+11";
var encoded_url = encodeURIComponent(url);
console.log(encoded_url)
It's safer to always percent-encode all characters except those defined as "unreserved" in RFC-3986.
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
So, percent-encode the plus character and other special characters.
The problem that you are having with pluses is because, according to RFC-1866 (HTML 2.0 specification), paragraph 8.2.1. subparagraph 1., "The form field names and values are escaped: space characters are replaced by `+', and then reserved characters are escaped"). This way of encoding form data is also given in later HTML specifications, look for relevant paragraphs about application/x-www-form-urlencoded.
Just to add this to the list:
Uri.EscapeUriString("Hi there+Hello there") // Hi%20there+Hello%20there
Uri.EscapeDataString("Hi there+Hello there") // Hi%20there%2BHello%20there
See https://stackoverflow.com/a/34189188/98491
Usually you want to use EscapeDataString which does it right.
Generally if you use .NET API's - new Uri("someproto:with+plus").LocalPath or AbsolutePath will keep plus character in URL. (Same "someproto:with+plus" string)
but Uri.EscapeDataString("with+plus") will escape plus character and will produce "with%2Bplus".
Just to be consistent I would recommend to always escape plus character to "%2B" and use it everywhere - then no need to guess who thinks and what about your plus character.
I'm not sure why from escaped character '+' decoding would produce space character ' ' - but apparently it's the issue with some of components.

Regular expression to convert substring to link

i need a Regular Expression to convert a a string to a link.i wrote something but it doesnt work in asp.net.i couldnt solve and i am new in Regular Expression.This function converts (bkz: string) to (bkz: show.aspx?td=string)
Dim pattern As String = "<bkz[a-z0-9$-$&-&.-.ö-öı-ış-şç-çğ-ğü-ü\s]+)>"
Dim regex As New Regex(pattern, RegexOptions.IgnoreCase)
str = regex.Replace(str, "<font color=""#CC0000"">$1</font>")
Generic remarks on your code: beside the lack of opening parentheses, you do redundant things: $-$ isn't incorrect but can be simplified into $ only. Same for accented chars.
Everybody will tell you that font tag is deprecated even in plain HTML: favor span with style attribute.
And from your question and the example in the reply, I think the expression could be something like:
\(bkz: ([a-z0-9$&.öışçğü\s]+)\)
the replace string would look like:
(bkz: <span style=""color: #C00"">$1</span>)
BUT the first $1 must be actually URL encoded.
Your regexp is in trouble because of a ')' without '('
Would:
<bkz:\s+((?:.(?!>))+?.)>
work better ?
The first group would capture what you are after.
Thanks Vonc,Now it doesnt raise error but also When i assign str to a Label.Text,i cant see the link too.Forexample after i bind str to my label,it should be viewed in view-source ;
<span id="Label1">(bkz: here)</span>
But now,it is in viewsource source;
<span id="Label1">(bkz: here)</span>

Resources