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 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