HTML in User Comments

G

Guest

I have a repeater where I output user comments (255 char limit).

I want to give the user the ability to add <i>italic</i> and <b>bold</b> font using standard HTML tags.

I changed the repeater to have a label with id=RepeaterCommentText. I then registered the ItemDataBound event to be handled by CommentBound (which has the appropriate parameters). On Comment Entry I change the tags to have square braces (ex. "<b>" to ""). In CommentBind I change the specially formatted tags back to HTML (this code is at home and I’m not. The actual code does work, I know there are probably some minor errors in this 1 block, but you get the idea).

// code in CommentBind
Label lbl = (Label)e.FindControl("RepeaterCommentText");
String text = lbl.text.Replace("", "<b>");
text = text.Replace("
", "</b>");

in the code that handles comment entry I do the reverse (replace "<b>" with "").

Everything works fine. However, if the user enters text like the following:

Text:
<i>Italic
<b>bold

Then the rest of the page becomes bold and italic, as there are no closing tags.
What I thought about doing on comment entry was

// keep the normal tags
CommentText = CommentText.Replace("<b>", "<b>");
CommentText = CommentText.Replace("</b>", "</b>
");
int startTags, endTags; // counters
startTags = 0; endTags = 0; // init counts

// count opening tags
while (CommentText.IndexOf("<b>") != -1)
{
CommentText.Remove(CommentText.IndexOf("<b>"), 3);
startTags++;
}

//count closing tags
while (CommentText.IndexOf("<b>") != -1)
{
CommentText.Remove(CommentText.IndexOf("</b>"), 4);
endTags++;
}

// if the user failed to enter a few closing tags, add them
if ( startTags > endTags)
{
while (startTags > endTags)
{
CommentText += "
"; endTags++;
}
}

// if the user failed to enter a few opening tags, add them
if ( endTags > startTags )
{
while ( endTags > startTags )
{
CommentText = "" + CommentText; startTags++;
}
}

I don't like this method though as it seems like way too much code for a simple task.

Any suggestions for another approaches?

Thanks,
Ryan
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Ryan,

There are better solution for this, take a look at this control :
http://www.gotdotnet.com/community/usersamples/

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Ryan Riddell said:
I have a repeater where I output user comments (255 char limit).

I want to give the user the ability to add <i>italic</i> and <b>bold</b> font using standard HTML tags.

I changed the repeater to have a label with id=RepeaterCommentText. I then
registered the ItemDataBound event to be handled by CommentBound (which has
the appropriate parameters). On Comment Entry I change the tags to have
square braces (ex. "<b>" to ""). In CommentBind I change the specially
formatted tags back to HTML (this code is at home and I'm not. The actual
code does work, I know there are probably some minor errors in this 1 block,
but you get the idea).
// code in CommentBind
Label lbl = (Label)e.FindControl("RepeaterCommentText");
String text = lbl.text.Replace("", "<b>");
text = text.Replace("
", "</b>");

in the code that handles comment entry I do the reverse (replace "<b>" with "").

Everything works fine. However, if the user enters text like the following:

Text:
<i>Italic
<b>bold

Then the rest of the page becomes bold and italic, as there are no closing tags.
What I thought about doing on comment entry was

// keep the normal tags
CommentText = CommentText.Replace("<b>", "<b>");
CommentText = CommentText.Replace("</b>", "</b>
");
int startTags, endTags; // counters
startTags = 0; endTags = 0; // init counts

// count opening tags
while (CommentText.IndexOf("<b>") != -1)
{
CommentText.Remove(CommentText.IndexOf("<b>"), 3);
startTags++;
}

//count closing tags
while (CommentText.IndexOf("<b>") != -1)
{
CommentText.Remove(CommentText.IndexOf("</b>"), 4);
endTags++;
}

// if the user failed to enter a few closing tags, add them
if ( startTags > endTags)
{
while (startTags > endTags)
{
CommentText += "
"; endTags++;
}
}

// if the user failed to enter a few opening tags, add them
if ( endTags > startTags )
{
while ( endTags > startTags )
{
CommentText = "" + CommentText; startTags++;
}
}

I don't like this method though as it seems like way too much code for a simple task.

Any suggestions for another approaches?

Thanks,
Ryan
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi again,

Sorry , I sent you the wrong link, this is the correct one:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/editing/tutorials/html_editor.asp


Sorry for that :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Ryan Riddell said:
I have a repeater where I output user comments (255 char limit).

I want to give the user the ability to add <i>italic</i> and <b>bold</b> font using standard HTML tags.

I changed the repeater to have a label with id=RepeaterCommentText. I then
registered the ItemDataBound event to be handled by CommentBound (which has
the appropriate parameters). On Comment Entry I change the tags to have
square braces (ex. "<b>" to ""). In CommentBind I change the specially
formatted tags back to HTML (this code is at home and I'm not. The actual
code does work, I know there are probably some minor errors in this 1 block,
but you get the idea).
// code in CommentBind
Label lbl = (Label)e.FindControl("RepeaterCommentText");
String text = lbl.text.Replace("", "<b>");
text = text.Replace("
", "</b>");

in the code that handles comment entry I do the reverse (replace "<b>" with "").

Everything works fine. However, if the user enters text like the following:

Text:
<i>Italic
<b>bold

Then the rest of the page becomes bold and italic, as there are no closing tags.
What I thought about doing on comment entry was

// keep the normal tags
CommentText = CommentText.Replace("<b>", "<b>");
CommentText = CommentText.Replace("</b>", "</b>
");
int startTags, endTags; // counters
startTags = 0; endTags = 0; // init counts

// count opening tags
while (CommentText.IndexOf("<b>") != -1)
{
CommentText.Remove(CommentText.IndexOf("<b>"), 3);
startTags++;
}

//count closing tags
while (CommentText.IndexOf("<b>") != -1)
{
CommentText.Remove(CommentText.IndexOf("</b>"), 4);
endTags++;
}

// if the user failed to enter a few closing tags, add them
if ( startTags > endTags)
{
while (startTags > endTags)
{
CommentText += "
"; endTags++;
}
}

// if the user failed to enter a few opening tags, add them
if ( endTags > startTags )
{
while ( endTags > startTags )
{
CommentText = "" + CommentText; startTags++;
}
}

I don't like this method though as it seems like way too much code for a simple task.

Any suggestions for another approaches?

Thanks,
Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top