Click here to Skip to main content
15,880,392 members
Articles / Web Development / ASP.NET

Display Emoticons on an ASP.NET Webpage

Rate me:
Please Sign up or sign in to vote.
4.19/5 (21 votes)
28 Jun 2003CPOL1 min read 117.3K   4K   47   7
Code to replace emoticons within a string by their HTML-images.

Sample Image - AspNetEmoticons.jpg

Introduction

Inspired by a question in the CodeProject's ASP.NET forum, I decided to quick-assemble some code for displaying emoticons within a web page.

Displaying emoticons is the task of replacing text-fragments within a string (e.g. ":-)", ":-(" ) by some HTML code that displays an image (e.g. Laughing smiley and Crying smiley). So simple spoken, the whole project is a nicely encapsulated string-replace that hopefully is useful to the guy who asked the question, as well as to other programmers.

Description

The project contains a ready-to-compile-and-run Visual Studio 2003 solution with one Web Form web page Index.aspx to test out the functionality. The effective code is contained within the class Emoticon in the file Emoticons.cs.

The class Emoticon has two static members. The first is:

/// <summary>
/// Returns a collection of all available emoticons.
/// </summary>
public static Emoticon[] All

to get a collection of all available emoticons. Within this member, the actual references to the image files are created, so if you want to add your own emoticons, you must place the news images within the Emoticons subfolder and modify this member to "know" the new emoticons.

The second static member is the function:

/// <summary>
/// Returns a string with all emoticons replaced by their images.
/// </summary>
public static string Format( string input )

This member function does the core replacement of an input string with the HTML-code to display the the emoticons. Any occurrence of the emoticons registered within All are replaced and returned by this function.

Using the code

To use the code, simply call the Format function like this:

C#
string text = Emoticon.Format( "Some text with a smiley :-) emoticon." );

History

  • 2003-06-29: Article created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
Generalwww.dotnetobject.com Pin
farooq14820088-Aug-11 18:29
farooq14820088-Aug-11 18:29 
GeneralNice work!!! Pin
simpa29-Sep-10 2:04
simpa29-Sep-10 2:04 
GeneralThx Pin
Morten Jørgensen1-Feb-10 10:40
Morten Jørgensen1-Feb-10 10:40 
GeneralEmoticon.Format in a bound dataGrid Pin
gadgetfbi16-Jul-03 13:45
gadgetfbi16-Jul-03 13:45 
GeneralRe: Emoticon.Format in a bound dataGrid Pin
Uwe Keim20-Jul-03 3:33
sitebuilderUwe Keim20-Jul-03 3:33 
GeneralPlease be patient... Pin
Uwe Keim29-Jun-03 8:54
sitebuilderUwe Keim29-Jun-03 8:54 
GeneralRe: Please be patient... Pin
Uwe Keim29-Jun-03 9:02
sitebuilderUwe Keim29-Jun-03 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.