简体   繁体   中英

How to call a codebehind function using javascript?

I'm trying to call this code behind function :

protected void Insert(object sender, EventArgs e)
        {
            blah blah blah code
            this.BindGrid();
        }

While in the .aspx file I've got my java function which looks like this:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:Button ID="Button2" runat="server" Text="Call Button Click" Style="display:none" OnClick="Insert" />
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClientClick="return Validate();" Width="100" />

        <script type="text/javascript">
                function Validate()
                {
                    var id = document.getElementById("txtStudentID").value;
                    Number(id);
                    var fn = document.getElementById("txtFirstName").value;
                    String(fn);
                    var ln = document.getElementById("txtLastName").value;
                    String(ln);
                    var cls = document.getElementById("txtClass").value;
                    String(cls);
                    var rn = document.getElementById("txtRollNumber").value;
                    Number(rn);
                    My code blah blah blah
                    document.getElementById("Button2").click();
                    PageMethods.Insert(onSuccessMethod, onFailMethod);
                };
          </script>

I need the Insert to work when I press the button add (the one that's visible). I'm not sure if my logic is correct or if there's a better way to it. It'd be much appreciated if someone knew hot can I call the Insert fonction from my javascript. I've tried a couple of stuff from google but none of them seem to work. thanks in advance

您可以尝试以下方法:

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","Validate()",true);

We can use [WebMethod] for the method which we want to call from JavaScript. And that should be a static method as per my knowledge. And we can call that method from JavaScript by using

PageMethods.MethodNameOnCodeBehindClass()

So is it possible for your scenario to create a static method which can call your desired method.

您也可以尝试:

ScriptManager.RegisterClientScriptBlock(this,Gettype(this),"","validate();",true);

Ok It turns out to be a simple problem of Onclick and OnClientClick. OnClientClick is the function that executes first and once completed goes to the OnClick fonction. At the end of the day here's what I ended up with: Code behind :

public void Validate(object sender, EventArgs e)
{
      boring code blah blah blah
}

Here's the aspx code

    <script id="validity" type="text/javascript">

        function Validate() {

             Blah blah blah boring code
        }
    </script>
    <asp:Button ID="btnAdd" runat="server" OnClientClick="return Validate();" OnClick="Validate"
            Text="Add" Width="100" />

I use the onclientclick to execute the javascript based validation. and once completed I ececute the server side dataupload. Seems very basic once completed. I hope it helps others in future

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM