What is the meaning of __doPostBack function, and when is it used? - javascript

I had problem triggering server side button click events so I found a solution on the net that I should do something like
<input type="submit" name="button" id="loginButton" value="Submit"
class="button-orange" alt="Register" title="Register" runat = "server" onclick ="this.disabled=true;__doPostBack('loginButton','')"/>
I did it, and it worked, but I would like to know what is going on!

Check this article:
Understanding the JavaScript __doPostBack Function
This method is used to submit (post back) a form to the server and allows ASP.NET framework to call appropriate event handlers attached to the control that raised the post back.
You usually (in simple scenarios) don't use the method directly - it is internally used by the controls you drop on the page.
The parameters passed to this function are stored in a hidden field and picked up by ASP.NET framework on the server-side in order to find the control that raised the post back.

simply said, it is used mainly by controls with AutoPostBack property
http://www.dotnetspider.com/resources/189-AutoPostBack-What-How-works.aspx
if you want to implement autopostback for your custom control, then you need to implement IPostBackDataHandler

The solution might be working but it's not a real fix.. better way will be to find why the button events are not triggering and fix the core of the problem.
Now to answer your questions.. PostBack is the term used to describe when the form is being submitted (posted) back to the same page. Simple as that.
Ordinary submit button would have been enough, but part of PostBack is the ability to identify which control triggered it, meaning what button or link was clicked.
To do such a thing ASP.NET is automatically adding hidden fields to the form and when clicking on element that should cause PostBack, JavaScript code is used to update the values of those hidden fields to the proper values indicating what was clicked - the argument you pass.
The name Microsoft chose to give to the JS function doing the above is __doPostBack - it's just a name of a function, ordinary JavaScript function that ASP.NET automatically writes to the browser.
Hope things are bit more clear now.

Related

How to intercept button click, run analytics code, and then continue with click function

I have been asked to build code where I can intercept the click of a button, stop the function that fires on the click, run an analytics script, then fire the original click function. I can do this with JS/jQuery on a standard button when using onClick, but this button is built in Angular and using an ng-click instead, so I am a little out of my element.
The button I am trying to intercept is this:
<input id="btn-hero-form-try" class="btn btn-solid" data-ng-disabled="trialForm.buttonDisabled" data-ng-click="trialForm.submitTrialForm('personal')" type="submit" value="Download Free Trial">
A few things make this more difficult: I have no access to the HTML code on the CMS, so I can't add any additional parameters that way, and have been asked not to inject any via script from the page.
I have been playing with the ng-click-interceptor, but have not gotten it to work yet.
Any help, or pointing in the right direction would be greatly appreciated.
You are free to upgrade common directive ngClick - just create your own!
In common words - they share same event but called separately.
It will not affect default, it will be called later so you need to mess with priority, and you will be needed to append it to existing application.
If you are aware of changing existing code - then you need jQuery, selector like [ngClick] to find any clickable item and run asynchronous .on(click) events depending on target content or value of ngClick attribute.
I don't think to sync analytics with callbacks of actual clicks is good idea, due to possible side effects on any step.

onServerClick when javascript disabled

I'm trying to get an html button to call a server-side method like this:
<button onserverclick="onClick" runat="server">Submit</button>
However, this call is not being made when the browser's javascript is disabled.
I am confused because my understanding is that this call has no interaction with javascript.
The __doPostBack() method is responsible for raising a click event to the server in the background. With JavaScript disabled there's no way that's going to happen.
Here's one of the many articles that explains how this works. And a related question here which gives a glimpse into what code is generated behind the scenes.

WebForm_AutoFocus is not being generated

I have an ASP.NET page which sometimes does not generate the expected "WebForm_AutoFocus()" javascript, even though I am explicitly calling .Focus() on one of the controls. Are there any known scenarios where some factor would prevent that javascript from being generated?
Specifically, when the user clicks a particular button, I'm creating some new controls dynamically: in this case a text box. In OnPreRender I'm grabbing that newly-created control and calling .Focus() on it. The idea is, when the postback completes, the browser gives focus to the newly-created textbox control. However, the generated HTML (verified via firebug and fiddler) simply does not contain a WebForm_Autofocus call at all.
In other scernarios, the same page (on postback or on initial hit) calls .Focus() on a different control - one which is not dynamically created; in those cases the WebForm_AutoFocus() script is generated perfectly, and all is well.
Unfortunately, I'm working on a client system that LOVES frameworks upon frameworks, and abstractions upon abstractions, so I cannot post a meaningful/concise code sample here. However, if any of my friendly SO'ers knows more about generation of that "WebForm_AutoFocus" javascript - and scenarios which would prevent it from being generated - that would give me a good place to start digging.
The autofocus call only gets included if asp.net thinks there is a need for it. Set a default button on the page so that asp.net will know to make the method.

Is it possible to track all form submits in a page (using javascript for instance)?

Assuming I have several forms and a few dynamic forms created and submitted (also in a few frames)
Is it possible to track down the submit of these forms (at least in same frame) ?
I know the onsubmit event for attaching to the forms, but what if I don't know the forms and some are even created dynamically?
(running on document.forms[] won't assist here).
Is there some general onsubmit event or window.onsubmit that catches all the submits on page?
Additional questions is whether there is something similar to catch any HREF click of a user before href is being fired? (again without knowing them in advance).
Thanks,
Tal
Of course document.forms will work.
You just need to EITHER add your test to the onsubmit when they are created, or you need to scan the document.forms on a regular basis.
However you will only know they are submitted if you save the fact somewhere. If the form is submitted to the current page, you would need a cookie. If they are submitted to a new window or to a frame, then you can keep the counter in the page.
It would be great if you would tell us WHY you want to do this. Perhaps we have a suggestion to do what your end result is rather than tell you how to do what you think is what you need.

Which is better to be use JSF's Action event or Javascript?

In my application i am enabling and disabling a button according to the value selected by the user from the <h:SelectOneMenu>. I am using a valueChangeListener for the same operation.My doubt is whether it is good to use javascript or the valueChangeListener for better performance.
Rule #1: JavaScript can be disabled.
By the way, the valueChangeListener already won't be fired automatically without a little help of JavaScript. The onchange="submit()" part is JavaScript.
Depends on what you want to do once the value changes. With javascript, the only way to trigger a server side action is to submit() the form via the onchange attribute. This simply submits all information to the backing bean. The valueChangeListener on the other hand gives you more control on the server side (what element changed, what was the old value, what was the new value)

Categories