سؤال

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: 
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top