博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript调用C#后台程序执行查询
阅读量:7196 次
发布时间:2019-06-29

本文共 1274 字,大约阅读时间需要 4 分钟。

目的:在aspx页面的一个文本框失去焦点时,触发一个查询TABLE的动作。

 

1. page_load 添加 txtInternal.Attributes["onfocus"] = "MyJsFunc()"; 

2. 页面javascript添加:

<script ...> 

... 

            function MyJsFunc() 
            {
                var txt = document.getElementById("txtLotID").value;
                PageMethods.
MyMethod(txt,
OnSuccess);
                
            }
            function
OnSuccess(value) 
            {           
                document.getElementById("txtInternal").innerText=value;
                //$get('<%= Label1.ClientID %>').innerText = result=="false" ? "该用户已注册" : "OK";           

            } 

... 

 <script>

 3. html添加:在div标签里头,form外头

<div>

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager> 

<form>

</form>

</div> 

 

4.aspx.cs文件:

[System.Web.Services.WebMethod] //要在客户端使用服务器方法,必须加上这个Attribute
    public static string MyMethod(string str_lotid) //方法必须是static
    {
        LabelView labelview = new LabelView();
        labelview.LotID = str_lotid;
        HttpResponse resp = System.Web.HttpContext.Current.Response;
        
        ProcessGetLabelViewByLotID getLabelbylotid = new ProcessGetLabelViewByLotID();
        getLabelbylotid.LabelView = labelview;
        try
        {
            getLabelbylotid.Invoke();
        }
        catch (Exception ex)
        {
            string str_url;
            str_url = "ErrorPage.aspx?ErrorMessage=" + HttpUtility.UrlEncode(ex.Message);//Server.urlencode(ex.Message);
            resp.Redirect(str_url);
        }
        string str = getLabelbylotid.LabelView.PartName.ToString();
        return str;

    } 

 

备注:一定要有OnSuccess

转载地址:http://kptkm.baihongyu.com/

你可能感兴趣的文章
Provisioning Services 7.8 入门系列教程之六 手动添加设备
查看>>
技术大牛对程序员招聘的吐槽和建议
查看>>
《未来架构师》的教学范例(2)
查看>>
Exchange 混合部署—Exchange 2007到Exchange 2013迁移
查看>>
C++构造函数和析构函数的学习(一)
查看>>
Redhat更新yum源
查看>>
你知道测试大牛怎么写测试计划的吗?
查看>>
高层治理、底层做事
查看>>
【歪谈】创业和守业期间老板的“嘴脸”为何会不同?
查看>>
【VMware虚拟化解决方案】配置和部署VMware ESXi5.5
查看>>
配置虚拟交换机vSwitch
查看>>
Commons IO方便读写文件的工具类
查看>>
Java线程池 / Executor / Callable / Future
查看>>
java 可设置最大内存
查看>>
IP地址转、整数互相转换
查看>>
ScriptManager.RegisterStartupScript方法和Page.ClientScript.RegisterStartupScript() 区别
查看>>
我心中的核心组件(可插拔的AOP)~分布式文件上传组件~基于FastDFS
查看>>
jQ效果:jQuery,JS实现验证聚焦,失焦
查看>>
C++中的inline函数
查看>>
查看centos版本
查看>>