简体   繁体   中英

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

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? otherwise how can I let the button cmdOK to execute a javascript function first before postback? 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.

The OnClick event calls server-side code, after a postback.

Example from the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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