views:

45

answers:

2

i have setup a profanity filter with bad words in a XML file and have the following function to run on my page to replace the words:

BadWordFilter.Instance.GetCleanString(TextBox1.Text);

i'm about to go through my entire site now wrapping that function around every little text variable one by one and it's going to be a huge pain in the butt

i'm hoping there's a way that i could just set my masterpage to automatically run all text through this thing on any page_load, so that the effect would be site-wide instantly. is this possible?

much appreciated for any help

+1  A: 

One quick tip I have is to use the tag mapping feature of asp.net for this:

  • Create a custom textbox class derived from the TextBox class
  • Override the get/set Text property & in the get part, return the cleaned string
  • Use tag mapping feature in the web.config file to replace all TextBox classes with your custom text box class & everything should work really well.

This link has a sample implementation which uses the HTMLEncode, but you get the idea: http://www.devwebpro.co.uk/devwebprouk-46-20071010ASPNETTagMapping.html

HTH.

Sunny
that looks like it could be exactly perfect thank you, but if i might add on an additional factor in this, perhaps you'd have another answer?i forgot to mention that i was planning on offering profanity filtration as an OPTION to users, meaning some would be running the site text through the function, some wouldn't. this means editing the web.config probably isn't an option for me right?is there another way you know of by any chance that can be done conditionally on page_load? if not thanks anyway that was helpful
korben
nevermind i figured i could just do it in the .cs files that are loading from the webconfig and it works great, thanks
korben
A: 

I realize you said Page_Load(), but I suspect this will do what you need.

In the Page.PreRender event, walk through the controls:

/* inside Page_PreRender() handler...*/
if (user_options.filterBadWords == true)
{
    FilterControls(this);
}    

/* this does the real work*/
private void FilterControls(Control ctrl)
{
    foreach (Control c in ctrl.Controls)
    {
        if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
        {
            TextBox t = (TextBox)c;
            /* do your thing */
            t.Text = BadWordsFilter(t.Text);
        }
        if (c.HasControls())
            FilterControls(c);
    }
}

This is a hack, which will get you through your current problem: overriding the TextBox control is ultimately a better solution.

egrunin