开发者

how to call the codebehind file methods in java script in asp.net2.0?

开发者 https://www.devze.com 2023-03-15 07:20 出处:网络
i can write methods like that public void CompareEmail() { some code } public void UpdateEmail() { some code

i can write methods like that

public void CompareEmail()
{
some code
}
public void UpdateEmail()
    {
some code

}
public void InsertEmail()
    {
some code

    }

i am click the button onclientclick call the function like that

 <asp:Button ID="btnSendNow" runat="server" CssClass="invdisp_btn" OnClick="btnSendNow_Click"
                    Text="Send Now" OnClientClick="return getEmailMessage()" />

java script function is like that

<script type="text/javascript" language="javascript">
    function getEmailMessage()
     {
  var LoginID = document.getElementById('hdn').value;\\ i can pass 1 or 0
        if (LoginID != 0)
        {   
        //This place using CompareEmail() method H开发者_如何学Goow to write code comapare values not matching ask update confirm box  
        var ans;
        ans = window.confirm('DO u want to update?'); 

            if (ans == true) 
            {
//                      control.UpdateEmail();\\how to call UpdateMethod in .cs file
                alert('updated');

            }
            else {

                return false;
            }
        }
        else {
            var ans;
            ans = window.confirm('DO u want to Insert values?');
            if (ans == true) 
            {
            PageMethods.InsertEmail();\\how to call InsertMethod in .cs file
//            control.InsertEmail();
               alert('Inserted');
            }
            else 
            {
                return false;
            }
        }
    }

</script>

how to write the code in java script function call the .cs file methods pls help me

Thank u hemanth


You can achieve it by declaring your method as Web Methods in the code behind files.

Have look at this How to call a server-side method from client-side JavaScript !, it completely gives a solution to your problem.

Otherwise you can also use AJAx to do this.


You should try Pagemethods and ASP.net Ajax.


To call a server side method from client side, you have the following options:

  1. Use ICallBackEventHandler
  2. Use a Webservice with [ScriptService] attribute and call it's web methods from js
  3. Use PageMethods. But that allows you to call static methods on your aspx page, only. Hence you won't be able to access any page controls.

I would recommend option 2, since it's the easiest.

0

精彩评论

暂无评论...
验证码 换一张
取 消