Is it possible to call asp.net codebehind function from javascript in vs2008?
My problem is, I have two codebehind methods, one is for some validation and it will return true or false. This method will be called when user click submit button and if return value is true, I want to call javascript function for confirm(confirm dialog box) and if it is OK,
I will call another codebehind method for update. If validation method return false or javascript cancel, nothing changes. Now, my code is like this:

<script language="javascript" type="text/javascript">
function Confirm()
{
    var checkUpdate = confirm("Do you wish to save changes?");
    if (checkUpdate)
    {
        return true;
    }
    else 
    {
        return false;
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    if(CheckValidate())
    {
        string script = "<SCRIPT LANGUAGE='JavaScript'> ";
                    script += "Confirm()";
                    script += "</SCRIPT>";
                    Page.RegisterStartupScript("ClientScript", script);

        //If Javascript Ok Call Update,Otherewise,nothing;  
    }
}

private boolean CheckValidate()
{
    return boolean;
}

private void UpdateData()
{
    //Update;   

 }

However, it goes immediately to update method and after updating, javascript confirmation box comes out.

How i change to get right sequence? please give me the right way.

share|improve this question

50% accept rate
feedback

3 Answers

Yes by making use of PageMethods you can call the codebehind methods from javascript.

Check this link for more detail about pagemethods : http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX

share|improve this answer
if it is possible,i only want to use asp.net and javascript,not ajax. thanks. – Indi May 26 '11 at 9:47
feedback

To call a server side method on a client side event you need to do the following:

1- Create the server side method:

void DoSomething(...) { ... }

2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent which take one string argument (You can assign the name to the value of this argument).:

public void RaisePostBackEvent(string eventArgument) 
{
        DoSomething(...);
}

3- Write a script to trigger post back:

function TriggerPostBack(control, arg){
    __doPostBack(control, arg);
}

4- Call the PostBack trigger function when needed:

<a .... onclick="TriggerPostBack('control', 'arg')" .. /> 
share|improve this answer
feedback

In your case, why don't you use OnClientClick function of the button to call your javascript function?

Regards, S Mukherjee.

share|improve this answer
if i use onclientclick,javascript function will be call first and then validation and update. my sequence is (i)Validation(codebehind)(ii)Confirm(javascript)(iii)Update(codebehind). thanks. – Indi May 26 '11 at 9:36
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.