Call javascript using asp:button - javascript

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

Related

Excluding ContentPlaceHolder from generated ID tags

I have just implemented master page to control all my sub pages from a old project.
I had to add <asp:content id="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> to all my sub pages in order for them to be rendered correctly.
Some particular functionalities of my page include javascript calls from a Button with commands in an example below:
<asp:button id="cmdReturn" runat="server" causesvalidation="False" cssclass="fontSize90" meta:resourcekey="cmdReturnResource1" text="Cancel" width="152px" />
With the new ContentPlaceHolder, the javascript generated will not be compatible with my functions since it tacks on the ContentPlaceHolder1 infront of all my JS calls like the example below
<input type="submit" name="ctl00$ContentPlaceHolder1$cmdReturn" value="Cancel" onclick="return confirm_notsaved('eng');" id="ContentPlaceHolder1_cmdReturn" class="fontSize90" style="width:152px;" />
Notice the name="ctl00$ContentPlaceHolder1$cmdReturn". I want to exclude the ContentPlaceHolder section of the name so, when generated, it turns into:
name="cmdReturn"
Is this possible after I have added the masterpage? I don't think I have access to the class/script that generates the HTML in the end result.
What could I do in this case to solve this problem?
Add ClientIDMode="Static" to your <asp:Button... tag.

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);
};

OnClientClick event in In ASP.NET

I'm using ASP.NET to pass a value to a JavaScript function and, for some reason I haven't been able to determine, it isn't working when I try to pass in a value from another control. Instead, it acts like there is a syntax error and it just submits back to the main form.
Does anyone know why?
Example:
<asp:TextBox ID="txtToSay" runat="server" Text="Something"></asp:TextBox>
<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething(<%=txtToSay.Text%>);" /> <!-- doesn't work -->
<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething('<%=txtToSay.Text%>');" /> <!-- doesn't work -->
<asp:Button runat="server" ID="btnSaySomething2" Text="Say Something"
OnClientClick="saySomething('Something');" /> <!-- works -->
<script type="text/javascript">
function saySomething(txt){
alert(txt);
};
</script>
Additional Information:
Web Application running on .NET 4.0
Language: C#
Update:
After working with this a while, I've determined that you can't use <%%> tags in ASP controls. Additionally, if you're looking for dynamic evaluation of control values AVOID AVOID AVOID using <%=someControl.Text%> or similar constructs since they are only evaluated once a request is submitted to the server. If you need a static value from another control at runtime, simply set that value in the page load event or handle it another way in the code behind.
Javacript will search for variable name = txtToSay.Text in saySomething function call, Put quotes around it to make it string value
Change
OnClientClick="saySomething(<%=txtToSay.Text%>);"
To
OnClientClick="saySomething('<%=txtToSay.Text%>');"
You can get the txtToSay.Text without passing it this way
<script type="text/javascript">
function saySomething(txt){
alert(document.getElementById('<%=txtToSay.Text%>').value);
};
</script>
you need to put ' around your text in the saySomething() call.
Like this:
<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something" OnClientClick="saySomething('<%=txtToSay.Text%>');" />
UPDATE
<%= %> won't work inside an asp.net control. Can you set it from the code-behind?
I.E
btnSaySomething1.OnClientClick = "Text to say"

Finding an asp:button and asp:textbox in Javascript

What I'm trying to do is get an asp:button to click. The only problem is that it is within a few tags.
Example:
<loginview>
<asp:login1>
<logintemplate>
//asp:textbox and asp:button are located here.
</loginview>
</asp:login>
</logintemplate>
So how would I get javascript to point to that location so that I can manipulate it. For example, get the button to click.
First, you need to figure out which template is being used, since you can only access the active one. (Anonymous or LoggedIn). Once you do that, use the FindControl method on the LoginView to find the ClientID of the element you need to reference.
For example:
<form runat="server">
<asp:LoginView runat="server" ID="LoginView">
<AnonymousTemplate>
<asp:Button ID="ASPButton" Text="Button" runat="server" />
</AnonymousTemplate>
</asp:LoginView>
</form>
<script type="text/javascript">
var el = document.getElementById('<%= LoginView.FindControl("ASPButton").ClientID %>');
</script>
Check out the jQuery framework: you can find controls by ID, and then call methods/properties on those controls.
http://jquery.com/

asp button and history back onclientclick

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.

Categories