debuggin in aspx - asp.net

I am trying to debug some code in my code behind files for as aspx page. I have debug set to true both in the page and in my web.config.
Can someone tell me why a) the breakpoint never fires even though the dropdown gets filled and b) why when i uncomment the msgbox it never fires and the dropdown does NOT fill.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If IsPostBack Then
ddlCity.Items.Clear()
Dim context As New enerteckEntities()
'Dim query = context.DistinctCityFromZiptax(Convert.ToInt16(ddlState.SelectedValue))
Dim query = From c In context.ziptaxes Where c.StateID = ddlState.SelectedValue Order By c.City Select c.City, c.ZipTaxId
'MsgBox(query.Distinct().ToList())
ddlCity.DataSource = query.Distinct().ToList()
ddlCity.DataValueField = "ziptaxid"
ddlCity.DataTextField = "City"
ddlCity.DataBind()
End If
End Sub

Well based on your code, you will only execute this after a PostBack and not on initial Page Load.
MsgBox will never show up because this is not a WinForms application! You cannot use that in a web.application. If you want you can use Response.Write() or just add a dummy Label to your page and set the text property temporarily. It is the same effect.
The easiest would be to just debug it. Make sure your breakpoints are full Red dots, and you must be in Debug mode. If you are trying to debug from IIS then you must attach to process. If you are using IIS7 (I am assuming you are) then you must go to:
Debug Menu > Attach to Process > Find the process named "w3wp.exe" and double click on it.
You are now attached.
If your breakpoints are not full red dots after starting debug, then your compiled code and your debug files do not match. Do a Rebuild as opposed to a Build. Beyond that you might be having a funky problem, you can try deleting your obj and bin folders (make sure to save any third party dlls first).

To hit a break point visual studio has to attach to the process hosting the aspx page.
This is typically done in a Web Application Project by pressing the F5 key or clicking on the "Debug" menu and clicking "Start Debugging".

Related

ScriptManager.RegisterStartupScript not working if Response.redirect used

