Can anyone tell me how to debug the code while running so that to see which server side method is being called? There are Custom methods in the code which are converted to Dll and i want to find what is happening. Also the method called on Button click is not doing any validtion, but there is coupon validtion happening on button click. also there is no javascript for this, since i disabled javascript and run the code and the validations run fine.
The button declaration:
<asp:Button ID="Apply" runat="server onClick = "Apply_Click" .. >
The click event:
Apply_Click(object sender, EventArgs e)
{
GetXML();
}
This just generates the XML but calls no other method. So now i have to find which server method is causing the server validation. Please can anyone help.
I am guessing that you have Visual Studio 2010 Pro (correct me if I'm wrong). Once your page is running, go into Visual Studio, click the pause button, then the step-into button. Now, when you click anything on the page (that sends the page server-side), VStudio will immediately kick-in, already paused.
You might also want to put break points on the blocks of code that you think are running. Then you can check your stack trace to see what path was taken to get there.
There are also ways of setting break-points on variables (watch) so VStudio will break whenever that value has changed. I think that feature is only available in Pro or Ultimate.
Related
I need a very simple code in ASP.NET.
Inside any button on the page that contains a set of statement code, the user is asked to confirm the continuation.
If he agrees, it completes those orders or stops.
Please, can you help me?
if (ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Are you sure');", true) == ok)
{
Label1.Text = "ok";
// other statements
}
else
{
Label1.Text = "no";
// other statements
}
You can not do that kind of code in web land. Remember, code behind ONLY runs when the user just clicked on a button, THEN whole web page travels up to the server. Code behind runs, AND THEN whole page travels back down to client side browser. The page loads, renders and THEN even the JavaScript code STARTS to run. On the server side, the web page is un-load - blown away to bit. The web server is now waiting for ANY web page to be posted - not necessary your page!
So, code behind NEVER EVER can and will interact directly with the user. The code behind ONLY can touch, modify the web page that is up on the server for a VERY short period of time.
If you say do this:
TextBox1.Text = "Hello how are you?";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Are you sure');", true) == ok)
Or this:
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Are you sure');", true) == ok)
TextBox1.Text = "hello world";
The effect is the SAME.
What happens is this:
So, now the web page travels up to the server.
You code can modify that text box. And THEN inject the JavaScript block.
You get this:
The code behind runs. It will inject the JavaScript, runt he code to set the text box.
at this point, what does it matter if I set the text box first, or inject the JavaScript? the page is up on the web server, just sitting their. The user sees that web "wait" spinner.
Now, when you code is done, (and the user STILL DOES not see any changes yet), then the the WHOLE page is sent back to client side like this:
Then whole page re-loads, then JavaScript starts running again, and our injected block of code will NOW run!!!
So, as you can see with above, we don't really interact with the user directly - and in fact we can't!!! - the web page travels up to server - your code behind runs and has a very short little window of time to modify the page, and then the WHOLE page is send back to client, page is re-plot, re-load, and then JavaScript runs again.
So, you not able to "wait", since any code that waits will result in the web page sitting stuck up on the server side.
So in above, the order of the two commands don't matter much, does it? I can modify the text box, or inject some script - but NONE of that will run nor be seen by end user until such time the WHOLE page travels back down to the client side.
So, Any changes to the page, and even in different order in most cases does not matter. Your code is making changes to the web page BEFORE the WHOLE page will be transmitted back to the client side. So, in most cases, the order of things and controls you change on a page don't matter, since those changes can't be seen by the end user until ALL OF your code behind is done, and then the whole page starts the trip back to the client side.
So, in effect, grasping this concept of the round trip is really quite much the most fundamental concept you need to always have clear in your mind to write web code with asp.net. You really can't do web development without the above.
So, we can however add to the button, and use a confirm() like this:
our plane jane asp.net button?
It will now have two routines!!!
It will have the client side click event. This part does the dialog or prompt. If the user answers yes, then we allow the button click (server side) to run. But remember such code can only run client side - that means the WHOLE page has to be sitting on the users desktop - no code behind running at that point.
So, in the most simple format, say we have a button to delete a record.
But, a simple click on the button - rather dangerous.
So, you can add a code stub (JavaScript) to the button like this:
<asp:Button ID="cmdDelete" runat="server" Text="Server delete prompt"
OnClientClick="return confirm('Really delete this file?');" />
So, when we click this button, you get a JavaScript prompt. And if it returns "true", then the button code behind REALLY does run.
So unlike desktop code, you can have that "old way" of say a if/then block of code, and based on a user prompt, conditional run that code. (because code behind ONLY runs during that so called post-back (round trip).
so, the above will work just fine, and looks like this:
Now, of course the "main" issue is that the browser built-in dialog prompts do look rather ugly.
Using nice dialog boxes?
Of course, with the exception of "alert()" and "confirm() in JavaScript which HALT code in the browser? In most cases this is not allowed anymore - since it can lock up and freeze the browser.
And thus, now , Almost EVERY new nice looking "add-in"? Code as a general rule in JavaScript does NOT wait anymore - it runs asynchronous.
So, say we using jQuery.UI and we want to dump the VERY ugly built in browser alert() or confirm dialog?
You can do it this way:
<asp:Button ID="cmdTest" runat="server" Text="Server Delete Prompt" ClientIDMode="Static" style="display:none"
OnClientClick="return mytest(this)"/>
<br />
<div id="MyDialog" style="display:none">
<h2>my cool dialog text</h2>
<h2>This delete operation can't be undone</h2>
</div>
</div>
<script>
var mytest2ok = false
function mytest(cmdBtn) {
if (mytest2ok) {
return true
}
myDialog = $("#MyDialog")
myDialog.dialog({
title: "Delete the whole server system",
modal: true,
appendTo: "form",
autoOpen: false,
buttons: {
ok: function () {
myDialog.dialog('close')
mytest2ok = true
$(cmdBtn).click()
},
cancel: function () {
myDialog.dialog('close')
}
}
})
myDialog.dialog('open')
return false
}
</script>
In this case, we again call and setup a OnClientClick. But, jQuery.UI code as I noted does not wait. So we click on button, our dialog displays and the JavaScript code runs through and finished!!! - that's why we added the ok flag.
So, the dialog is displayed. User hits ok, and then we run the "ok" code stub, and it sets our flag, and clicks the button again!!! - this time the client side button code returns true and the server side button click will run. This code looks like this, and allows you a nice looking dialog, and in effect the SAME reuslts - a pop dialog to conditional run or not the code based on user input.
jQuery.UI lets you use the content of a "div" for anything you want in the dialog.
So, we now have this:
So, this is a web based dialog box - it even makes the screen go "gray-darker" for you. And it is model. But, the code is not frozen, and when you click ok, then the "ok" stub runs, and clicks the server side button for you again.
So, in most cases, you have to:
Pop the dialog in the client browser, and THEN choose to click or run the server side button code if you want.
Could you do this 100% server side? well, you probably could if you used TWO buttons, and hide the 2nd button. So, you click on first button. Server side code runs and injects the JavaScript to pop dialog, and then based on yes/no, then the 2nd button will be clicked on and your server side code stub will run. But NOTE VERY close, this would suggest and mean you again NEVER are waiting in the middle of your code stub server side, but going to pop a dialog, and then based on user answer, another button code will run server side.
As noted, it thus an advantage to put the dialog box in FRONT of the server side button by using client side code to determine the yes/no choice.
So, based on user input, you either click (run) the button server code code, or you do not. But there not a practical means to "halt" the server side code in the middle of a code stub to wait for a prompt, since referring to above diagram, you can see the web page when up on the server does not have any user interaction, but your code can only modify the web page BEFORE it makes the trip back down client side.
So, almost always, we need a client side code bit, and then we choose yes/no to run that button.
So, code behind NEVER interacts with the user, but can ONLY ever wait for a WHOLE web page to be sent up to the server (for code behind to run).
I want to create a dialog confirm in ASP.NET.
I'm doing it with this code:
protected void Button1_Click(object sender, EventArgs e)
{
ClientScriptManager CSM = Page.ClientScript;
string strconfirm = "<script>if(!window.confirm('Are you sure?')){window.location.href='Default.aspx'}</script>";
CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false);
if (the condition to yes clicked)
{
// do something ....
}
else
{
// do something else ....
}
}
How can I get the yes / no condition in this case?
Thanks for reading my post
You can't do it this way. Your code behind NEVER interacts with the user. Your code behind ONLY interacts with the web page, and ONLY does so WHILE the page is making the round trip.
Before a post back? You have this setup:
You do NOT have this:
And you do not have this either:
So, when a user clicks a button - this occurs CLIENT side. The page - whole web page is sent up to the server - and THEN your code interacts with the web page - NOT the user!!!
so, you click on a button - (browser). The page THEN starts the all important round trip. Like this:
the page travels up to server,
You now have this:
Now and THEN your code behind runs. When done, your code behind MUST finish, else the page stays stuck up on the server side. When done, the whole page page makes the trip down back to the browser. And THEN the page is re-displayed and THEN any js code can run.
So when done? Your page makes the trip back down to the browser like this:
And only AFTER the page makes that trip, can the browser display what your code behind changed. So your code behind NEVER interacts with the user, but in fact can ONLY interact with the web page and ONLY do so for the short time that the web page is up on the server.
So, how can you pop a dialog and get a yes/no answer? Well, you require the dialog to run and launched client side. And based on the user hitting ok, or cancel, you can THEN have a code stub run.
So you can say have code that flows like this:
ClientScriptManager CSM = Page.ClientScript;
string strconfirm = "<script>if(!window.confirm('Are you sure?')){window.location.href='Default.aspx'}</script>";
CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false);
Function OkCode - runs on user click or seleciton.
Function Cancel Code - runs on user click of cancel button.
So, you need 3 code stubs. One to inject the script that will pop the dialog. And then the ok or cancel button in that script/dialog can THEN launch the server side code again. This will of course require that round trip (as all server side button event code does).
There are a few short cuts. Say I have a standard asp.net button, and I want a confirm dialog? Well, you can do this:
<asp:Button ID="Button1" runat="server" Text="Delete"
OnClientClick="return confirm('Delete record');"/>
The above works, since the confrim in js returns true or false and that will prevent the button code (code behind server side) from running.
You could of course have a script that you call from code behind. Keep in mind that your register script quite much has to be the LAST thing you do and inject into that page before it travels back down to the client. The page dispays and THEN your script you added THEN runs!
You could say then have two buttons on your form - hide them. And then have a js dialog that clicks either button based on what you pass.
So, say we build that function. We will pass it the question/text, and then say two buttons (one for ok click, and one for cancel click). And you could set the style of those buttons as display:none. This will still not get you a server side if/then result from that dialog. So, you have to quite much in near ALL cases break your code out to 3 seperate parts.
The part that triggers the dialog.
And the two buttons that run code behind stubs based on the choice(s) the user makes. In other words, adding the dialog by script would STILL require you to have TWO separate code stubs behind to run (the one for ok, and the one for cancel). You simply can't pop a dialog server side and wait. That would result in the web page being stuck up on the server in the above round trip concept. That round trip dialog MUST be absorbed into your mind - without that model of how the round trip works, then you make mistakes in your design assumptions - and thus your question which is actually not really possible to achieve.
You could create a generalized dialog that you set the text in code behind along with your register script idea. but the button choices and code it runs would also have to be setup in that register script, and you would still require two separate code behind stubs (one for cancel, and one for ok). So it would be possible to setup something that requires you to NOT have special code in each page, but you would require a js function routine you build up, and you would have to supply it the name of the two code stubs, and then that js routine could send a __DoPostBack passing the choice made.
But even again, note how you need separate routines, since that web page is sitting on the users desktop, and your code behind is not waiting for a dialog, but in fact a WHOLE PAGE post back.
So just keep mind that code behind does not interact with the user - it ONLY interacts with the web page, and ONLY does so during that short round trip and in fact ONLY during the time that the web page is up on the server, and your code behind runs and then is finished. And THEN the web page starts that trip back to the client side - the page is re-loaded, changes you made are displayed, and THEN the js in that page starts to run again.
I'm using VS2010 and I'm going through some examples in my book and I'm trying to find the preInit method. I seem to recall in VS2008 for VB.Net all the Page LifeCycle methods were in the upper right hand side drop down for the "Page" element (upper left hand side).
Like I said I'm using VS2010 and in C#. In my upper left drop down all I see is _Default. The only PLC event I see is Page_Load. Do I have to manually type out the preInit method or is there a way to get it to auto populate like I've seen in VB using 2008?
I followed Kirill answer and it did everything he said it would. However, this is what it produced (Default.aspx is the name of the page I'm using):
private void _Default_PreInit(object sender, EventArgs e)
{
...
}
When I put a break point in there, it never got into that code block and the code was never run...
However, when I manually wrote it out using the following:
protected void Page_PreInit(object sender,EventsArgs)
{
...
}
The break point and the code in that block worked!
Any ideas?
Here is the answer.
If short:
In the solution explorer, right click the page and select "View
component designer" from the context
menu
open the properties panel/window (press F4)
now click the yellow arrow/flash icon and you will see a list of all
page events
double-click the event for which you want to add a handler
UPDATE
Yes you're right, it's not working as expected. Sorry for posting the answer without really checking if it works. I do not know how to fix it, but I've at least found the explanation.
Besides the method you've mentioned it also adds something like
private void InitializeComponent()
{
this.PreInit += new System.EventHandler(this._Default_PreInit);
}
It comes from the old designing/compilation model. After MS changed default designing/compilation model in ASP.NET 2.0., by default
AutoEventWireup is set to true, which instructs compiler to automatically
attach event handlers from the code behind/beside file using naming
convention (for the page events) e.g.
protected void Page_Load(...)
{
}
is automatically attached behind the scenes:
this.Load += new EventHandler(this.Page_Load)
So obviously the corresponding behavior of the Visual Studio 2010 was not changed according to these changes, and it still creates event handlers following the old naming convention and adding the delegate attaching method, which is no more necessary to have.
I found then answer to my question. I'm studying for the MCTS 70-515 exam and on Chapter 3 (pg.111) in the "Understanding the ASP.Net Life Cycle and Handling Events lesson I read the following paragraph:
Adding a C# Event Handler
"The code editor for C# also has an event handler drop-down list. However, it only contains access to events you have already provided handlers for. The C# environment provides a tool for wiring up control events, but it does not provide such a tool for the Page events. For these you have to code the event handler manually."
However, in a VB.Net web side, you get what I was originally describing with the 2 drop downs.
I have a ASP.NET 3.5 web site with an AJAX update panel. I simply need to process some server side code and then issue a user prompt that says "Code processing complete".
I know there is supposed to be support for Msgbox-esque methods in ASP.NET but I can't find them and any other JavaScript based solutions don't work effectively when you have an update panel.
Help.
Couldn't find a direct example for this, so you can see how this is being used, and change it for your needs. On the client, there is a get_isInAsyncPostback() method to check if an updatepanel will be performing an async postback.
This link shows you how to cancel an update: http://www.asp.net/ajax/documentation/live/Tutorials/CancelAsyncPostback.aspx
Using the themes in this, instead of the beginRequest, you can tap into the endRequest event, and if an async postback, you can post an alert here. This assumes that the code works successfully, which are you adding that detection?
HTH.
MsgBox doesn't exist, but look at the javascript alert() function. That'll popup the message for you.
here is a link with more information on javascript popups
If you want to inject javascript from the server-side code, you can use this:
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Code processing complete.');",True)
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.