Two differences between click events OnClick and OnClientClick requests in c#

In C#, if the button of the asp control has two click calls, one is OnClick and the other is OnClientClick. So what is the difference between the two? Let’s talk about the difference below.

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Page_Load" OnClientClick="one()" /> 

First of all, you will see asp: in front of this button, which means it is an asp control.

runat="server", which means that this control can request the server, that is, the backend. If this sentence is added, the backend can directly operate the control through the ID, such as: assignment; the backend code can be written as this.Button1.Text = ""; 

It is said that onClick is to execute the server-side operation, and onClientClick is to execute the front-end method operation, so who executes first, onClientClick executes first, if the method in onClientClick returns true, the back-end method in onClick will be executed.

If you let these two methods work at the same time, you should pay attention to OnClientClick="return method name (); If you don't add return, then the OnClick event will be executed regardless of whether the return structure of OnClientClick is true or false.

Front-end code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication2.index" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<form id="form1" runat="server">
 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Page_Load" OnClientClick="return one()" />
</form>
</body>
</html>
<script type="text/javascript">
    function one() {
        document.getElementById("Button1").innerText = "你好";
        return false;
    }
</script>

  rear end:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
             Button1.Text = "www";
         }     
    }
}   

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768334&siteId=291194637