0
page = (Page)HttpContext.Current.CurrentHandler;
TextBox txtEndUser = ((TextBox)page.FindControl("txtEndUser"));

Here txtEndUser always comes as null.

How can we fix this ?

teenup
  • 7,131
  • 12
  • 61
  • 114

3 Answers3

0

If the page does not directly contains the TextBox control, then you wont be able to retrieve it with the FindControl function. You need to create a function that search contained containers for the target control ..

Akram Shahda
  • 14,295
  • 4
  • 43
  • 64
0

It appears to me that this code snippet is in HttpModule so probably, the request has not been handed out to a handler yet (for example, you're in BeginRequest).

Reference: Why HttpContext.Current.Handler is null?

Community
  • 1
  • 1
Ahmed Magdy
  • 5,576
  • 8
  • 40
  • 74
  • Here Handler is not coming as null, Handler is perfectly set to the Page object and I am using other members of Page object. Rather, the txtEndUser is coming out as null, I am using this code in a Class method that is being called from a UserControl loaded in a Sharepoint Webpart which itself is in asp.net Page. – teenup Jun 15 '11 at 12:49
0

As a workaround if you don't find the way using FindControl you can add public getter to your custom page class the have something like this:

WebForm1 myForm = (WebForm1)HttpContext.Current.CurrentHandler;
TextBox txtEndUser = myForm.GetEndUserTextbox();
Shadow Wizard Says No More War
  • 64,101
  • 26
  • 136
  • 201