仅用aspx文件实现Ajax调用后台cs无刷新程序。(实例)
两个文件:aaa.aspx 和aaa.aspx.cs
一、aaa.aspx
- <script type="text/javascript">
- function Request() {
- alert("1");
- $.ajax({
- type: 'post',
- url: 'Test442.aspx?method=Send2', // 【调用后台的事件】
- cache: false,
- success: function (data) {
- if (data == '00000') {
- //receive();
- }
- var tbReceive = $('#tbReceive').val();
- $('#tbReceive').val(tbReceive + data + ";");
- },
- error: function (XMLHttpRequest, textStatus) {
- }
- });
- };
- </script>
二、aaa.aspx.cs
- protected void Page_Load(object sender, EventArgs e)
- {
- string result = "";
- if (!IsPostBack)
- {
- if (Request["method"] != null)
- {
- try
- {
- switch (Request.QueryString["method"])
- {
- case "Send2":
- result = Send2(); <span style="font-family: Arial, Helvetica, sans-serif;">// 【执行的事件send2】</span>
- break;
- }
- }
- catch (Exception ex)
- {
- result = ex.Message;
- LogManager.WriteLog(ex.Message);
- }
- Response.Clear();
- Response.ContentType = "text/plain";
- Response.ContentEncoding = System.Text.Encoding.UTF8;
- Response.Write(result);
- Response.End();
- }
- }
- }