Call page methods from Javascript using Atlas without UpdatePanel

This time I’ll show how to call C# methods from page javascript using Atlas library.

 
Introduction

I had task to implement adding comments to blog post in the Ajax way. Because of I used Atlas in this project I decided to use Atlas Update Panel. But I got problem – after Atlas postback Tiny text editor which I used didn’t got working. So I decided to transfer fields text in some another way… but there was one problem I still needed atlas postback to refresh datalist after comment is added. And I found simple solution.

 
 
 
Solution

Atlas has feature very similar to Ajax.NET to use C# methods from javascript. Then I decided to add obvious html button and after comment is posted I wanted to make postback of this update panel so I did it:

 

<atlas:UpdatePanel ID="UpdatePanel2" runat="server" Mode="Conditional">

            <ContentTemplate>           

 

 <button id="MyButton" onclick="MyButtonClick()" type="button">

      Add Comment</button>
           

                <asp:Button ID="btnAddComment" runat="server" Text="Add Comment" OnClick="btnAddComment_Click" CssClass="hiddenBtn" />

            </ContentTemplate>
            <Triggers>

                <atlas:ControlEventTrigger ControlID="btnAddComment" EventName="Click"></atlas:ControlEventTrigger>

            </Triggers>
        </atlas:UpdatePanel>
 
   <script>
 
 function MyButtonClick()
 {
 

    tinyMCE.triggerSave(true,true);

   var comment = tinyMCE.getContent();   

 PageMethods.SaveComment(comment, $("txtName").value, $("txtCode").value, Add_Complete, Add_Timeout, Add_Error); //method with callbacks

 }
 function Add_Complete()
 {
     if(arguments.length >0)

        $("result").innerText = arguments[0].toString();

      if(arguments[0].toString()=="Comment posted")

      {

        $("ctl00_ContentPlaceHolder1_btnAddComment").click();//do postback

      }
        $("captcha").src ="JpegImage.aspx?x="+new Date().toUTCString() ;
    
 }
function Add_Timeout()
 {
     alert("timeout");
 }
 function Add_Error(result, response)
 {
     alert("Error");
 }
C# code:
    [WebMethod()]

    public string SaveComment(string comment, string name,string code)

    {
 

        string msg = GetValidationMessage(comment, name, code);

        if (msg == "")
        {
 
            try
            {

                taComments.Insert(new Guid(postId), comment, name, BlogUtil.UserId);

                msg = "Comment posted";

            }
            catch (Exception)
            {

                msg = "Comment not posted - server error";

 
            }
 
 
        }
        return msg;
    }
 
 
Don’t forgett to add:
using System.Web.Services;
 
 
Regards Igor Kononuchenko (C) 2006

Comments

Hi Dude,

I have a similar kind of problem. I have a A TextBox which is placed inside an updatePanel and on textchange i need to get the autocomplete functionality. I have already done it using Javascipt/CSS and server code without UpdatePanel but now i need to do it using UpdatePanel in 3.5 and for which i do not have an idea of how to call the server code from the JavaScript and hence show them up in the div as autocomplete.

Can u suggest something on the same?

ASAP

Are you human

You can use simple HTML-formatting tags(like <b>, <ul>, <code> and others)

Wow free space!
Super hosting provider might be here!

Is it Google?