在Server端动态注册Javascript

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ClientCallbacks.aspx.cs" Inherits="WebApplication5.Register.ClientCallbacks" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>未命名页面</title>
    <script type="text/javascript">
      function DoSearch()
      {
        var txtName = document.getElementById("txtUserName");
        CallServer(txtName.value,"");
      }

      function ReceiveServerData(txtUserInfo)
      {
        Results.innerText = txtUserInfo;
      }

      setInterval('DoSearch()',1000);

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         姓名:<input id="txtUserName" type="text" /><br />
         <span id="Results" style="background-color:Orange;width:500px;"></span>
    </div>
    </form>
</body>
</html>

  

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


namespace WebApplication5.Register
{
    public partial class ClientCallbacks : System.Web.UI.Page,ICallbackEventHandler
    {
        //用户基本信息
        protected string txtUserInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            //动态注册JavaScript
            String cbReference = Page.ClientScript.GetCallbackEventReference(this,
                "arg", "ReceiveServerData","context");
            String callbackScript;
            callbackScript = "function CallServer(arg, context)"+"{"+
                cbReference + "}" ;
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                "CallServer",callbackScript,true);
        }

        //引发Callback 事件处理
        public void RaiseCallbackEvent(string txtName)
        {
            if (txtName != null)
            {
                //string ConnectionString = ConfigurationManager.AppSettings["SQLConnectionString"];
                //SqlConnection conn = new SqlConnection(ConnectionString);
                SqlConnection conn = new SqlConnection(@"data source=.\\SQLEXPRESS;
                                         initial catalog=ERP;user id=sa;password=123");
                conn.Open();

                SqlCommand cmd = new SqlCommand("select * from ERPUser where userName=@userName");
                cmd.Connection = conn;
                cmd.Parameters.Add("@userName", System.Data.SqlDbType.NVarChar, 10).Value = txtName;
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    txtUserInfo = "员工代号:" + dr["ID"] + "   ,";
                    txtUserInfo += "姓名:" + dr["UserName"] + "   ,";
                    txtUserInfo += "密码:" + dr["UserPwd"];
                    txtUserInfo += "服务器查询时间:" + DateTime.Now.ToLongTimeString();
                }
                else
                {
                    if (String.IsNullOrEmpty(txtName))
                    {
                        txtUserInfo = "请输入姓名";
                    }
                    else
                    {
                        txtUserInfo = "查无此人!";
                    }
                }

                cmd.Dispose();
                dr.Dispose();
                conn.Dispose();
            }
        }


        //回传 Callback 结果
        public string GetCallbackResult()
        {
            return txtUserInfo;
        }
    }
}

  

posted @ 2012-12-18 09:42  FiberHomer  阅读(129)  评论(0编辑  收藏  举报