Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Replace with HTML

Status
Not open for further replies.

gi11ies

Programmer
Mar 26, 2002
52
Im creating a simple forum just now that runs of an access datbase, I have the bare bones of the whole thing functioning, though Im wondering how I use the Replace command to post html into the datagrid.

For example if a user enters a web address - ie ... I insert it into the database as part of the text for a posting as [link]www.website.com[/url]

ie

blah blah blah blah [link]www.website.com[/url] blah blah blah blah blab blah blah.

I already use the Replace command within the datagrid to insert '<br>' when I display the code on the page with.

<asp:label text='<%#Replace(Databinder.Eval(Container.DataItem,"thread_text").tostring(), VbCrLf, "<br />")%>' ID="lblText" runat="server"/>

Im wondering how do I, or if I can add additional parameters to the replace command Replace command for converting my [link]www.website.com[/url] to < a href='
Im also wondering how I do the same for images - ie give url address for an image
image.jpg


Hope Im making sense? Can I not do this with a datagrid, and have to use DataList instead or something else?

Any help would be great.

Gillies
 
Is it just me, or is this ironic that you'd be asking it at this web site?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Yeah maybe ironic :) its just something VERY basic Im working on in my spare time, its for a fishing club.

Gillies
 
yes you could, but I would suggest looking for some open source forum software and save yourself the effort.

not sure if dotnetnuke ( has forums but the software is pretty cool adn it is free

bassguy
 
The best open source forum for ASP.NET is at but to answer your question, you don't want to run a string.Replace(), rather you'll want to check out Regex.Replace().


Code:
//untested
//it would be something like this
string modifiedText = Regex.Replace( "blah [URL unfurl="true"]www.site.com[/URL] blah", @"[URL unfurl="true"]www\..*?\.\w{2,3}",[/URL] @"[link]$&[\link]" );
 
Thanks for the replies everyone, BoulderBum ... built from what you posted.

What I ended up doing though was using the findcontrol for the datagrid, and then the replace ... so code Im using is:

Dim dgItem as DataGridItem

For each dgItem in sysacDG.items
' Display an Image in Post
Dim userPost As String = CType(dgItem.FindControl("lblText"), Label).Text
Dim userPost_imgOpen As String = userPost.Replace("", "<img src='")
Dim userPost_imgClose As String = userPost_imgOpen.Replace("", "'>")
' Display a Hyperlink
Dim userUrlOpen As String = userPost_imgClose.Replace("","<a href='") Dim userUrlClose As String = userUrlOpen.Replace("","'>")
Dim userDescOpen As String = userUrlClose.Replace("[desc]","")
Dim userDescClose As String = userDescOpen.Replace("[/desc]","</a>")
' Display Bold text
Dim userBoldOpen As String = userDescClose.Replace("","<b>")
Dim userBoldClose As String = userBoldOpen.Replace("
","</b>")

CType(dgItem.FindControl("lblText"), Label).Text = userBoldClose
Next

Its working good so far, so am happy :)

Gillies
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top