简体   繁体   English

将Javascript与ASP.Net控件一起使用

[英]Using Javascript With ASP.Net controls

Is there a best practice when it comes to setting client side "onclick" events when using ASP.Net controls? 使用ASP.Net控件设置客户端“ onclick”事件时是否有最佳实践? Simply adding the onclick attribute results in a Visual Studio warning that onclick is not a valid attribute of that control. 简单地添加onclick属性会导致Visual Studio警告onclick不是该控件的有效属性。 Adding it during the Page_Load event through codebehind works, but is less clear than I'd like. 通过codebehind可以在Page_Load事件期间添加它,但是不如我所愿。

Are these the only two choices? 这是仅有的两个选择吗? Is there a right way to do this that I'm missing? 是否有正确的方法来做我所缺少的?

Thanks! 谢谢! Eric Sipple 埃里克·西普尔

** just a pre-note on the answer: HTML validation in VS is often BS. ** 只是答案的前言:VS中的HTML验证通常是BS。 It complains about stuff that works IRL, even if that stuff is bad practice. 它抱怨适用于IRL的东西,即使这些东西是不好的做法。 But sometimes you gotta bend the rules to get stuff done. 但是有时您必须改变规则以完成工作。

Every ASP.NET page (2.0 and greater) comes with a ClientScriptManager . 每个ASP.NET页(2.0及更高版本)都带有一个ClientScriptManager You use this to register javascript from server side code. 您可以使用它从服务器端代码注册javascript。 You can pass in javascript that registers events on controls after the page has loaded, when the HTML controls on the page are all present in the DOM. 当页面上的HTML控件全部出现在DOM中时,您可以传入在页面加载后在控件上注册事件的JavaScript。

It presents a single, unified place on pages to dynamically add javascript, which isn't a bad thing. 它在页面上显示了一个统一的位置,可以动态添加javascript,这不是一件坏事。 It is, however, awfully ugly. 但是,它非常丑陋。

In this time of the death of the ASP.NET server control model, you might want to set events the way they will be in the future of ASP.NET MVC. 在ASP.NET服务器控件模型消失的这段时间里,您可能想要以与ASP.NET MVC将来相同的方式设置事件。 Grab a copy of jQuery and add it to your website. 获取jQuery的副本并将其添加到您的网站。 You can then easily register your events thusly: 然后,您可以轻松地注册事件:

  <html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function(){
        $("controlId").bind("click", function(e) { /* do your best here! */ });
      });
    </script>
  </head>
  <!-- etc -->
  </html>

You can add the events through Javascript to the HTML elements that you want. 您可以通过Javascript将事件添加到所需的HTML元素中。 This is the cleanest way, since it doesn't mix the server&client technologies. 这是最干净的方法,因为它没有混合使用服务器和客户端技术。 Furthermore, this way many handlers can be registered to the same event (in contrast to the onclick attribute). 此外,通过这种方式,可以将许多处理程序注册到同一事件(与onclick属性相反)。

Basically, the code would be 基本上,代码是

document.getElementById('myLovelyButtonId').attachEvent('onclick',doSomething)

for Internet Explorer and 用于Internet Explorer和

document.getElementById('myLovelyButtonId').addEventListener('click',doSomething,false)

for other browsers. 对于其他浏览器。 (doSomething is an event handler - JavaScript function). (doSomething是事件处理程序-JavaScript函数)。 These calls should be made in the window load event handler 这些调用应在窗口加载事件处理程序中进行

Consider reading the advanced event registration article on Quirksmode for further info. 考虑阅读有关Quirksmode高级事件注册文章以获取更多信息。

Also, consider using a library like jQuery . 另外,考虑使用类似jQuery的库。 This way, the statement will become 这样,语句将变为

$('#myLovelyButtonId').click(
    function doSomething () {
        alert('my lovely code here');
    });

Setting the value for WebControl.Attributes["onclick"] is okay. 可以设置WebControl.Attributes["onclick"]值。 If ASP.NET needs a client-side click handler, it will concatenate the values, delimited by semi-colon. 如果ASP.NET需要客户端click处理程序,它将连接以分号分隔的值。

Fix grammatical or spelling errors. 修复语法或拼写错误。

Clarify meaning without changing it. 澄清含义而不更改它。

Correct minor mistakes. 纠正小错误。

Add related resources or links. 添加相关资源或链接。

Always respect the original author. 始终尊重原始作者。

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

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