Special (or foreign countries) characters - c#

I am new to C# and was experimenting with the ASP.Net GridView (framework 3.5), and I found out a big problem when the gridView text contains the following:
ñ/Ñ/á/Á/é/É/í/Í/ó/Ó/ú/Ú or a â, ê, î, ô, û, ë, ï, ü or even a ç
The results end up displaying something like this: &+#+237
I made a mixture of pain by doing a table,dataset etc... to get the data from the database clean as the letter itself. I also tried making a method that cleaned the variables but it was a terrible IF.
I was wondering if there was a way to make the letters appear as themselves (ñ as ñ not as &+#+237) when extracting data from the gridview without getting the data directly from the database and without doing a lot of code. If there is not, is there a way to code efficiently clean them up?

Your characters are being HTML encoded, either somewhere in your data access layer, or within the gridview controls. Try running the text through HttpUtility.HtmlDecode() before binding the grid.
If that doesn't work, then it's probably being encoded at the control level. I see that you tagged this question with "textbox". The standard ASP.NET TextBox control will automatically encode anything that's assigned to its Text property. Instead, you can use a generic System.Web.UI.HtmlControls.HtmlTextArea control or new HtmlGenericControl("input").

Related

Replacing Microsoft Word Content Control Dynamically At Runtime

I want to be able to bind content control fields to each others' values. Basically if you change a field at the top, all others in the document also update to that. I'm replacing hundreds of individual variables, each with 100 duplicates. There is a better way than the 'Find and Replace Tool'.
Here is a sample document directly from Microsoft's site that shows exactly what I would like to be able to do:
https://omextemplates.content.office.net/support/templates/en-us/tf03444179.dotx
When the '' value is changed, all others in the document update.
I've already looked at plenty of solutions like: c# word interop find and replace everything
But they do not dynamically respond during run-time. In other words you have to go in and change which string you want to replace for each value.
Been looking for a while now, thanks in advance if anyone else can figure this out.

Retrieve text from database and format it on the front end of the website

I have some text currently stored in my database table as nvarchar.
I am currently retrieving the text using a stored procedure and binding it to a literal within a gridview on the front end.
What I would like to do is to retrieve the text and then format it , like inserting line spaces and making
certain area bold. Is it possible to do so ? Can anyone give me an idea of how it can be done ?
One idea thats striking me is to use XML while storing the text . But even if I do that how would I make a certain part of the text bold and include line spaces.
So currently, my text is stored in the database table column nvarchar(max) as:
This is the heading this is the content
What I would like to do is to display the above within a gridview like:
**This is a Heading** (heading in bold)
This is the content
The simplest method (one I have used several times) is to store the html in the table like this:
<h1>This is the heading</h1>This is the content
You will have to add special handling for working with html, but it works just fine.
You can also store the header string in one field, and body in another.
Short of that, you would have to have some indicator telling the front which part of the string should be bolded, etc. and that can get very complex
Short answer is that this is possible but takes some work.
You first need to decide in what format are you going to store the data and how can you specify format on the client side, before text is entered into database.
If you have WYSIWYG editor for text – html conversion you can try storing the HTML. This will be the easiest way in terms of storage.
If you decide to use this method note that you’ll need to do a lot of validation on the server to avoid cross site scripting attacks. Shortly put – make sure the HTML you get on the server doesn’t contain any javascript or any tags apart from those you want to support.
Its better to use Editor of AJAX Toolkit, doesnt required any other thing, its a complete editor, you can even color your font as u wanted.

C# HTML entities from XML string in DataGridView

I'm working with a Web Service that returns an XML string. I need to display the contents of the XML string in a DataGridView to users.
The XML string resembles something like this:
<Field id="13598" type="1"><p><strong>This </strong>is our <em>response</em></p></Field>
So far, I've managed to format output that's something like this which is displayed in my DataGridView:
e.g.
<p><strong>This </strong>is our <em>statement</em></p>
Using System.Net.WebUtility.HtmlDecode
Is there a way for me to display the formatted text in the DataGridView?
e.g.
This is our statement
I'm hoping to achieve useability as the intended users of the program might not be familiar with HTML.
I would've just removed the entities, but I need to retain them as they will be required for writing to an XML string later on as well, due to any editing.
e.g.
<p><strong>This</strong> is our updated <em>statement</em></p>
I've been toying with the idea of using a new form with a TextBox to enable editing as some of these strings might be much longer than this, and could contain other forms of formatting such as bullet points and the like. I'm not too sure how to go about with this. I'm not entirely sure if what I'm hoping to achieve is possible. If not, are there alternative methods for me to achieve this?
You could use the WebBrowser control, and set its content using the NavigateToString method

How to build a "word-by-word" autocomplete in WPF?

I already did some research and ended up with several autocomplete boxes which have one thing in common: they all match the whole expression which has been entered. Sometimes, they seem to be easily expandable, but at the end, they aren't.
However, I need a Textbox which allows the user to enter a word (e.g. "tag1"), displays a popup with some suggestions based on this single word, accept the suggestion with the return key and type in a new word (e.g "tag1 tag2") in the same textbox, with the popup popping up again. (I like the way CintaNotes handles this)
I need this for a tagging interface. It's often faster for the user to write the tags into a simple box, but sometimes, he needs assistance. This is what the autocomplete is for.
I've found some results, which don't work for my purpose (imho):
http://www.wpfpedia.com/item/details/743/wpf-autocomplete-textbox-control
http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx
http://www.codeproject.com/KB/WPF/autocomplete_textbox.aspx
http://weblogs.thinktecture.com/cnagel/2011/01/autocomplete-textbox-with-wpf.html
Btw, I really like the way the Tag-Box for SO operates.
Does anyone have an idea? Is there a "out-of-the-box" - solution somewhere, which suits my needs but I didn't find? Or do I have to build one myself?
Thanks! :)
I think you mean a textbox which autocomplete for multiple words.
Like TokenizedTexbox on WPF Extended Toolkit.
this is the page: http://wpftoolkit.codeplex.com/wikipage?title=TokenizedTextBox&referringTitle=Home
Probably you would need to create your own Dictionary object of Key and Value pairs and hook that Dictionary to the textbox events and popup a suggestions dialog that displays the Value(s) from your Dictionary
Check this implementation out: http://code.google.com/p/kocontrols/downloads/list
You may inject your own Search algorithm and your own converter which converts the selected element to text, which you display in the TextBox. You will have to modify it slightly but I think that you might be able to solve your problem by basing your control on this implementation.
I never thought about this type of use case.
Can't you use different textboxes for the different tags? Something similar to how goole code does it?
If you have time, you can use the RichEditControl or TextBox and apply the same pattern used in Intellisense engine or Code Completation enabled editors: Hook the text changes events, Recogize context (last/current word) and display the options list on popup control. And optionally, on commit (detect acceptation or space key), apply the format to the word.

