tags:

views:

22

answers:

2

I admit, I'm an .NET n00b. Basically what I'm trying to do is I have a page with a text box on it and an image button. On click of the image button I want it to show a view control I have set up. Inside this view control is an image and some text. So this is what I have in my code-behind.

protected void btnSubmit_Click(object sender, ImageClickEventArgs e) { string email = txtUnsubscribe.Text; vwSuccess.Visible = true; }

Simple right? Well when I click on the button for submit, I get the "Object reference not set to an instance of an object." error message. Where am I going wrong?

A: 

Are txtUnsubscribe and vwSuccess both not null? Have you tried stepping through it in a debugger?

JB King
A: 

Based on the information (which is to say, based on not much), I'd guess you have an issue with the execution order.

Since you have txtUnsubscribe and vwSuccess members, I'm assuming you initialize these somewhere. If they are auto-generated from the aspx templates, then they are initialized automatically before the event handling so that method will never throw a null reference exception.

If the null reference exception is thrown by that event handler, then it must mean that one of these variables is not initialized (which means that at least one of them is not autogenerated from the aspx and instead should be initialized manually). If you are initializing the variables, then you are likely doing it too late in an event like PreRender or Render.

When you click the button in the browser the browser performs a PostBack to the web server. By default at this point the web server re-constructs the page, performs the event handling and then renders it back to the client. It is important to realize that the page isn't maintained on the server between requests.

The order of events during page load/postback can be found from MSDN: http://msdn.microsoft.com/en-us/library/aa719775(VS.71).aspx

Of course if the exception isn't thrown by that event handler, this whole answer is likely to be wrong and there's probably some simpler issue.

Mikko Rantanen