htmlCommandLink only works first time - jsf-1.2

I've already read "all" other issues concerning not working CommandLinks, but none apply for me, hopefully there's another solution.
I have an a4j:htmlCommandLink which only works the first time it is clicked.
If I click somewhere else on the page which triggers a reRender of the a4j:outputPanel, then it WILL work every time I click.
<a4j:outputPanel rendered="true" id="results">
<a4j:form id="csv-form">
<a4j:htmlCommandLink title="Export" action="#{bean.export()}" value="Export CSV" id="csv-link"/>
</a4j:form>
<a4j:form id="other-form">
<a4j:commandLink value="Test" action="#{bean.action()}" reRender="results" title="Test">
</a4j:form>
</a4j:outputPanel>
Why doesn't it work the second time I click (before I do a reRender)?
UPDATE
This works:
<a4j:form id="csv-form">
<a4j:htmlCommandLink title="Export" action="#{bean.export()}" value="Export CSV" id="csv-link"/>
<a4j:support event="onclick" reRender="results"></a4j:support>
</a4j:form>

I don't know if this is a bug in JSF 1.2, but it seems that the a4j:outputPanel is not rendered after I click the htmlCommandLink the first time.
I added an a4j:support. On the onclick event it reRenders results. I've updated the code above.
Every time the htmlCommandLink is clicked, the "results" outputPanel is reRendered. And it works. :)

Related

Strange behaviour of asp.net button on Firefox of pressing twice

If I have this asp.net button:
<asp:Button ID="Button_Save" Width="150px" OnClick="Button_Save_Click" runat="server" Text="Save" />
In order to prevent the user to press the button twice to insert two data records, I added this in the code behind:
Button_Save.Attributes.Add("onclick", "this.disabled='true';" + ClientScript.GetPostBackEventReference(Button_Save_Data, null) + ";");
It works fine with IE and Chrome. However, in Firefox, every time the user presses the button once, I got two data records inserted.
After some time googling, I modified the button this way by adding: UseSubmitBehavior="false":
<asp:Button ID="Button_Save" Width="150px" UseSubmitBehavior="false" OnClick="Button_Save_Click" runat="server" Text="Save" />
Then it also works with Firefox, only one data record inserted. But then I will have to add that setting for every button on my web application. It requires a lot of work.
However, I think this is really a big problem if that is the case always with asp.net app running on Firefox. Or did I implement something wrong?
Thanks in advance.
You would be better off adding checks to your SAVE code to prevent duplicate POSTs. I've used code like yours and still had duplicate records because someone hit the Refresh button at the wrong time.
EDIT:
Here is the code I use. Key is to match a value on the server with a value coming back in the postback.
In Page_Load:
If Not IsPostBack Then
Session("rcdupdate") = Server.UrlEncode(System.DateTime.Now.ToString)
End If
In Page_Prerender
ViewState("rcdupdate") = Session("rcdupdate")
And in the Save routine
If Session("rcdupdate").ToString = ViewState("rcdupdate").ToString Then
... write the data to the database ...
Session("rcdupdate") = Server.UrlEncode(System.DateTime.Now.ToString)
End If
You can extend asp:button and add your new functionality, then fine/replace all of your asp:buttons. Going forward you can then change all your buttons in once place.

ASP.Button - OnClientClick firing incorrectly

