Click here to Skip to main content
15,893,190 members

How to Call JavaScript before ServerSide Click Event in ASP.Net C Sharp

amit Baswa asked:

Open original thread
Please Take a look to following code.

Default.Aspx
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function call() {
            alert("Call Client");
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="btnclick" name="btnclick" type="button" runat="server" onserverclick="btnclick_ServerClick"  value="Input Button"/><br />
        <asp:Button ID="btnClickAsp" runat="server" Text="ASP Button" OnClick="btnclick_ServerClick"/>
    </div>
    </form>
</body>
</html>


Default.Aspx.cs
C#
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnclick.Attributes.Add("onserverclick", "return call()");
        btnClickAsp.Attributes.Add("onclick", "return call()");

    }
    protected void btnclick_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("default2.aspx",false);
    }

}


Now My problem is that on Click of 'Asp Button' an Alert Box of JavaScript is shown and on click 'Ok' the server side method 'btnclick_ServerClick' is called and redirect to 'Default2.aspx' Page.
BUT
This does not happen same thing for '<input /> Button'. It don't even call JavaScript function nor get call to server side. But I need both to be called. please suggest me right code. Am I going wrong somewhere?
Tags: C#, Javascript, ASP.NET, Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900