ASP.NET中用JS编写AJAX的编程实例
//************测试页面
<%@ Page Language="C#" MasterPageFile="~/Base/Eip.master" AutoEventWireup="true" CodeFile="text.aspx.cs" Inherits="Codinglog_text" Title="測試頁面" %>
<%@ Register Assembly="RJS.Web.WebControl.PopCalendar.Ajax.Net" Namespace="RJS.Web.WebControl"
TagPrefix="rjs" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Register Src="../Base/ContentLeft.ascx" TagName="ContentLeft" TagPrefix="uc3" %>
<%@ Register Src="../Manage/UserControls/Page_Top.ascx" TagName="Page_Top" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript" type="text/javascript" for="window" event="onload">
<!--
return window_onload()
// -->
</script>
<script language="javascript" type="text/javascript">
<!--
function window_onload() {
}
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for Firefox, Opera, IE7, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
document.getElementById('Test1').innerHTML=xmlhttp.responseText;
}
else
{
alert("Problem retrieving data:" + xmlhttp.statusText);
}
}
}
function Button3_onclick() {
loadXMLDoc("172.22.236.12/toptekeip/codinglog/ajaxrespond.ashx");
}
function Button4_onclick() {
document.getElementById('Test1').innerHTML="";
}
function Text1_onkeydown() {
var strtmp="";
//alert("只能錄入數字!");
if(event.keyCode==13)
{
strtmp= document.getElementById("Text1").value;
loadXMLDoc("172.22.236.12/toptekeip/codinglog/ajaxrespond.ashx?reqkey=1&serial=" + strtmp);
}
}
function Text1_onkeypress() {
if(event.keyCode==48)
{ event.keyCode=0;
//event.keycode.returnValue = false;
alert("砂砂在");
}
}
function Text1_onkeydown() {
}
// -->
</script>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td style="width: 20%; height: 211px;" valign="top">
<uc3:ContentLeft ID="ContentLeft1" runat="server" />
</td>
<td valign="top" style="height: 211px">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="height: 50px; ">
<uc1:Page_Top ID="Page_Top1" runat="server" />
</td>
</tr>
<tr>
<td style="height: 50px; width: 600px;">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="我的測試菜單"></asp:Label>
<asp:Panel ID="Panel2" runat="server" Height="50px" Style="border-right: #666 1px solid;
padding-right: 3px; border-top: #666 1px solid; padding-left: 3px; font-size: 12px;
background: #ffffff; visibility: hidden; padding-bottom: 3px; border-left: #666 1px solid;
line-height: 150%; padding-top: 3px; border-bottom: #666 1px solid" Width="160px">
<asp:LinkButton ID="LinkButton1" runat="server" Style="color: #000; font-size: 12px; text-decoration: none;">LinkButton</asp:LinkButton><br />
<asp:LinkButton ID="LinkButton2" runat="server" Style="color: #000; font-size: 12px; text-decoration: none;">LinkButton</asp:LinkButton>
</asp:Panel>
<cc1:HoverMenuExtender ID="HoverMenuExtender1" runat="server"
TargetControlID="Label1" PopupControlID="Panel2" PopupPosition="bottom" OffsetX="0" OffsetY="0" PopDelay="50">
</cc1:HoverMenuExtender>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtMeetType" runat="server" Width="160px"></asp:TextBox>
<asp:Panel ID="Panel1" runat="server" Height="50px" Style="border-right: #666 1px solid;
padding-right: 3px; border-top: #666 1px solid; padding-left: 3px; font-size: 12px;
background: #ffffff; visibility: hidden; padding-bottom: 3px; border-left: #666 1px solid;
line-height: 150%; padding-top: 3px; border-bottom: #666 1px solid" Width="160px">
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<asp:LinkButton ID='MeetType' runat="server" CommandArgument='aaaa'
CommandName="Select" Style="color: #000; font-size: 12px; text-decoration: none;">sdsds</asp:LinkButton><br />
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
<cc1:DropDownExtender ID="DropDownExtender1" runat="server" DropDownControlID="Panel1"
TargetControlID="txtMeetType">
</cc1:DropDownExtender>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<cc1:AnimationExtender ID="AnimationExtender1" runat="server" TargetControlID="Panel3">
</cc1:AnimationExtender>
<asp:Panel ID="Panel3" runat="server" Height="50px" Width="125px">
asdfasdfasdf</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<br />
</td>
</tr>
<tr>
<td >
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/GridSave.gif" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server"
TargetControlID="TextBox1" Format="yyyy-M-d" PopupButtonID="ImageButton1" SkinID="2">
</cc1:CalendarExtender>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel5" runat="server">
<ContentTemplate>
<cc1:DropShadowExtender ID="DropShadowExtender1" runat="server" TargetControlID="Panel4" Opacity=".5" Rounded="true" TrackPosition="true">
</cc1:DropShadowExtender>
<asp:Panel ID="Panel4" runat="server" Height="50px" Width="125px" BackColor="#FFFFC0">
dasdfasdasdfasdfasdfas</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td >
<asp:UpdatePanel ID="UpdatePanel6" runat="server">
<ContentTemplate>
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="LinkButton3" PopupControlID="Panel5" BackgroundCssClass="modalBackground" DropShadow="true" OkControlID="Button1" CancelControlID="Button2">
</cc1:ModalPopupExtender>
<asp:Panel ID="Panel5" runat="server" Height="93px" Width="232px" BackColor="White" BorderColor="#0000C0" BorderStyle="Solid" BorderWidth="1px">
fhjfgjfg<asp:Button ID="Button2" runat="server" Text="Button2" />
<asp:Button ID="Button1" runat="server" Text="Button1" />jhfgj</asp:Panel><asp:LinkButton ID="LinkButton3" runat="server">LinkButton</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td style="height: 20px; width: 572px;">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID="TextBox2" WatermarkText="請錄入姓名" WatermarkCssClass="watermarked">
</cc1:TextBoxWatermarkExtender>
</td>
</tr>
<tr>
<td>
<input id="Button3" type="button" value="button" onclick="return Button3_onclick()" />
<input id="Button4" type="button" value="button" onclick="return Button4_onclick()" />
<input id="Text1" type="text" onkeypress="return Text1_onkeypress()" onkeydown="return Text1_onkeydown()" /></td>
</tr>
<tr>
<td >
<div id="Test1" style="border:1px solid #0000ff;">
</div>
</td>
</tr>
<tr>
<td style="height: 20px; width: 572px;">
<table>
<tr>
<td style="width: 100px">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox10" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox11" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox12" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:TextBox ID="TextBox13" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox14" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox15" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox16" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox17" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:TextBox ID="TextBox18" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox19" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox20" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox21" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox22" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:TextBox ID="TextBox23" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox24" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox25" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox26" runat="server"></asp:TextBox></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox27" runat="server"></asp:TextBox></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
</asp:Content>
//***********响应文件编写
<%@ WebHandler Language="C#" class="AJAXRespond" %>
using System;
using System.Web;
using System.Web.SessionState;
using AMA.DB;
using AMA.CommonComponent;
using AMA.Pub;
using Oracle.DataAccess.Client;
using System.Data;
public class AJAXRespond : IHttpHandler ,IReadOnlySessionState
{
OracleDB dbs = new OracleDB();
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
//context.Response.Write("Hello World");
PubFuns.initSession();
string strEmp = "";
if (context.Request["reqkey"].ToString() == "1")
{
/*
strEmp += "<table><tr><td>";
strEmp += "<b>歡迎你</b>," + context.Session["EmpName"].ToString().Trim();
strEmp += "</td></tr></table>";
* */
if (context.Request["serial"] != null)
{
//string ss = dbs.GetSingleData("select serial_number from emesp.sn_wip_rec where serial_number='" + context.Request["serial"].ToString().Trim() + "'");
OracleConnection con = new OracleConnection();
con.ConnectionString = dbs.ConnectionString;
con.Open();
OracleCommand cmd = new OracleCommand("EMESC.PC_CHECK_SN", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
OracleParameter DATA = new OracleParameter("DATA", OracleDbType.NVarchar2, 4000);
DATA.Direction = ParameterDirection.Input;
DATA.Value = context.Request["serial"].ToString().Trim();
cmd.Parameters.Add(DATA);
OracleParameter RES = new OracleParameter("RES", OracleDbType.NVarchar2, 4000);
RES.Direction = ParameterDirection.Output;
cmd.Parameters.Add(RES);
cmd.ExecuteNonQuery();
string ss = cmd.Parameters["RES"].Value.ToString();
cmd.Dispose();
con.Close();
strEmp += "<table><tr><td>";
strEmp += "<b>歡迎你</b>," + ss;
strEmp += "</td></tr></table>";
}
else
{
strEmp = "返回為空!";
}
}
context.Response.Write(strEmp);
}
public bool IsReusable {
get {
return false;
}
}
}