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

3

4 Answers 4

2

You may try this :

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","Validate()",true);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the fast reply, could you tell me if I add this inside my foction or in the beginning of the code (aspx.cs)?
well it's not working, the page just reloads doesn't even enter the Insert fonction. :(
add this inside the event method of your button, it should works
Unfortunately it doesn't. Or I'm just doing it wrong somehow
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.

2 Comments

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.
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..
1

You can also try:

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

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.