asp.net webform 通过ajax取得json数据(二)

46 篇文章 0 订阅
 23 Nov 2013   Mudassar Khan  21 Comments  268069 Views
  ASP.Net   AJAX   jQuery

Here Mudassar Ahmed Khan has explained how to make a call to ASP.Net WebMethod using jQuery AJAX.

In this article I have explained, how to make a AJAX call to ASP.Net WebMethod using jQuery AJAX.
jQuery allows you to call Server Side ASP.net methods from client side without any PostBack. Actually it is an AJAX call to the server but it allows us to call the method or function defined server side.
 
 
Syntax
The following picture describes the syntax of the jQuery AJAX call.

 
 
HTML Markup
The following HTML Markup consists of an ASP.Net TextBox and an HTML Button.
< div >
Your Name :
< asp : TextBox  ID ="txtUserName"  runat ="server"></ asp : TextBox >
< input  id ="btnGetTime"  type ="button"  value ="Show Current Time"
     onclick  =  "ShowCurrentTime()"  />
</ div >
 
 
Client Side Script
When the Button is clicked the  ShowCurrentTime JavaScript function is executed which makes an AJAX call to the  GetCurrentTime WebMethod. The value of the TextBox is passed as parameter to the WebMethod.
< script  src ="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"  type ="text/javascript"></ script >
< script  type  =  "text/javascript">
function ShowCurrentTime() {
    $.ajax({
        type:  "POST",
        url:  "CS.aspx/GetCurrentTime",
        data:  '{name: "' + $( "#<%=txtUserName.ClientID%>")[0].value +  '" }',
        contentType:  "application/json; charset=utf-8",
        dataType:  "json",
        success: OnSuccess,
        failure:  function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</ script >
 
 
The WebMethod
The following WebMethod returns a greeting message to the user along with the current server time. An important thing to note is that the method is declared as  static(C#) and  Shared (VB.Net) and is decorated with  WebMethod attribute, this is necessary otherwise the method will not be called from client side jQuery AJAX call.
C#
[System.Web.Services. WebMethod]
public  static  string GetCurrentTime( string name)
{
     return  "Hello " + name +  Environment.NewLine +  "The Current Time is: "
        +  DateTime.Now.ToString();
}
 
VB.Net
<System.Web.Services.WebMethod()> _
Public  Shared  Function GetCurrentTime( ByVal name  As  StringAs  String
    Return  "Hello " & name & Environment.NewLine &  "The Current Time is: " & _
            DateTime.Now.ToString()
End  Function
 
 
Screenshot
Calling ASP.Net WebMethod using jQuery AJAX
 
 
Browser Compatibility

The above code has been tested in the following browsers. 

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Demo
 View Demo
 
 
Downloads
 Download

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET WebForm 是一种用于构建动态网页的技术框架,可以通过使用它提供的控件和事件模型来开发各种功能丰富的网页应用程序。要实现审批流,可以按照以下步骤进行: 1. 数据库设计:首先,需要设计一个数据库来存储审批流相关的数据。可以创建一个“审批表(Approval)”来存储审批流的信息,例如审批ID、审批标题、申请人、审批状态等。 2. 创建页面:使用ASP.NET WebForm创建一个审批页面。可以通过使用ASP.NET控件,如GridView、Repeater等来展示审批列表或者审批详情。还可以通过Label、TextBox等控件来收集用户输入的审批意见。 3. 编写代码逻辑:在页面的代码文件中,可以编写逻辑来处理审批流。可以使用ASP.NET提供的事件模型,例如按钮的Click事件,来触发审批流的处理逻辑。例如,点击“通过”按钮后,可以更新数据库中的审批状态,并发送通知邮件给下一个审批人;点击“拒绝”按钮后,可以更新审批状态,并发送通知邮件给申请人。 4. 审批流控制:审批流通常是由多个环节构成的。可以使用ASP.NET WebForm中的页面导航控件(如MultiView)来控制审批流程的流转。可以通过编写代码来判断当前审批人是谁,根据审批人的决策来显示不同的页面视图。 5. 审批历史记录:审批流一般需要记录审批的历史记录。可以在数据库中创建一个“审批历史记录表(ApprovalHistory)”,用于存储每次审批的详细信息。可以在每次审批流程处理完成后,将相关信息记录到表中。 通过以上步骤,就可以在ASP.NET WebForm中实现一个基本的审批流。当然,具体的实现方式还取决于实际需求和业务流程,并可以根据具体情况进行定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值