HTML tags inside the Database - YAY or NAY?

I am using ASP.NET with C# and SQL Server 2005. I have a plane simple ASP.NET label on my form.
<asp:Label ID="lblBodyCopy1" runat="server"></asp:Label>
On Page load i populate this label with text that is saved in my database.
<p class="r_box_A">text text</p><p class="r_box_B">text text text</p>
Is this the right thing to do? If not what is then because i need to populate that label with text in such way that will have paragraphes and styles to each paragraph.
Thanks in advance!
Generally, no. You want your DB fields to be independant of the medium they'll be used in (because there may be many: HTML, Plain Text, etc)
What you should do is reformat your fields to whatever context they are being displayed. This implies that if you need the data in a 'richer' format, you typically use a markup scheme (markdown, perhaps) so you can process it and render it in whatever fashion you like.
You may, however, cache this formatting in another field, to speed up the retrieval process.
You can have HTML code in your Label as well as your Literal. A Literal control is much more light weight than a Label.. it's meant to write out text/html directly to the browser. A Label is little bulkier than a Literal, but has all benefits of a WebControl such as styling options etc.
Literal Control : Asp.NET Literal
Who enters these strings to the database? The usual problem with this approach is that:
you need to make sure that the resulting html is well formed
it can be not secure if people include scripts in the html stored in the database, etc.
So in most cases you want to store only real text in the database, and then format it and escape all html chars when adding to the page.
I personally would avoid storing the HTML in the database... but then again it depends on the context.
If you have a table designed for storing input from a rich textbox then you will somehow need to store any formatting that has been applied.
As others have mentioned, prehaps a safer alternative would be to create your own [bbCode] syntax, pretty much similar to the way the textbox on here (Stack OverFlow) works...
eg. any text in bold 'foo'
becomes [b]foo[/b]
There is added work involved because you will need to parse the text and replace any tags with the equivalent [bbCode] tags and then do the reverse when outputting to a web page, but I personally think it is a safer approach with regards to data integrity.
Why not store it as xhtml in the database - you can validate it before you push it in. use the XML field type. This way if your need to query on it you can, and you data is still structured.

Categories

Resources