简体   繁体   English

ASP.NET中的onclick和OnClick

[英]onclick and OnClick in ASP.NET

I have seen: 我见过:

<asp:Button ID="cmdOK" Text="OK" OnClick="cmdOK_Click" runat="server" />

cmdOK_Click is the method name in the code behind class on server cmdOK_Click是服务器上类背后的代码中的方法名称

but I also see: 但我也看到:

<select ID="lstBackColor" onclick="__doPostBack('lstBackColor',")" />

which means that onclick is linked to a javascript function, so are onclick and OnClick different things? 这意味着onclick链接到javascript函数,onclick和OnClick是否有所不同? otherwise how can I let the button cmdOK to execute a javascript function first before postback? 否则,如何让按钮cmdOK在回发之前先执行javascript函数? can I code like: 我可以这样编码吗:

<asp:Button ID="cmdOK" Text="OK" onclick="aJavascriptFunction()" OnClick="cmdOK_Click" runat="server" />

If you want to call javascript on a click on an <asp:Button runat=server> , you will need the OnClientClick property. 如果要单击<asp:Button runat=server>来调用javascript,则需要OnClientClick属性。

The OnClick event calls server-side code, after a postback. 回发后, OnClick事件将调用服务器端代码。

Example from the docs : 来自docs的示例:

<asp:button id="Button1"
   usesubmitbehavior="true"
   text="Open Web site"
   onclientclick="Navigate()"
   runat="server" onclick="Button1_Click" />

<script type="text/javascript">
  function Navigate()
  {
    javascript:window.open("http://www.microsoft.com");
  }    

</script>

Note also that the casing of "onclick" does not matter. 还要注意,“ onclick”的大小写无关紧要。

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

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