views:

1194

answers:

5

When I view source on my ASP.NET page I get the following snippet:

<script type="text/javascript"> 
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script> 

<script src="/WebResource.axd?d=5lheyan7fritNTjDRpG8vA2&amp;t=633734967316503584" type="text/javascript"></script>

the .aspx file looks kinda like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NaturalDateDemo._Default" %>

<form id="form1" runat="server" enableviewstate="False">
   Enter something to parse:<br />
   <asp:TextBox ID="TextBox1" runat="server" Width="270px"></asp:TextBox>
   <asp:Button ID="Button1" runat="server" Text="Parse" PostBackUrl="Default.aspx" CausesValidation="False" EnableViewState="False" UseSubmitBehavior="true" />
</form>

Both of the chunks of code (the literal code and the stuff in /WebResource.axd) seem to be related to doing JavaScript based post-backs and I can't think of any reason that my page would require JavaScript to do the post-backs.

  • What is this doing?
  • Where is it coming from?
  • Can I get rid of it?
  • How can I get rid of it?

The page this question is coming from


A little Google work found this indicating that validation controls would cause this, but I don't think I have any validation controls.

Looking at the WebResource.axd file in detail shows a lot of what looks to be boiler plate function that I can't find any references to from anywhere.


After further investigation it seem that my submit button is not be doing what I thought it was because it has some JavaScript in its onclick handler. However using FireBug I can see that the page is doing a full refresh (the HTTP response has the full page text) so I have no clue what that's about. OTOH I'm now seeing an __EVENTVALIDATION argument that shouldn't be there (because there is nothing to validate) so that might be interesting to look up.


It seems based on the answerer below that what I'm running into is that the default (only?) way for ASP.NET to do a post-back is via JavaScript even when a basic HTML Form would do just fine. (Personally I think this is a stupid design choice on MS part; ASP.NET shouldn't introduce the JavaScript dependency until you ask it to do something that can't be done without it.)

Based on that opinion, several people have speculated that I don't know what I'm talking about. (I will grant that I confused the issue by miss using the term "post-back", thinking it only implies JS based stuff.) While I don't know the implementation details of ASP.NET, I do known how the general HTTP POST stuff works and my opinion is based on considering how I would implement the solution my self and not just based on how I would like it to work.

+2  A: 

4 things to check:

  1. Are you using an UpdatePanel somewhere on the page?
  2. Do you have any javascript making calls to web methods in your code behind?
  3. Does your master page have anything in it that might be adding to your output?
  4. Are you referencing any controls from an external library?

Once you've investigated that, you might be able to at least cut out some options as to the source of the problem.

Can you provide any sample of the code on your ASPX?

EDIT: Your post now includes a link to the source page. The page has a postback button on the form, which renders results of parsing without doing a full postback (notice that the whole screen doesn't reload). The webresource URL in your path contains the javascript functions used to accomplish this. Your code is doing a partial postback, which is why you have postback-related code on your page.

EDIT #2: To completely remove postbacks from the system, you will need to remove the .NET form submission currently being used by your application. Your button is posting back, therefore it needs code to perform this postback to your server. If you want to remove this, you need to do things with old-school HTML posting straight to another URL. You will need a standard POST form with an action URL and form input fields. However, at that point, you are sort of defeating the purpose of using .NET and its postback support. I guess the final question is why do you need to remove this javascript?

Jay S
1) the text `UpdatePanel` doen't exist in the solution. 2) I have no Web Methods at all. 3) Like what? 4) it's all standard ASP.NET aside from a custom control I built based on a standard ASP.NET control
BCS
Re: 3) Could be a navigation button for a menu, or a popup calendar control from a library. Anything that you click on that supports codebehind event handlers could cause this, including using a standard .NET LinkButton
Jay S
Also, does your code reference any compiled DLL libraries in your BIN folder?
Jay S
It does reference another project, but that project is not even aware that it is in a ASP.NET page.
BCS
Can you provide the full generated HTML? Does the link say "__doPostback" when you hover over it? Are there references to __doPostback anywhere in the rendered source (other than the method itself)?
Jay S
Additionally, do you have any references to: assembly: WebResource in your AssemblyInfo files? These may be loading external resources.
Jay S
I have gone to the link you provided, and you clearly have postbacks occurring with your form. I have edited my answer to reflect this.
Jay S
The rendered HTML is here: http://smplsite.com/NaturalDate/Default.aspxI'm finding that the submit button has an `onclick` handler that reference this stuff...
BCS
How do I get rid of that? I want a full reload if that can get rid of the JS.
BCS
I have updated my answer. Essentially, if you don't want postback code, you can't use postbacks.
Jay S
I need to junk the JS because I have seen the page not work with JS turned off. As to having to redo the page to lose this; if that really is the cases, than MS screwed up *big time* (and I find it hard to believe that they are that stupid) as there is nothing here that really needs post-backs. It should be trivial for ASP.NET to render what I have right now as an old school HTML forms based page by just not outputting the JS and `onclick`.
BCS
MS did not screw up big time, this is what a postback is. It's when you push a button and ask the server to do something and refresh the screen with the information. You are posting a question back to the server (hence: Post-back). It then provides you with the results. There are multiple ways of implementing things, but you've chosen to do this in such a way that is explicitly doing postbacks.
Jay S
If you are calling non-JS, HTML forms based submit is a type of post-back then what I said may be wrong. However I was only referring to JS based post-backs; if the default ASP.NET stuff *only* works via JS based post-backs, then I'll have to stand by my claim that they screwed up because, as you point out, that's only one of several way to do it and not the most robust one.
BCS
This is plainly wrong, see my answer. ASP.NET does not require this script, it was actually (implicitly) asked to provide this script. Just look at the `PostBackUrl` on the button. **That's** the cause.
Ruben
The mystery is solved: see Ruben's answer.
BCS
I stand corrected. Without seeing the ASPX markup, I made an invalid assumption about your postback button. Tip of the hat to Ruben.
Jay S
+1  A: 

