Asp.net codebehind from javascript?

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

<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 immediately proceeds to the update method and after the update, the javascript confirmation window exits.

? , .

+3
3
+1

, :

1- :

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

2- System.Web.UI.IPostBackEventHandler.RaisePostBackEvent, ( ).:

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

3- script, :

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

4 PostBack:

<a .... onclick="TriggerPostBack('control', 'arg')" .. /> 
0

, OnClientClick javascript?

, S Mukherjee.

0
source

All Articles