简体   繁体   English

在ASP.net AJAX中使用Javascript事件

[英]Using Javascript events with ASP.net AJAX

I can't believe that this hasn't been covered before, but I have been having a hard time finding an answer to this one on Google. 我不敢相信以前没有涉及到这个问题,但是我一直很难在Google上找到答案。

I am learning AJAX with ASP.NET, and I have a working prototype. 我正在用ASP.NET学习AJAX,并且我有一个可以正常工作的原型。 My only problem is that I want to kick off my AJAX validation on a JavaScript event (onKeyUp to be exact) however, ASP.NET seems to only support its server side events for its version of AJAX. 我唯一的问题是,我想在JavaScript事件(确切地说是onKeyUp)上启动我的AJAX验证,但是,ASP.NET似乎仅支持其AJAX版本的服务器端事件。 How do I call my AJAX from a client side event? 如何从客户端事件中调用我的AJAX?

Thanks 谢谢

UPDATE UPDATE

Just wanted to add that I did find this trick out there, but this causes the website to do a postback, which makes no sense to me if your trying to do AJAX. 只是想补充一点,我确实找到了这个技巧,但这使网站进行回发,如果您尝试进行AJAX,这对我来说毫无意义。

txtFirstName.Attributes.Add("onKeyUp","__doPostBack('ctl00_ContentPlaceHolder1_txtFirstName','')");

It sounds like you're doing Web Forms stuff and you want to validate some content as its being typed or at least after they submit the data. 听起来您好像在做Web Forms之类的东西,并且想要在键入内容时或至少在他们提交数据之后验证某些内容。

You might want to take a look at the ASP.NET Ajax Toolkit. 您可能想看看ASP.NET Ajax工具包。 You can download the .dll and add it as a reference to your project. 您可以下载.dll并将其添加为对您的项目的引用。

Take a look at the ValidatorCallOut 看看ValidatorCallOut

Ok, so I "Borrowed" this code from w3Schools.com, So I want to give them credit 好的,所以我从w3Schools.com“借用了”这段代码,所以我想给他们功劳

<script type="text/javascript">
  function val() {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("spnFirstName").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", "server.aspx", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send();
    //xmlhttp.send("fname=Henry&lname=Ford"); //This is if you want to send values
  }
</script>



First Name: <asp:TextBox ID="txtFirstName" runat="server" OnTextChanged="txtFirstName_TextChanged" AutoPostBack="True" onkeyup="Val()"></asp:TextBox>
<span id="spnFirstName" runat="server" style="color:Red"> </span>

Server.aspx is an empty page that just has the word "Hello" in it. Server.aspx是一个空页面,其中仅包含单词“ Hello”。 As you can see, I tied this simple javascript function to the onKeyUp Event. 如您所见,我将此简单的javascript函数绑定到onKeyUp事件。 This also seems not to interfer with the ASP.NET AJAX validation. 这似乎也不会干扰ASP.NET AJAX验证。

I like doing this much better than adding a dll, because as long as I stick with the core asp.net product, I don't have to worry about other developers having to dig up libraries they might not have. 我喜欢这样做比添加dll好得多,因为只要坚持使用asp.net核心产品,我就不必担心其他开发人员不得不挖掘他们可能没有的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM