Implement HTML button client to control Web Page Submission

Source: Internet
Author: User

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 (clientbutton,)" 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. We 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? ");
If (result)
{
Return true;
}
Else
{
Return false;
}
}
</Script>
The method is also very simple. I believe my friends will understand it at first glance. When talking about this, some friends asked me, but this is only applicable to HtmlButton, button, and more rigid. What if I want to use images as buttons, of course, there are methods, and there is no need to modify anything. After studying the css style of the button, you can satisfy your needs!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.