I have a asp.net button and I am using OnclientClick to close the window
<asp:Button ID="btnCancelSomething" runat="server" class="type-button"
Text="Cancel" OnClientClick = "Javascript:self.close()"
onclick="btnCancelSomething_Click"/>
the onclick event does nothing and I am planning to remove it. However, when I click the button the first time it is loading the page again. I click the button again then the script is fired.
How to make the window close the first time?
If the button isn't going to have any server-side functionality behind it anyway, why make it an asp:Button at all? A regular button will do the trick just fine:
<input type="button" value="Cancel" class="type-button" onclick="self.close()" />
That way it's purely client-side, since that's all the functionality that's needed anyway. It won't cause any "post-back" unnecessarily.
You could even take it a step further and break apart markup from functionality:
<input type="button" value="Cancel" class="type-button" id="cancelButton" />
Then in a script tag elsewhere, or in a separate file:
$('#cancelButton').click(function() {
self.close();
});
(Of course, this example assumes jQuery. But then what doesn't? The separation of markup and functionality if the key point here, though. How you achieve it, even how you identify the element with an id as in my example or some other way, is up to you.)

changing the PostBackUrl in ASP.net after a file download

I have a ASP.net which allows users to select a number of inputs and dropdown lists to filter the report and to download an Excel report after pressing a button on that same form.
The page can be used mulitple times, by changing the inputs and pressing the download
button each time.
The button should be posting back to the hidden IFRAME to a separate webpage, but I am
unable to change the target for the form on the fly as needed.
<form runat="server" method="post">
<asp:Button runat="server" id="DownloadToExcel" TabIndex="-1" Text="Download To Excel" UseSubmitBehavior="False" PostBackUrl="report.aspx" />
</form>
<iframe style="hide" id="output" src=""></iframe>
Additonally, when I download the file when pressing the "DownloadToExcel" button, the file downlodads as expected. However, any additional postbacks including updating the screen are posting back to report.aspx and not filter.aspx.
How do I correct the postback so it only postbacks once to download the file, and then change it back to the original page so the screen can continue to be used to filter and download a second report.
This may also eliminate the need of the IFRAME as well.
I solve the problem my adding PostBackURLs to all the submit buttons directly.
One for the Excel download that goes to report.aspx and the other for the postback on the page for filter.aspx.
It appears that if the postback is changed, in this case to report.aspx, it reuses the pathname for any subsequent postbacks, unless defined explicity, for each time you wish to postback.

HtmlButton handler fires twice when clicked (when AutoEventWireup="True")

I have an html button, see below. When it's clicked and AutoEventWireup="true", the Save_Click click handler is fired twice. When AutoEventWireup="False", it fires once.
Why is it firing twice? The button is not registered twice and no code which is adding the event handler. Using master page and no Ajax.
<button id="Save" accesskey="v" type="submit" runat="server" onserverclick="Save_Click"></button>
And now (at least in .net 4) even better:
<button runat="server">
by default behaves as it has type="submit" (fires twice on click), so for it to work correctly, we should explicitly set type="button", i.e.:
<button id="ButtonSubscribe2" runat="server" type="button" onserverclick="Save_Click"></button>
Ok I found out that that an HTMLButton fires for the onserverclick event and for the type="submit". When I removed type="submit", it fires once. This quirky behavior took me a long time to discover!
Just a guess: the handler isn't being fired twice, but you've set up a similar behavior in the Page_Load event that makes it appear to be firing twice.

Handle user hitting 'Enter' key in a ASP.NET MVC web site

I am working on a ASP.NET MVC web site that has multiple submit buttons. i.e.
<input type="submit" name="SubmitButton" value="Reset" />
<input type="submit" name="SubmitButton" value="OK" />
<input type="submit" name="SubmitButton" value="Back" />
I need to allow users to quickly submitting the form by pressing the 'Enter' key. HTML standard seems to specify that the first submit button will be assumed if a user press the 'Enter' key. However, I need to make the 2nd button (i.e. the "OK") button the default button and for reasons I don't even want to talk about, changing the button order is not an option.
I googled around and I found this post about using Page.Form.DefaultButton in ASP.NET but this doesn't work with ASP.NET MVC.
I also tried the following javascript solution, while it works in Chrome but doesn't work in IE6
$('body').keypress(function(e) {
if (e.which === 13) {
$("input[value='OK']").trigger('click');
}
});
I can think of some really extreme solutions such as going through every single controls in the form an attach the above function to them. However, I don't think that's a very neat solution so I am wondering has anyone got a better solution?
First off, this is wrong:
<input type="submit" name="SubmitButton" value="Reset" />
<input type="submit" name="SubmitButton" value="OK" />
<input type="submit" name="SubmitButton" value="Back" />
All three of them are submit buttons. A reset is an input of type="reset". Get that sorted. Second of all, I've successfully implemented something like that, and it works on IE6. Try this:
function keypressHandler(e)
{
if(e.which == 13) {
e.preventDefault(); //stops default action: submitting form
$(this).blur();
$('#SubmitButton').focus().click();//give your submit an ID
}
}
$('#myForm').keypress(keypressHandler);
The focus() part makes the button appear to be pressed when the user presses enter. Quite nifty.
Use this.
$(function(){
$('input').keydown(function(e){
if (e.keyCode == 13) {
$("input[value='OK']").focus().click();
return false;
}
});
});
You should only have one submit button. The reset button should be type="reset" and the back button should probably be type="button" like this:
<input type="reset" name="ResetButton" value="Reset" />
<input type="submit" name="SubmitButton" value="OK" />
<input type="button" name="BackButton" value="Back" />
Then, Reset and OK will just work the way they are supposed to and you'll only need to handle the Back button click with Javascript.
Edit: The other option would be to place the Reset and Back submit buttons each in their own forms inside iframes. Then they would be ignored by the main form and wouldn't be default buttons. Also, this would allow you to point them to different server actions if needed and there wouldn't be any reliance on Javascript for the button actions.
HTML standard seems to specify that
the first submit button will be
assumed if a user press the 'Enter'
key.
No, the usage of the enter key isn't defined, it's a propritary extension that's been added under various interpretations. You will get different behavoir in different browsers (and it can become very dangerous when you start mixing in different cultural or UI conventions about left to right/right to left ordering of options).
If there is only 1 button on the form then all the mainstream browsers happen to follow the same behavior - they submit the form as if that button was pressed (a buttonName=buttonValue is included with the form data). Of course this doesn't mean the buttons onclick handler is going to fire - that behavoir is browser specific.
When there are several buttons it's a complete crap shoot. Some browsers decide that the first button (and the definition of first can vary - most use the first one mentioned in the Html tree, while others attempt to use screen position) was clicked and use it in the submission, while other browsers (notably some versions of IE) make the equally correct assumption that no specific button was pressed, and so don't include a buttonName=buttonValue (the rest of the form is submitted fine).
Since you use jquery, if you use hotkeys plugin, you can make a such approach:
$(document).bind('keydown', 'return', function (evt){
$.next("input[value='OK']").trigger("click");
return false;
});
Change button order in source but not visually (ie, use CSS to swap the buttons)?

Resources