Calling a Page Method In Javascript

3 posts, 1 answers
  1. Vuyiswa Maseko
    Vuyiswa Maseko avatar
    187 posts
    Member since:
    Jan 2010

    Posted 10 Aug 2010 Link to this post

    Good Day All

    I have a Method that i have defined that i will access in JavaScript(Page Method) and its defined like this


      
    [WebMethod, System.Web.Script.Services.ScriptMethod]
     public static void Getadata(String StrSearch)
     {
         View obj = new View();
         obj.Bind_SearchBox(StrSearch);
              
        // return Scriptt;
     }


    And the Bind_SearchBox() method is a non static method that is defined in this code behind of this page and View is the class name of the Page.  Now i debugged this and i see the results are passed to the method and the Method is Defined like this
     
     
    public void Bind_SearchBox(String Search)
        {
     
     
            ViewerService.ViewerService obj = new ViewerService.ViewerService();
     
            String SessionKey = obj.newSession();
     
            DateTime Date1 = Convert.ToDateTime("1980-01-01");
            DateTime Date2 = Convert.ToDateTime("2012-12-31");
     
            ViewerService.extract extract = obj.getObjects(SessionKey, Search, Date1, false, Date2, false, "", "");
            try
            {
                RadPanelBar1.Items.Clear();
                RadScheduler1.Appointments.Clear();
     
                int Len = extract.set.Length;
                for (int i = 0; i < Len; i++)
                {
                    ViewerService.vertex value = extract.set[i];
     
                    String PanelClass = value.meta;
                    PanelClass = PanelClass.Replace(Remstr, "");
                    PanelClass = PanelClass.Replace(Remstr2, "");
                    Appointment app = null;
                    if (value.atom != null)
                    {
     
                        RadPanelItem pane = RadPanelBar1.Items.FindItemByText(PanelClass);
                        if (pane == null)
                        {
                            RadPanelItem nwpane = new Telerik.Web.UI.RadPanelItem(PanelClass);
                            RadPanelItem nwpaneSpliter = new Telerik.Web.UI.RadPanelItem(PanelClass);
                            nwpaneSpliter.IsSeparator = true;
                            RadPanelBar1.Items.Add(nwpane);
                            pane = nwpane;
                        }
                        if (value.meta == "za.co.abacus.C_EVENT")
                        {
                            app = new Appointment();
                        }
     
                        int atomLen = value.atom.Length;
                        for (int j = 0; j < atomLen; j++)
                        {
                            ViewerService.atom atm = value.atom[j];
     
                            if (atm.meta.Contains("za.co.reactor.A_LABEL"))
                            {
                                RadPanelItem NewItem = new RadPanelItem(atm.content);
                                pane.Items.Add(NewItem);
                                if (app != null)
                                {
                                    app.Subject = atm.content;
                                    app.Description = atm.content;
                                    app.ID = value.key;
                                }
     
                            }
     
                            if (app != null && atm.meta.Contains("za.co.abacus.C_EVENT"))
                            {
                                app.ID = atm.content;
                            }
                            if (app != null && atm.meta.Contains("za.co.reactor.A_HORIZON"))
                            {
                                app.Start = Convert.ToDateTime(atm.content);
                            }
                            if (app != null && atm.meta.Contains("za.co.reactor.AA_HORIZON"))
                            {
                                app.End = Convert.ToDateTime(atm.content);
                            }
                            if (app != null && atm.meta.Contains("za.co.reactor.A_TEXT"))
                            {
                                app.Description = atm.content;
                            }
      
                            if (app != null && app.End > app.Start)
                            {
                                RadScheduler1.DataStartField = app.Start.ToString();
                                RadScheduler1.DataSubjectField = app.Subject.ToString();
                                RadScheduler1.DataEndField = app.ToString();
                                RadScheduler1.DataKeyField = app.ID.ToString();
                                RadScheduler1.SelectedView = SchedulerViewType.MonthView;
                                RadScheduler1.SelectedDate = app.Start;
                                RadScheduler1.Visible = true;
     
                            }
                        }
     
     
                    }
     
                }
            }
            catch (ApplicationException ex)
            {
     
            }
            finally
            {
                obj.closeSession(SessionKey);
     
            }
      
     
        }


    Now this Function works well and it has no problems , in the debug more, i check the value that is supplied to this function from the page method function its fine.  and my JS looks like this

    function keyPress() {
         var tb = document.getElementById("<%=txtsearchid%>");
         if (tb.value.length == 2) {
             PageMethods.Getadata(tb.value);
             ToggleCollapsePane();
     
         }
         return false;
     }


    and the Toggle function
     
     
    function ToggleCollapsePane() {
     
            var splitter = $find("RadSplitter1");
     
            var pane = splitter.getPaneById("LeftPane");
     
            if (!pane) return;
     
            if (pane.get_collapsed()) {
                pane.expand();
            }
            else {
                pane.collapse();
     
            }
        }

    so the Getadata() Function will call the Bind_SearchBox() Function and pass the Parameter and the function Bind_SearchBox() will do the Job as you can see at the end it sets some control to visible. But this does not bring me back results(The RadScheduler1 is not binded with data and RadPanelBar1 does not show anything)

    I had a search button that is that was calling the same method and passing the same parameters but it could show results and it looked like this
     
    protected void btnSearch_Click(object sender, EventArgs e)
        {
     
            RadToolBarItem textItem = RadToolBar1.FindItemByText("Button1");
     
            TextBox txtseach = (TextBox)textItem.FindControl("txtsearch");
     
            if (txtseach.Text != "")
            {
                Bind_SearchBox(txtseach.Text.Trim());
     
            }
     
        }


    If i click this above button it will show results. The [B]txtseach[/B]  have the same text and it calls the same method but when i call this function and inject parameters from page method it does not work.

    I realized that when i debug , when it comes to this line

    RadPanelBar1.Items.Clear();
    RadScheduler1.Appointments.Clear();

    and this one

    RadPanelItem pane = RadPanelBar1.Items.FindItemByText(PanelClass);

    the Code exists the Loops and go straight to the Finally block

    I am Surprised.

    Thanks 
  2. Answer
    Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 13 Aug 2010 Link to this post

    Hi Vuyiswa,

    Your scenario is little complex and I need to see your complete project in order to provide a working solution. This is why, could you please open a new support ticket and send me a project (please make sure that I can run it on my side) which shows your setup? I will check it and do my best to provide a working solution as soon as possible.

    Kind regards,
    Fiko
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Vuyiswa Maseko
    Vuyiswa Maseko avatar
    187 posts
    Member since:
    Jan 2010

    Posted 13 Aug 2010 Link to this post

    Good Day Admin

    Thank you for your good work you are doing here at the Forum. I have Found the Solution.When using Page-methods, the Page does not Follow the normal page life cycles, i had to use Ajax call back to achieve what i want,

    Thanks one more time.

Back to Top