3

i am trying to use http Module to disable textbox of each page. Here is my sample coding

  public void context_OnPreRequestHandlerExecute(object sender, EventArgs args)
    {
        try
        {
            HttpApplication app = sender as HttpApplication;
            if (app != null)
            {
                Page page = app.Context.Handler as Page;
                if (page != null)
                {
                    page.PreRender += OnPreRender;
                    page.PreLoad += onPreLoad;
                }
            }



        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.Message);
        }
    }


    public void OnPreRender(object sender, EventArgs args)
    {
        Page page = sender as Page;
        if (page.IsCrossPagePostBack)
        {
            DisableAllTextBoxes(page);
        }

    }

    private static void DisableAllTextBoxes(Control parent)
    {

        foreach (Control c in parent.Controls)
        {
            var tb = c as Button;
            if (tb != null)
            {
                tb.Enabled = false;
            }
            DisableAllTextBoxes(c);
        }

    }

This coding can work very well but when i use server.transer to another page. Button are not able to disable already. For example webform1 transfer to webform2. Webform 1's button is able to disable but webform2 is not able to disable. Can anyone solve my problem?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
user998405
  • 1,329
  • 5
  • 41
  • 84

2 Answers2

0
  • Use LINQ to get all your textbox controls.
  • Don't use Server.Transfer()

Create an extension method on ControlCollection that returns an IEnumerable. That handles the recursion. Then you could use it on your page like this:

var textboxes = this.Controls.FindAll().OfType<TextBox>();

foreach (var t in textboxes)
{ 
    t.Enabled = false;
}

...

public static class Extensions
{
public static IEnumerable<Control> FindAll(this ControlCollection collection)
{
    foreach (Control item in collection)
    {
        yield return item;

        if (item.HasControls())
        {
            foreach (var subItem in item.Controls.FindAll())
            {
                yield return subItem;
            }
        }
    }
}
}

Taken from this answer.

Community
  • 1
  • 1
p.campbell
  • 98,673
  • 67
  • 256
  • 322
0

Server.Transfer DOES NOT go through all http module pipline (thats why context_OnPreRequestHandlerExecute isn't executed for you )

you should try Server.TransferRequest or response.redirect or HttpContext.Current.RewritePath

Royi Namir
  • 144,742
  • 138
  • 468
  • 792