I am using asp.net 4, .net 4 and masterpages. I added in the following code to my child page
Private Sub FoodChain_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
MyBase.OnInit(e)
PopulateDropdowns()
HideQuestionDiv()
HideDropdowns()
End Sub
and ran my app. The app runs fine but when I went back to the design view I now get an error saying "object reference not set to an instance". This is only during design time view and referencing the main place holder.
Is this a bug or am I missing something ? As I said the application runs fine and there are now issues during runtime.
Try putting all your databinding inside the following. (sorry about the c# code). I think it is Me.DesignMode in VB.
if (!this.DesignMode)
{
PopulateDropdowns();
HideQuestionDiv();
HideDropdowns();
}
I'm not sure if this works correctly for nested user controls but worth a try. Note that this won't work inside a constructor, only other events.
MSDN here.
Related
ASP.NET WebForms application. There is a custom web control (IE inherits from System.Web.UI.WebControls.WebControl). I can put breakpoints anywhere in CreateChildControls() or any other overloaded method and these breakpoints work fine. However, if I try to add a breakpoint to a method that is bound to a Button on the page, I get an error on the breakpoint:
The breakpoint will not currently be hit. No executable code of the debugger's target code is associated with this line. Possible causes include: conditional compilation, compiler optimizations, or the target architecture of this line is not supported by the current debugger code type.
I've tried cleaning the solution, deleting all bin/obj folders, restarting local IIS, recycling the app pool, all to no avail.
Code is in VB.NET (sorry).
Here is the function definition (in the WebControl code):
Private Sub AddToCartClick(ByVal sender As Object, ByVal e As System.EventArgs)
// breakpoint on line 1
Here is the code that ties the function to the Button:
Dim button = new Button
AddHandler button.Click, AddressOf AddToCartClick
The code executes and has worked for a long time, but I cannot debug it with breakpoints.
I have created a composite control for a file upload input (using an HtmlInputFile control). My problema is that everything works fine until I try to save the file using HtmlInputFile's "SaveAs" in my page's code behind. Whenever I press the button for uploading the file, I get a "object not set to an instance bla bla bla". However if I pause execution right before the SaveAs (with a break point) and then got step by step (F10), all of the composite properties have the corresponding data and I can effectively upload the file.
I am quite sure this has to do with page and control lifecycle, but everything I have read so far has led me no where. Can anyone please shed some light as to how I can force the composite control to load/render before I execute the code behind Click event?
The code behind that triggers the SaveAs is pretty straight forward:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
fup.xmsPostedFile.SaveAs(Server.MapPath(String.Format("~/memberpages/{0}", fup.xmsFileName)))
End Sub
The fup.xmsPostedFile property only references the _inputControl.PostedFile property. Same with fup.xmsFileName.
any help will be greatly appreciated. Thanks in advanced.
Finally got it to work. Turns out I was missing the EnsureChildControls(). I changed the property for this:
Public ReadOnly Property xmsPostedFile() As HttpPostedFile
Get
Return _inputFile.PostedFile
End Get
End Property
to this
Public ReadOnly Property xmsPostedFile() As HttpPostedFile
Get
EnsureChildControls()
Return _inputFile.PostedFile
End Get
End Property
and it started working as expected.
I have an ASP.NET 4.0 application that uses CrossPagePostback. I just noticed that my originating page is being loaded twice and I can't find the reason why. Here is the general logic:
BasePage.vb:
Public Class BasePage
Inherits System.Web.UI.Page
Protected Overrides Sub OnPreInit(e As System.EventArgs)
If IsCrossPagePostBack Then Exit Sub
...
Common code
...
MyBase.OnPreInit(e)
End Sub
Protected Overrides Sub OnInit(e As System.EventArgs)
If IsCrossPagePostBack Then Exit Sub
...
Common code
...
MyBase.OnInit(e)
End Sub
Protected Overrides Sub OnLoad(e As System.EventArgs)
If IsCrossPagePostBack Then Exit Sub
...
Common code
...
MyBase.OnLoad(e)
End Sub
End Class
Page1.vb.aspx:
Public Class _Page1
Inherits BasePage
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
...
Page1 specific code
...
End Sub
End Class
Page2.vb.aspx:
Public Class _Page2
Inherits BasePage
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
...
Page2 specific code
...
End Sub
End Class
In Page1.aspx I have an ASP LinkButton with PostBackUrl set to Page2.aspx. I set breakpoints in OnPreInit, OnInit and OnLoad in BasePage.vb. When the LinkButton is clicked in Page1.aspx, all 3 page events fire for Page1.aspx - IsCrossPagePostBack=True, which is what I expect for the originating page. Then all 3 page events fire for Page2.aspx and IsCrossPagePostback=False which is again expected.
I would expect both pages to be done firing these page events at this point. I can even see that Page2.aspx has rendered and is visible in my browser, but for some reason Page1.aspx page events are once again fired and this time IsCrossPagePostback=False which allows undesired code to run for the originating page.
If you weren't setting breakpoints and debugging this code you'd never know it was happening. There aren't any noticable adverse effects from this happening, except that it's costing processing time and I KNOW it's wrong. I just can't determine what's causing Page1.aspx to be loaded again after Page2.aspx is loaded.
In my Call Stack window there is no caller, only a single line containing the current Sub (OnPreInit, OnInit or OnLoad).
I've traced through the code step by step. I get to the point that Page2.aspx is fully loaded and then end up in the page event for Page1.aspx again.
I also have a LinkButton control in Page2.aspx that has its PostBackUrl set for Page1.aspx - yet don't have this problem.
Does anyone know what might cause this or how to debug it? I've seen a few posts that say empty img tags will cause it, but that's not the problem here.
EDIT:
After an all-nighter of debugging and research, I've determined that my originating CrossPagePostback problem is in fact being caused by an IMG tag with a missing image. This is a big problem for me as the IMG SRC= URL is coming from a data received in a REST response I have no control over.
The REST response contains lots of data, some of which are URLs to images relating to a product. The images are essential to the page information. There are millions of products and it just happens that I noticed this problem on a product that has missing images. 99.99% of all the products have valid URLs for the images, so this is not going to be a common problem.
Yet, I really don't want my originating page to be loading twice and I'd like to stop it from happening. Is this a bug that an originating page in a CrossPagePostback scenario will load twice if an IMG SRC= is broken in the target page?? Is there a fix??
Am going to do some more research, but figured I'd post a followup to my question right away. Anyone know of a solution/fix? I've read through similar posts here on SO that a missing image src will cause an originating CrossPagePostback page to load twice. The answer always seems to be either correct the src URL or make the image available so this don't happen. Unfortunately, I don't have either of those options - the img SRC is absolutely correct as is provided in the REST response received and the image is not mine, it's supposed to be on a remote server as specified in the REST response.
Aside from checking the existence of each individual image before rendoring my page, I don't know of a workaround. Checking for existance just isn't a good option.
After an all-nighter of debugging and research, I've determined that my originating CrossPagePostback problem is in fact being caused by an IMG tag with a missing image. This is a big problem for me as the IMG SRC= URL is coming from a data received in a REST response I have no control over.
The REST response contains lots of data, some of which are URLs to images relating to a product. The images are essential to the page information. There are millions of products and it just happens that I noticed this problem on a product that has missing images. 99.99% of all the products have valid URLs for the images, so this is not going to be a common problem.
Yet, I really don't want my originating page to be loading twice and I'd like to stop it from happening. Is this a bug that an originating page in a CrossPagePostback scenario will load twice if an IMG SRC= is broken in the target page?? Is there a fix??
Am going to do some more research, but figured I'd post a followup to my question right away. Anyone know of a solution/fix?
Thanks.
My module's Page_Load event is firing twice for each "actual" load. On the initial load both loads' Page.IsPostBack property is false.
I've renamed Page_Load to Module_Load to verify the name wasn't an issue. I've made sure the method isn't handling both Me.Load and MyBase.Load, which has been the case in the past.
The only thing I'm doing out of the ordinary is that my module is inheriting from an intermediate base class. Could this be the culprit?
My module:
Namespace Modules.RedactedNamespace
Public Class List
Inherits RedactedModuleBase
Protected Sub Module_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.IsPostBack Then
BindList()
End If
End Sub
End Class
End Namespace
My base:
Namespace Modules.RedactedNamespace
Public MustInherit Class RedactedModuleBase
Inherits DotNetNuke.Entities.Modules.PortalModuleBase
End Class
End Namespace
Edit (This fixed it) - I had an Image without an ImageUrl. Presumably this is set by my CollapsiblePanelExtender but rendered with a blank src.
This can happen if you have an img tag with an empty src attribute.
I know this sounds strange, but I believe it has to do with the web browser trying to figure out how to load the image with a blank SRC.
I don't know the protocols involved, but I'd bet there is some ambiguity regarding how to resolve empty string.
So, in the case of some browsers, it actually fires a web request to the current URL hoping the image comes back.
Sounds like a reasonable assumption, but it just so happens to break many ASP.Net web forms.
I have a situation where I need to ignore parts of page load sub inside a isPostback = true. Basically inside the isPostBack I want something like if button1 caused postback.... do this else do this...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = True Then
If TextBox1.Text <> String.Empty Then
CheckDate()
End If
End If
End Sub
I think what you need is the reference to the control name which triggered the postback.
http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
Implement the solution which is there in the above link (Got it from here.... How to Get Source of postback)
If the control name is your button then do not do what needs to be done upon postback.
HTH
You should probably not have all this going on inside the Page_Load event. Instead, you should be handling events for each control that can cause a postback. This helps with code clarity, and ease of maintenance, not to mention better control in the first place.
Here's a nice brief blog entry I found on the subject: http://www.sitepoint.com/blogs/2007/01/21/page_load-is-evil/
Incidentally, handling events is much different in ASP.NET than in other environments, My guess, just based on the fact that you're trying to accomplish this in the Page_Load event is that you're not yet "getting" the event-driven programming model. (If I'm wrong, I apologize, I don't mean to be insulting).
If I'm right, however, once you get used to it, it's going to be a lot simpler for you than things were in the classic ASP days, for example, where you had to do things like try to figure out which button was clicked. Here's another nice article to explain this further: http://articles.sitepoint.com/article/driven-asp-net-development-c
It's hard to see this as a good idea. From the short snippet you posted, it looks like what you really need is a Validation control attached to your textbox.
Have a look at the POSTed items. You should see some sort of reference to that button in there. IIRC, if it was clicked, you will see some sort of reference in there, and if it wasn't it wouldn't be in there.
I had this same problem a while a ago and that's how I circumvented loading some stuff.