close

感謝好文,終於要比較進入狀況了!哈雷路亞~~~

http://www.dotblogs.com.tw/hatelove/archive/2009/12/22/jqueryajax.aspx

 

<%@ WebHandler Language="C#" Class="HandingPersonInfo" %>

using System;

using System.Web;

using System.Web.SessionState;

public class HandingPersonInfo : IHttpHandler, IReadOnlySessionState

{

    public void ProcessRequest(HttpContext context)

    {

        context.Response.ContentType = "text/plain";

        string ID = context.Request.Form["ID"];

        string middelName = context.Request.Form["MiddleName"];

        string lastName = context.Session["LastName"].ToString();

        string Name = ID + " " + middelName + " " + lastName;

        context.Response.Write(Name);

    }

    public bool IsReusable

    {

        get

        {

            return false;

        }

    }

}

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title></title>

    <script src="jquery-1.3.2.min.js" type="text/javascript"></script>

    <script>

        $(function () {

            $('#txtIDbyAshx').change(function () {

                var ID = $(this).val();

                var middleName = "middle";

                $.ajax({

                    type: 'post',

                    url: "HandingPersonInfo.ashx",

                    data: 'ID=' + ID + '&MiddleName=' + middleName,

async:false

                    success: function (name) {

                        //$('#txtNamebyAshx')[0].value = name;

                        alert(name);

                    },

                    error: function () { alert('ajax failed'); }

                })

            });

        });

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

       .ashx: first name<asp:TextBox ID="txtIDbyAshx" runat="server"></asp:TextBox>

        full name<asp:TextBox ID="txtNamebyAshx" runat="server"></asp:TextBox><br />

    </div>

    </form>

</body>

</html>

 

protected void Page_Load(object sender, EventArgs e)

    {

        Session["LastName"] = "91";

    }

 

------------------------------寫成function或method---------------------------------------------------------------------------------

public void ProcessRequest(HttpContext context)
    {
        string ID = context.Request.Form["ID"];
        if (ID == "1") 
        {
            DB1(context);
        }
        if(ID=="2")
        {
            DB2(context);
        }
        
    }

    public void DB1(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string ID = context.Request.Form["ID"];
        string Name = ID + " DB2";
        //string middelName = context.Request.Form["MiddleName"];
        //string lastName = context.Session["LastName"].ToString();
        //string Name = ID + " " + middelName + " " + lastName;
        context.Response.Write(Name);
    }
    public void DB2(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string ID = context.Request.Form["ID"];
        string Name = ID + " DB2";
        context.Response.Write(Name);
    }
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 amychang2014 的頭像
    amychang2014

    工作需要筆記

    amychang2014 發表在 痞客邦 留言(0) 人氣()