在ASP.NET客户端更新SQL数据库 [英] Update SQL Database from client side in ASP.NET

查看:155
本文介绍了在ASP.NET客户端更新SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Web开发还挺新,寻找无需刷新网页一样(如Facebook按钮)以更新客户端更新数据库SQL数据库,或在其他说明担保方式。
我搜索了很多,发现它可以通过使用Web服务,由JavaScript调用或使用JavaScript直接或AJAX来完成,但是这是最好的和安全的方式并没有任何其他的方式做到这一点?
感谢..

I am kinda new in web development, looking for secured way to update SQL Database from the client side, or in other description updating the database without refreshing the webpage like (like facebook button). I searched a lot and found that it can be done by using a web service and call it by javascript or using javascript direct or ajax, but which is the best and secured way and there is any other way to do it ? thanks..

推荐答案

您可以使用Ajax从客户端更新数据库..如果你喜欢点击网页中的按钮,通过JavaScript或jQuery的获取页面的点击事件然后通过AJAX可以执行数据库更新。请参阅下面的code:

you can use ajax for updating database from client side.. Like if you click a button in web page, get the click event of that page through JavaScript or jQuery then through ajax you can perform database update. See the code below:

有关捕获的事件(比如我的按钮id是按钮1):

For catching event(like my button id is button1):

$('#<%=button1.ClientID%>').click(function{ 
      $.ajax({
               type: "POST",
               url: "default.aspx/UpdateDatabase",
               data: "{'textboxvalue':'" + $('<%=textbox1.ClientID%>').val() + "'}'
               contentType: "application/json;charset=utf-8",
               datatype: "json",
               success: UpdateDone
             });
});

在上面code你通过从TextBox1的一个值在Default.aspx页面的功能UpdateDatabse(请定义此功能为[的WebMethod])。然后做您的更新在此功能并返回一些字符串或布尔值,以便可以判断该更新完成,然后在成功价值的函数名,如果你的函数运行成功,以便确定像

In above code you have passed one value from a textbox1 to function UpdateDatabse in Default.aspx page(Please defined this function as [WebMethod]). then do your update in this function and return some string or bool value so that you can judge that update is done then value in success is name of function that will run if your function runs successfully so define that function in JavaScript or jQuery like

    function UpdateDone(response)
    {
        if(response.d == 'done')
         { alert('Update is done');  }
        else
         { alert('Sorry Update not done'); }
    }

以上code不会做你看,你的价值在数据库中更新的任何回发。请注意,您在C#页面做的函数,请把它标记为的WebMethod,这将是一个静态方法,那么只有你的JavaScript可以找到该功能。

Above code will not do any postback you see that your value is updated in database. Please note that the function you make in C# page please mark it as WebMethod and it will be a static method, then only your JavaScript can find that function.

希望这将解决您的问题。

Hope this will resolve your problem.

这篇关于在ASP.NET客户端更新SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