In my page load based on certain condition i call UserMsgBox("user Not Authorize") and it is working. while if i call Response.Redirect("Home.aspx") after that it is not showing alert message.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("val") IsNot Nothing Then
...
Else
UserMsgBox("user Not Authorize")
Response.Redirect("Home.aspx")
End If
End Sub
Private Sub UserMsgBox(sMsg As String)
Dim jScript As String = "alert('" + sMsg + "');"
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "onclick", jScript, True)
End Sub
i want alert message to be popup and Response.Redirect("Home.aspx") also work. any would be appreciated .
You have to set some kind of session() value and get/grab/see this on the home.aspx page.
The problem is at "this instant" you are on the current page. When you run that userMsgbox code, it is injected into the CURRENT page. Unless that page makes a full round trip
eg:
User hits some button on web page.
Web page travels up to web server.
Code behind runs - maybe you modify some controls.
And THEN you as the last thing, inject the userMsgbox.
Now the page travels back down to client side.
The browser side then renders the page.
After it renders, then your injected js runs.
But, you NEVER let that page make it back to the client side, you have this:
Response.Redirect("Home.aspx")
So we never let the page travel back - we chop it off and say load another page.
But our current code context is the currently loaded page. Not the one you jumping to.
So, your current page-behind code is working on the current page - and controls you change/set/modify or script you inject is going to operate on that current loaded page.
With the re-direct then that page never makes the trip down to the client side - you sending to a NEW page home.aspx
Two ideas:
Use/set some kind of session().
Say like
Session("HomeMessage") = "user Not Authorize"
Response.Redirect("Home.aspx")
Then in the home.aspx page (load event), you need:
if isPostBack = false then
if Session("HomeMessage") <> "" then
Call UserMsgbox(Session("HomeMessage")
Session("HomeMessage") = ""
end if
end if
So now when the full home page renders browser side, then your msgbox you injected will also run. That might not be the best UI.
But with that setup (the re-direct occurring server side), then you can't do this on the previous page (well, actually our current page). But that page never travels back to the client side - you re-direct to home page, and that what gets send down to the browser.
(so you need that userMsgbox sub in the home.aspx page). I mean, if you set some text box on that page - we never see that either - since the code jumps to load a fresh new (different page) that the code behind is running against.
And this does mean that the home page will display FIRST and then the msgbox will show.
The other way?
Well, you could allow the current page to post back, and have the js script display the message AND ALSO do the jump to the other page.
This would thus allow the current page to display with the msgbox. You then hit ok, and the client side does the jump to home. (you remove your response.redirect.
So you could replace this:
UserMsgBox("user Not Authorize")
Response.Redirect("Home.aspx")
With say a routine like this:
UserMsgBoxJump("user Not Authorize","Home.aspx")
and
Private Sub UserMsgBox2(sMsg As String, strURL As String)
Dim jScript As String = "alert('" + sMsg + "');window.location.href = '" & strURL & "';"
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "onclick", jScript, True)
End Sub
So now, we let the page travel back (it will render), and after full page render, then your injected js can run. Then the msgbox will display.
And when the user hits ok, then the client side js code does the navigation to the target page.
So, it really depends on how you want to do this. Probably the 2nd idea is more flexible, since then you don't have to mess up the home page on-load event to check the session.
So keep the post/page life cycle in mind.

OpenFileDialog in Webform Application

I tried to use OpenFileDialog() control inside my Webform Application but it's not working.
I tried this coding but it gave me error :
System.Windows.Forms.OpenFileDialog Not defined
Private Sub FnOpenFileDialog()
Dim openfile As New System.Windows.Forms.OpenFileDialog
openfile.Filter = String.Format("Image file (*.jpg)|*.jpg")
openfile.Multiselect = True
openfile.ShowDialog()
End Sub
Private Sub btnUpload_PreRender(sender As Object, e As EventArgs) Handles btnUpload.PreRender
Dim objThread As New Thread(AddressOf FnOpenFileDialog)
objThread.IsBackground = True
objThread.SetApartmentState(ApartmentState.STA)
objThread.Start()
End Sub
Or is there any way on how to do it? I don't want to use FileUpload control because it shows textbox + button. I only want to show the Button() control.
I refer to this website How to Apply OpenFileDialog Function to a WebApplication
In a ASP.NET web application, the VB code is run on the web server. So even if you would succeed in using OpenFileDialog that would result in the dialog being showed on the server, not in the browser.
JavaScript won't help you either, as the browswer sandbox in which JavaScripts run doesn't allow access to the file system (which is good from a security perspective). You simply have to accept that you need to work with the file upload control. You might be able to customize the appearance a bit by applying styles through css.
Use HTML input file ,as follow:
<input id="File1" type="file" />

Unable to debug web control click handler

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.

ASP.NET Wizard Back Button won't work

I have a ASP.NET Wizard running my checkout process of my shopping cart. I just added a Paypal Express checkout link to my 2nd step. The Paypal process takes the user away from the page and then redirects them back to my wizard when they're done. I'm parsing an HTTP parameter with Request.QueryString when the user comes back from Paypal to set the wizard to step 3. This loads just fine, but when I click on the Back button (of the wizard), it does a postback but stays on step 3. Can anyone think of a reason for this? The link it's referencing still has the HTTP parameters, but I'm checking for a postback prior to programmatically setting the wizard step based on the parameter. Does anyone have any experience with this?
Well, I'm not sure why it was doing it, but overriding the blackbox PreviousButtonClick event on the wizard with the following code fixed it. It seems to me like this should be the behavior the button was implementing anyway, but it wasn't. Weird.
Protected Sub wizSubmitOrder_PreviousButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizSubmitOrder.PreviousButtonClick
Dim previousStepIndex As Integer = wizSubmitOrder.ActiveStepIndex - 1
wizSubmitOrder.MoveTo(wizSubmitOrder.WizardSteps(previousStepIndex))
End Sub

Visual Studio Debugger. ASP.NET ImageButton Click events not firing

When clicking button in debug mode, the page reloads but the break points inside of the click event are never reached. However, breakpoints in the page_load work just fine.
Basically, it's as if code inside of the button's click event is not being executed.
I checked the site into source control and another developer tried it on his computer. The click events worked just fine there, catching the break points inside.
I tried another site on my computer and it has the same issue. So it's something specific to my computer, and not specific to any site.
Is there some setting I may have mistakenly changed that could cause click events to stop working while debugging?
Any help would be appreciated.
Edited:
This issue is happening on all sites I run in my debugger, and the buttons are not created dynamically.
Edited:
There are no indication of problems when adding the break points. I don't think it's a breakpoint issue, I believe it's an issue with click events not firing. I put identical code on another developers machine and the click event's worked fine while debugging on that machine.
If its something specific to your machine, and after all things fail, I would do a repair on Visual Studio. I've had to do that a couple times when things get weird and it usually works. But remember to exhaust all other avenues first.
How is the button created? If it's added to the page dynamically, remember that this must be done before the load event, or the events won't be wired up. That is the most common reason I've heard for this kind of thing.
Does it say that "symbols will not be loaded" OR "source file is different" when you put the breakpoint inside the button_click event?
If so, you have modified the code files & built the binary (which is different from what site is using).
In my Page_Load event I would check the IsPostBack property to make sure that it is actually a post and not get. I would verify that the Controls collection is populated and all control IDs match those in the Request.Forms.Keys.
The reason for this suggestion is that if you have any add-on in the browser or fancy proxy that messes up with submitted forms and input names are modified, events won't fire for those controls.
I'll take a stab as I remember something similar:
I had upgraded an app from vs2005 to vs2008 and my button code click worked in 2005, but in 2008 I needed to add a "Handles" qualifier at the end of the declaration like this:
Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
for whatever reason the 2005 code didn't need it, but the 2008 required it, and I vaguely remember having the very same symptoms you are describing.
So it seems all the click events I was testing were image buttons, and I didn't have the images on my local machine. For some reason, when the image was missing, the click event wouldn't fire.

Resources