asp button and history back onclientclick - javascript

hi I have an asp button which is created using the following code:
<asp:Button ID="btnBack" OnClientClick='javascript:history.back()' runat="server" Text="back">
</asp:Button>
However, now the javascript doesn't work to go a history back. On the other hand, if I make an alert() it works...why. Why is it not working to go a history back?

Try with return false at end:
<asp:button id="btnBack" runat="server" text="Back"
OnClientClick="JavaScript: window.history.back(1); return false;"></asp:button>

Try:
parent.history.back();
Or
history.go(-1)

Had the same problem in DevExpress project. Using ASPxButton without initializing the property "AutoPostBack" will result in redirect to self page before any client-side function call.
Try AutoPostBack="false" for the button.

Related

Call javascript using asp:button

I am trying to call a javascript function stored in separate js file using below code from aspx file
<asp:Panel ID="constraintPanel" runat="server">
<div class="rightColumn">
<div class="parseButtonDiv">
<asp:Button ID="Button" runat="server" class="button" Text="Parse"
OnClick="DoParseExpression();"/>
</div>
</div>
</asp:Panel>
The on click call is not going through.
Looking for suggestions
Thanks
Use OnClientClick
<asp:Button ID="Button" runat="server" class="button" Text="Parse"
OnClick="DoParseExpression();" OnClientClick="JavaScriptMethod();"/>
Client method will invoke first then it'll go to the server.
You have to add simple change to your code as below.
<asp:Button ID="Button" runat="server" class="button" Text="Parse" OnClick="DoParseExpression()" OnClientClick="return JSMethod()"/>
You can add your js file as normal way in your html page.
After adding above change at first it call Java Script method and then trigger asp method according to js method response.
NOTE: If this doesn't work, show your js method, then I can give correct solution according to your purpose.
In your case the OnClick triggers a request to server. You need to use OnClientClick https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick(v=vs.110).aspx

C# .NET OnClientClick returning true but OnClick not firing

I have the following code:
<asp:Button ID="btnHideDep" runat="server" Text="Remove this institution" CssClass="link1 small" OnClientClick="return confirm('Confirm remove?');" OnClick="btnHideDep_Click"/>
This button does not fire btnHideDep_Click when OK is pressed in the confirm box.
More interestingly, if i have OnClientClick=return true; the method btnHideDep_Click is still not fired. Also just to confirm, without the property OnClientclick, btnHideDep_Click is fired properly.
I read a few posts about this and it seems like this should work. So I must be missing something very basic here...
Try
OnClientClick="if (!confirm('Confirm remove?')) return false;"

Javascript functions with asp.net button

Is there a way to add javascript functions to an asp.net button other than the way that is similar to:
(C#)
Button link;
link.attributes.add("onmouseover", "functioncall()");
This is the most straight-forward way, quick and dirty...
<asp:Button ID="btn" runat="server" onmouseover="functioncall();"></asp:Button>
You should also consider using unobtrusive javascript with the aid of jQuery, by adding custom attributes...
<asp:Button ID="btn" runat="server" data-whatever="value"></asp:Button>
Then hook up the function on page load using jQuery...
$(function(){
$('[data-whatever=value]').click(functioncall);
};

cancel button not redirecting to separate page

I have an ASP.NET form with a cancel button that is supposed to, after confirmation from the user, redirect them to another page. I'm using javascript for this. It works fine in a simple HTML page, but apparently something is interfering with it on the .aspx page. The popup message works perfectly, but clicking "Okay" does not take you to another page. I have tested the if statement and it is working correctly, the only thing it won't do is leave the current page.
My javascript:
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Are you sure you want to cancel? Any information you have entered will be discarded.")
if (answer) {
window.location = "index.htm";
}
}
-->
</script>
My button:
<asp:Button ID="btnCancel" runat="server" Text="Cancel" onClientClick="return confirmation();" CausesValidation="false" />
Set UseSubmitBehavior property of the button control to false:
<asp:Button UseSubmitBehavior="false" ID="btnCancel" runat="server" Text="Cancel" onClientClick="return confirmation();" CausesValidation="false" />

__doPostBack and hit function

I want my javascript to fire the postback event. I have put the javascript call in the onClientClick:
<asp:Button ID="submit_btn" runat="server"
OnClientClick="javascript:doSubmit();" Text="Update" Width="75px"
Height="26px" PostBackUrl="#" />
I am verifying some values and then submitting:
__doPostBack('submit_btn', '');
The problem is that the 'click event submit_btn_Click is not being hit. I thought this would be the case! Is this wrong?
Your <asp:Button> will always generate a post-back event, and flow into your Page_OnLoad() and other initialization events. You won't need to specifically call the JavaScript method __doPostBack().

Categories