Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,

I want to execute my code behind function in javascript which is on the same aspx page.

my javascript..
XML
<script type="text/javascript">

    var xmlhttp;
    function loadXMLDoc(url, cfunc) {
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xmlhttp != null) {
            xmlhttp.onreadystatechange = cfunc;
            xmlhttp.open("GET", url, true);
            // xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            xmlhttp.send();
        }
        else {
            alert("Page can not process");
        }
    }


    function insertSealDetails()
    {
        // var username = $('#username').val();
        // var password = $('#password').val();
        var sealname = document.getElementById('<%= txtSealName.ClientID %>').value;
        var sealdesc = document.getElementById('<%= txtSealDescription.ClientID %>').value;
        var url = "";
        if ((sealname == null || sealname == ''))
        {
            alert("Please enter Sealname.");
            return;
        }

        url = "AjaxFunctions.aspx?sealname=" + sealname + "&sealdesc=" + sealdesc + "&Action=insertsealdetails";
        loadXMLDoc(url, function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var t = xmlhttp.responseText;
                t = t.replace(/^(.*\n)*.*<html/i, "<html");
                t = t.replace(/<\/html>(.*\n)*.*$/i, "</html>");
                var parser = new DOMParser();
                var dom = parser.parseFromString(t, "text/xml");
                //  x = dom.getElementById("responseText").firstChild;
                //alert(x);
                var response = dom.childNodes[0].getAttribute("name");
                if (response == "1") {
                    <%FillGDVSeal(); %>;
                }
                else if (response == "0") {
                    //alert('Invalid login !!');
                }
            }

        });
    }


    </script>

my code behind method..

 protected void FillGDVSeal()
    {
        lbMessage.Text = "Inserted";
        

    }



both javascript and code behind file are of same .aspx page..

I am not able to get any kind of output ...please help me...


thanks & regards,
krunal panchal
Posted
Updated 8-Feb-13 23:18pm
v2

1 solution

The question shows deep misunderstanding of how Web and HTTP work. Not just JavaScript cannot "call" a code-behind function, but even the question makes no sense. JavaScript is always a part of HTML which, in term, is just the output of the execution of the ASP.NET code. The code behind is executed on server side, on HTTP request from the client side. Code behind is executed and then it sends HTTP response to the client side where JavaScript can work. So, by moment JavaScript is started, code-behind code is already completed the execution. And the JavaScript is on the client side, which makes the whole concept of "call" just inapplicable.

What you can do with JavaScript, is sending another HTTP request and getting HTTP response from the server, but it is not really a call, and, generally, is not related to the code-behind code which created the currently running JavaScript. It can be done using Ajax:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^],
http://www.w3schools.com/ajax/default.asp[^],
http://api.jquery.com/category/ajax/[^] (a very nice way to use Ajax).

Now, about your JavaScipt. You are doing a really bad thing: using Microsoft ActiveX. By doing it, you totally kill the platform and browser compatibility. It is applicable only to Microsoft clients, not supported by all browser. Also, ActiveX is inherently utterly unsafe as it provides unlimited access to a local system, with is against the whole safety concept of the Web. In other words, really experienced users know that ActiveX is unsafe and won't trust your site, will simply deny using it.

—SA
 
Share this answer
 
Comments
Asim Mahmood 14-Feb-13 4:12am    
you can't call function from javascript; yes but you can fire event from javascript try that.
Sergey Alexandrovich Kryukov 14-Feb-13 11:38am    
What do you mean by "firing event"? Whatever you do, the only channel to trigger anything in HTTP server is to send HTTP request.
—SA
Asim Mahmood 14-Feb-13 12:34pm    
you can call server side button event in Javascript too
Sergey Alexandrovich Kryukov 14-Feb-13 13:04pm    
But this is always done via HTTP request...
—SA
Asim Mahmood 14-Feb-13 13:40pm    
yes ur rite but it's [possible with that you can try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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