This is how .NET postbacks work, and why a lot of people are moving the the .NET MVC model. In order for the postbacks to work correctly, .NET actually relies on javascript to do a lot of the dirty work. The only way you can really get rid of it is to go with the MVC model.

Jason
**Your kidding me?** I wan't aware that MS was *that stupid*. If that is true, then MS switched from a simple solution that works everywhere to a complex solution that doesn't and got exactly ZERO increase in what the system can do.
BCS
This is not "MS being stupid". This is the technology you've chosen to use. You obviously haven't spent the time to discover what .NET is, or why it uses postbacks, or what a postback is.
Jay S
actually it works very well. with a minimal amount of injection you get a ton of extra functionality. this is also why .NET mangles your control ids. it has a convention it uses to find the elements on the page.
Jason
@Jason: It may get you a ton of extra functionality when you shoos to use it. I on the other hand have no reason to use it so it gets me nothing. What dose it buy me? I have no validation logic. I'm doing no server side processing *except* for the submit and forms will handle that just fine.
BCS
if you don't like it, don't use it! go MVC. you won't hurt my feelings.
Jason
@Jason: the "it" I was talking about is the functionanlity that the JS give, it was *not* ASP.NET
BCS
This is plainly **wrong**, see my answer. ASP.NET does not require this script, it was actually (implicitly) *asked* to provide this script.
Ruben
ha, well dude finally posted his HTML... if he would have posted that in the first place i could have told him off the bat to get rid of it. you don't have to specify that when creating a button.
Jason
I think both the link and the code went up quite early.
BCS
A: 

From looking at your page, I don't understand how you "can't think of any reason that [your] page would require post-backs." It's quite clear to me that when clicking on your Parse button you are calling some methods on the server to interpret the passed in value and convert it to a date, then returning that date to the browser so that the user can see it.

Am I right in assuming that you did not originally create this page and have little experience with ASP.Net? It's not that "MS screwed up big time" (as you mentioned in your comment on Jay S's very well done answer that probably deserves a check mark) it's that your requirements seem to have changed and now you need to ensure that the page works when JavaScript is disabled, which is possible to do with ASP.Net, it's just not the natural way for ASP.Net to do things. However, this will likely require a pretty hefty overhaul of the page, as most drastic changes to requirements do.

Phairoh
`s/post-back/JS based post-back/` It seems that my understanding of what people refer to as "post-backs" was a bit too narrow. -- BTW I wrote the whole page and the backing library. It *is* my first ASP page thought. -- Where I think that MS screwer up is exactly that the *minor* change to a simple data submit page of now needing to work without JavaScript "requires a pretty hefty overhaul". (and if they don't consider that a minor change, that's another problem)
BCS
But it's not a minor change because that is the way you chose to implement it. I agree with Jay S and think that you should take a bit more time to understand the technology. There are many options you can choose to use and it is very powerful once you learn how to do things in the ASP.Net world.
Phairoh
@Phairoh: I chose to build the page the way MS seems to recommend. I still think that it would be a mistake for MS to recommend a solution that *can't not* use JS even when it's not giving the user anything. And to a lesser extent it would be a mistake for them to even build such a solution.
BCS
+8  A: 

The answer is a lot simpler than any I've seen here: just remove the PostBackUrl from the Button and all JavaScript disappears like magic.

Without JavaScript, an HTML button cannot submit to another page than the page specified in <form action="...">. However, ASP.NET does provide a possibility to do this out-of-the-box if you specify a PostBackUrl on a Button. That's what the JavaScript is about: supporting a non-default post-back.

In this specific case your PostBackUrl is the same as the <form>'s action, but apparently ASP.NET does not special-case this and any non-empty string triggers this wrapper script.

Ruben
I guess I was right; MS wasn't stupid.
BCS
Ah! BCS, you finally posted your aspx code! All makes sense with the world! Happy you got your answer :)
Phairoh
"[you] guess [you] were right"? you mean after calling MS stupid like 23850235 times in your comments on the answers to your question?
Jason
The answer was there all along. If you followed his 'The page this question is coming from' link (before he made changes) you could see the '_PREVIOUSPAGE' hidden field. I also made an incorrect assumption, in that i thought his intent WAS to make a cross page post. Which when using the asp .net button control 'PostBackUrl' adding of the javascript Web Resource file is unavoidable. Once the aspx code was posted it became clear that his intent was NOT a cross page post as the value of this property was the same as the calling page.
Phaedrus
@Jason: What I said was: **if** it was not possible to do what I was asking for, then MS did something stupid. As Ruben has shown, I *can* do what I was asking for, so the conclusion doesn't hold. (And I started out with things like "I don't think MS is that stupid" and at least that I got correct)
BCS
A: 

If you are using a control that inherits another class, the type passed must be the type of the parent, not the base class

rodolfo