开发者

Run method on server without Refreshing

开发者 https://www.devze.com 2023-01-15 13:02 出处:网络
I know how to use UpdatePanels and so on but I would like to know if its possible to run method on the server (after clicking a button) which doesnt change anything in controls on the site but for exa

I know how to use UpdatePanels and so on but I would like to know if its possible to run method on the server (after clicking a button) which doesnt change anything in controls on the site but for example updates database.

I know that I can use WebService but is there any way to use simp开发者_运维问答le code behind methods ?

thanks for help


I would recommend jQuery Ajax including the .ajaxStart() .ajaxStop() to do stuff like display a panel until/while the ajax event is active.

see here for more information: http://api.jquery.com/category/ajax/

EDIT: some complete sample code:

$(document).ready(function()
{
    /***************************************/
    function testLoadTime(jdata)
    {
        $("#timeResult").text(jdata);
    };
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataFilter: function(data)//makes it work with 2.0 or 3.5 .net
       {
            var msg;
            if (typeof (JSON) !== 'undefined' &&
            typeof (JSON.parse) === 'function')
                msg = JSON.parse(data);
            else
                msg = eval('(' + data + ')');
            if (msg.hasOwnProperty('d'))
                return msg.d;
            else
                return msg;
        },
        url: "MyProcedure.asmx/GetServerTimeString",
        success: function(msg)
        {
            testLoadTime(msg);
        }
    });

});

server side:

  [WebMethod]
    public static string GetServerTimeString()
    {
        return "Current Server Time: " + DateTime.Now.ToString();
    }


u have writen all ways to do it. There no any way to do it. 1. UpdatePanel (AjaxControl) 2.with javascript(or jQuery. Best to use JQuery) call WebService


It's not possible without javascript or ajax because every method which gets hosted in a webenvironment gets only called if the request goes to the server which means postback.


Since UpdatePanels and other useful AJAX controls appeared, we, developers, forgot a little bit "How" something is working on the back. I recommend you to use a Script callback, there are plenty of examples on Internet like this: http://msdn.microsoft.com/en-us/magazine/cc163941.aspx

With this you can update database on the server function, and do nothing(return false;) when it succeeds on client success event handling function. Hope that helps,


Yes you could call a C# function to update the database from say, javascript for example

Heres a link that explains how:

http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx

0

精彩评论

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