0

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

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
Rudra Matroja
  • 105
  • 1
  • 10
  • 1
    check http://stackoverflow.com/a/4558607/2558060 – Damith Oct 17 '15 at 07:22
  • Well I think I'll have to go through ajax since I can't change my protected method into a public static. – Rudra Matroja Oct 17 '15 at 09:10
  • Check the following urls, it will help http://www.aspsnippets.com/Articles/Call-Code-Behind-Server-Side-function-from-Client-Side-code-in-ASPNet-using-C-and-VBNet.aspx http://www.codeproject.com/Articles/180355/Calling-a-code-behind-function-from-JavaScript – Ashokreddy Oct 17 '15 at 08:06

4 Answers4

2

You may try this :

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","Validate()",true);
Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
1

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.

manas das
  • 64
  • 12
  • That was my first thought, but I it seems I can't even manage to call the public static methode from Javascript. let alone call the protected from the public static. – Rudra Matroja Oct 17 '15 at 09:45
  • Thats we can do buddy.. If you are agree to call a public static method from Javascript . because recently i have done the same implementation. BTW i didt get your comment :p, what exactly u were trying to say.. – manas das Oct 17 '15 at 11:44
1

You can also try:

ScriptManager.RegisterClientScriptBlock(this,Gettype(this),"","validate();",true);
JustLearning
  • 3,164
  • 3
  • 35
  • 52
-2

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

Rudra Matroja
  • 105
  • 1
  • 10