Converting HTML special characters in ASP.NET - asp.net

I am binding to a DropDownList as follows;
Sub bindGalleries(ByVal catID As Integer)
ddlGalleries.DataSource = Galleries.GetGalleries(catID)
ddlGalleries.DataTextField = "GalleryName"
ddlGalleries.DataValueField = "GalleryID"
ddlGalleries.DataBind()
End Sub
One of the items in the list is: 'Kültür & Sanat', which is displayed just right in the dropdownlist. But when I look at the source of the page, it is: Kültür & Sanat
How can I get the source to be exactly the same as the original string?
Note: my meta tag is: in master page..

This should give you what you need:
Server.HtmlDecode("Kültür & Sanat");
Write a method to "sanitize" the items in the DDL and store them in an array. Then just bind to the array.
You can populate the DDL in a similar fashion.

You may be able to use backslashes to escape

Try to put the proper charset in the heading portion of your page.
Example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-16"/>

The reason why you see those characters is because those characters are being htmlencoded upon saving into your source.
i.e.
YourSource = server.htmlEncode(value)
or
YourSource = server.urlEncode
You can save it to its original form by using server.htmlDecode or by omitting the server.htmlEncode.

Related

How can I write raw XML to a Label in ASP.NET

I am getting a block of XML back from a web service. The client wants to see this raw XML in a label on the page. When I try this:
lblXmlReturned.Text = returnedXml;
only the text gets displayed, without any of the XML tags. I need to include everything that gets returned from the web service.
This is a trimmed down sample of the XML being returned:
<Result Matches="1">
<VehicleData>
<Make>Volkswagen</Make>
<UK_History>false</UK_History>
</VehicleData>
<ABI>
<ABI_Code></ABI_Code>
<Advisory_Insurance_Group></Advisory_Insurance_Group>
</ABI>
<Risk_Indicators>
<Change_In_Colour>false</Change_In_Colour>
</Risk_Indicators>
<Valuation>
<Value xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></Value>
</Valuation>
<NCAP>
<Pre_2009></Pre_2009>
</NCAP>
</Result>
What can I do to make this appear on the screen? I noticed that Stack Overflow does a pretty good job of putting the XML on the scren. I checked the source and it's using <pre> tags. Is this something that I have have to use?
It would be easier to use a <asp:Literal /> with it's Mode set to Encode than to deal with manually encoding Label's Text
<asp:Literal runat="server" ID="Literal1" Mode="Encode" />
You need to HtmlEncode the XML first (which escapes special characters like < and > ):
string encodedXml = HttpUtility.HtmlEncode(xml);
Label1.Text = encodedXml;
Surrounding it in PRE tags would help preserve formatting, so you could do:
string encodedXml = String.Format("<pre>{0}</pre>", HttpUtility.HtmlEncode(xml));
Label1.Text = encodedXml;
As Bala R mentions, you could just use a Literal control with Mode="Encode" as this automatically HtmlEncodes any string. However, this would also encode any PRE tags you added into the string, which you wouldn't want. You could also use white-space:pre in CSS which should do the same thing as the PRE tag, I think.

ASP.NET - Current name of page from web user control?

I need to find out name of location where web user control is. Somethnig like HttpContext.Current.Request.Url.ToString(), but I get only page for this web user control.
Request.Url.Segments will give you a string array. The last item is the page
You should try the Request.Url.LocalPath property
string fileNameFromLocalPath = Path.GetFileName(Request.Url.LocalPath);
This code helps:
string filename = Path.GetFileName(Request.Url.AbsolutePath);
If you ask for Page.getType.name, you will get the name of the master, the aspx page.
if you want the name of the ascx control you are working on, use
me.GetType.Name.ToString
if your control is in a directory MyDir and the name of your ascx is test.ascx then the result will be
"ASP.MyDir_test_ascx"
You can also use (VB.Net):
Dim pageName as String = Page.GetType().Name
which replaces the .extension with an underscore
So from Default.aspx you would be returned Default_aspx
You can also use:
Dim pageName as String = CType(HttpContext.Current.CurrentHandler, Page).GetType().Name
Which will produce the same results as described above.

how to get xml html after transpose and databind()

I have some code that uses xsl and xml.
The Xml control is on the design page.
The xml control id is xmlApplication
The xmlstring is generated and xsl has the format with all the tables and cells etc.
Here is a part of thecode of a page which generates the final product which shows the xml in a certain format.
xmlApplication.Document = xmlDoc;
xmlApplication.Transform = transApp;
xmlApplication.DataBind();
I am guessing after xmlApplication.Databind(), xmlApplication will be converted into something that can be put inside .
Is it possible to grab as a string?
Please let me know if I have a wrong idea abut this.
Thanks a lot.
http://www.logiclabz.com/c/net-c-function-to-convert-xml-document-into-html-string-using-xslt.aspx

Converting French Characters to ASCII on form submit

Hi there I was wondering if anyone can help with this. I am submitting values to an access database using ASP classic and need to convert French characters to ASCII. I have done it before with an form to email script. here is the code that I used with the first line being the code that writes a value to the database field. any help would be great.
[code]
'--------------------------------------------------------------------------
' Checks form fields and headings for French Characters and replaces them
' with the ASCII equivalent.
'--------------------------------------------------------------------------
rsAddComments.Fields("Customer") = Request.Form("Customer")
body = Replace(body,"À",chr(192))
body = Replace(body,"Á",chr(193))
body = Replace(body,"Â",chr(194))
body = Replace(body,"Î",chr(206))
[/code]
Try to just put the ISO Latin 1 character set on the page:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
This work for me.
Actually that will not work. It is the way that access reads to text, same as outlook. I had to convert to ascii for outlook. What I did for that was
body = Replace(body,"é",chr(999))
Now I need to replace all of the values from the form fields with the ascii using ASP before it writes to the database. What I don't know is what to put in place of BODY in the above code.

html injection question

Using FreeTextBox, I'm capturing HTML-formatted text. The purpose is to allow a website owner to update their web page content on a few pages. I have the system completed except for knowing what to do with the resultant HTML markup.
After the page editor completes their work, I can get the output from FreeTextBox, in html format, like so: <font color="#000080"><b>This is some text.</b></font>
I tried storing it as escaped markup in web.config, but that didn't work since it kept hosing the tags even after I changed them to escaped characters, like so: <font color="#000080">
The reason I wanted to store this kind of string as a key in web.config is that I could successfully store a static string, set a lebel's value to it, and successfully render the text. But when I try to escape it, it gets reformatted in web.config by .Net somehow.
So I escaped all the characters, encoded them as Base64 and stored that. Then on page_load, I tried to decode it, but it just shows up as text, with all the html tags showing as well - it doesn't get rendered. I know a million people use this control, but I'm damned if I can figure out how to do it right.
So here's my question: how can I inject the saved HTML into an edited page so it shows up in browsers like the editor wants it to look?
Try Server.HtmlDecode to output the HTML to the screen.
As a side note, I prefer to use CKEditor for html-formatted input. I found it is the better option among all options (FreeTextBox, TinyMCE, anything else?) and it has got completely rewritten and faster in the version 3.0!
In case anyone comes here for the answer, here's one way to do it.
I had initial problems with web.config changing some of the HTML tags upon storage, so we use B64 encoding (may not be necessary). Store the saved html markup to an AppSettings key in web.config as Base64 encoding, using this for your setting update function. Add error checking and whatever else you need it to do:
'create configuration object
Dim cfg As Configuration
cfg = WebConfigurationManager.OpenWebConfiguration("~")
'get reference to appsettings("HTMLstring")
Dim HTMLString As KeyValueConfigurationElement = _
CType(cfg.AppSettings.Settings("HTMLstring"), KeyValueConfigurationElement)
'get text entered by user and marked up with HTML tags from FTB1, then
'encode as Base64 so we can store it as XML-safe string in web.config
Dim b64String As String = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(FTB1.Text))
'save new value into web.config
If Not HTMLString Is Nothing Then
HTMLString.Value = b64String
cfg.Save()
End If
Next, add a Literal control to the aspx markup:
<asp:Literal id="charHTML" runat="server"/>
To add the saved HTML to the post-edited page, do the following in Page_Load:
'this string of HTML code is stored in web.config as Base64 to preserve XML-unsafe characters that come from FreeTextBox.
Dim injectedHTML As String = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(AppSettings("HTMLstring")))
'the literal control will directly inject this HTML instead of encoding it
charHTML.Mode = LiteralMode.PassThrough
'set the value
charHTML.Text = injectedHTML
Hope this helps. sF

Resources