Response Object not working in Master Page

M

mike.gilmore

Hi,

I'm trying to move all my custom validation code into the master
page. Unfortunately anytime I use the response, request, or trace
objects in any master page module (other than page_load), " Object
reference not set to an instance of an object." is the error I
receive.

Below is a sample of code that will not work on my web server. Any
suggestions/advice are appreciated.

Thanks.

Debug.aspx.vb (btnGo - is a web control object in Default.aspx)

Partial Class Debug
Inherits System.Web.UI.Page

Protected Sub btnGo_Command(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.CommandEventArgs) Handles btnGo.Command

Dim objMaster As New DebugMaster
With objMaster

.CheckResponseObject()

End With



End Sub
End Class

DebugMaster.Master.Vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Write("Inside the Master Page - Page_Load event
handler")

End Sub

Public Sub CheckResponseObject()

Response.Write("Inside the Master Page - CheckResponseObject
method")

End Sub


----------------------

On load the Master Page's page load "Response.Write" works, however
when the btnGo event is called (thus calling CheckResponseObject), the
page returns an " Object reference not set to an instance of an
object." exception.

Similar errors are called when I use the request or trace objects as
well.

Any advice on how to resolve would be greatly appreciated.

Thanks,

Mike
 
M

Mark Rae [MVP]

In addition to Peter's response, I'm slightly curious as to what you're
trying to achieve here...

There's no need to use Response.Write in ASP.NET - just put a breakpoint
where you would like your code to break, and hit the button...
 
M

Mark Fitzpatrick

To help the debug process, enable tracing for your application and use
Trace.Write() to output messages to the trace log for testing. It's much
more powerful than the old Response.Write method of dumping variables and
hints at different points in the page.
 
M

mike.gilmore

Thanks to all for responding to my post and on a Saturday! In an
effort to make my example simple, I rewrote it using only one
response.write statement. In reality, I'm actually trying to call
Request.Browser.Cookies (as part of my validation routine). I'm not,
nor do I in practice, using response.write in my code ;-).

Neither the response, request, or trace objects work. All return the
"Object reference not set to an instance of an object." exception.

I will read the link provided.

Again, thank you.
 
M

Mark Fitzpatrick

The Response object is actually a shortcut. When you have a normal page, the
System.Web.UI.Page class basically let's you use Response, Request, etc.
because it's actually giving you a shortcut to the real class. Anywhere
outside of a page (and this includes a MasterPage because it inherits from a
different base clase) you need to use HttpContext.Current.Response or
HttpContext.Current.Request. Then you can get at the objects and properties
you want. The same goes for things like the Session and Server objects as
well.
 
M

mike.gilmore

Mark,

Thanks for the reply. I implemented your logic using the full name
and it worked!

The one thing I don't understand (and probably never will) is why the
"shortcut" response/request worked in the master page's page_load.

Again thanks, you saved many a headache...
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top