We know that the appropriate use of Html controls in Asp.net has many conveniences. Html controls can be controlled in the cs file after being submitted to the server on the client, this cannot be done with a separate Web server control, which limits the flexibility of our development. therefore, we will use the HtmlButton control in many places. the following is a tips for using this control:
If you want to click HtmlButton to submit the webpage, we will choose to run the HtmlButton as a server control. in this way, every time you click HtmlButton, the page will be submitted to the server. However, if you need to do some processing on the client after clicking the button, and then choose whether to submit the webpage, follow the general processing method, it seems that HtmlButton is powerless, but let's analyze the Html generated by HtmlButton after the webpage is running. The generated Html is as follows:
<Input language = "javascript" onclick = "_ doPostBack ('clientclick ','') "name =" clientbutton "id =" clientbutton "type =" button "style =" Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 80px "value =" Button "/>
We can see that the generated Html adds an onclick event to the button so that the webpage can be submitted. Then we can understand the execution process and then know what to do, if we add a client operation to the button before executing the _ doPostBack ('clientbutton', '') operation and decide whether to submit the operation, this will not meet our needs, if you understand the implementation principle, I will not talk about it. I will explain how to implement it below:
1. We Add Attributes to HtmlButton in the background cs file. clientbutton is set to HtmlButton as id, and clientoper is set to client javascript function. clientbutton. Attributes. Add ("onclick", "if (! Clientoper () {return ;}");
This sentence is generally placed in a webpage loading event.
2. Add the clientoper Method to the client and perform the required operations. After the operation is completed, determine whether to submit the request based on the returned value. If the return value is true, the webpage submits the request. If the return value is false, the client does not submit the request.
<Script language = javascript>
Function clientoper ()
{
// You can perform client operations, such as verification.
// Determine whether to submit
Var result = window. confirm ("are you sure you want to submit? ");