0

I have a solution with 2 projects in it. One project(TestControl) contains the aspx file. then the other(Controls) for the usercontrols. I have already made it possible for the aspx to display the user controls via referencing the Controls in the project and copying the files to the TestControl Project. But the Sub seems not to work and it sends an error "Object reference not set to an instance of an object" even at a simple response.redirect code. I tried a msgbox and it works and also placing the code in page_load. But inside a sub it's not working. Any ideas why? and a fix?

Simple code in the uc3.ascx file

   Public Sub Redirect()

        Response.Redirect("http://www.google.com")
    End Sub

Here is the error exception

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=System.Web
  StackTrace:
       at System.Web.UI.UserControl.get_Response()
       at Controls.uc3.Redirect() in C:\Users\Nelbin\Documents\Visual Studio 2010\Projects\TestApp\Controls\uc3.ascx.vb:line 9
       at Controls.uc1.btnUserControl3_Click(Object sender, EventArgs e) in C:\Users\Nelbin\Documents\Visual Studio 2010\Projects\TestApp\Controls\uc1.ascx.vb:line 16
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

1 Answers1

3

From reason which I don't see from your code the control does not access Response object. Try

System.Web.HttpContext.Current.Response

intead of just Response.

Public Sub Redirect()
    System.Web.HttpContext.Current.Response.Redirect("http://www.google.com")
End Sub

In web project System.Web.HttpContext.Current should never be nothing.

IvanH
  • 5,039
  • 14
  • 60
  • 81
  • Thanks it worked! Now my problem is still the same regarding with passing value from a user control to another – Halimbawa Dokumento Oct 14 '13 at 07:36
  • @HalimbawaDokumento: notice that this is definitely discussed in my Q&A: `Response` was null. Next step is to find out why, but first step was to find out what was null. – John Saunders Oct 14 '13 at 07:38
  • @JohnSaunders I saw that in your thread but can't think of a way to prevent that its null. Sorry I'm a noob haha. Now my next problem is regarding the value being pass on the user control to another. The control I believe is not initiated for no reason >. – Halimbawa Dokumento Oct 14 '13 at 07:47
  • @HalimbawaDokumento: use `System.Web.HttpContext.Current.Response` instead – John Saunders Oct 14 '13 at 08:00
  • could you give an example? I am getting the value via property or sub I don't know how can I use that part :( – Halimbawa Dokumento Oct 14 '13 at 08:30