Teach you to do a custom Web server control that pops up the confirmation dialog box ConfirmButton
Source: Internet
Author: User
Web|web Service |web Server | dialog | Controls often see problems like this in the forums: "... How to click the Delete button to pop up a confirmation delete dialog box.
Let's write one of these custom Web server controls ourselves!
Add an attribute "Confirmmessage" to indicate the message above the pop-up confirmation box.
Write a piece of JavaScript to the page before the server control renders on the page
The contents are as follows:
<script language= "JavaScript" >
<!--
function _doaspxboyconfirm ()
{
return confirm ("Do you confirm delete/save??")
}
-->
</script>
Check out MSDN for a description of the Control.onprerender method
You can get "This method notifies the server control to perform any necessary pre-rendering steps before saving view state and rendering content."
So we just use Page.registerclientscriptblock to send this JavaScript to the client in the OnPreRender method and give it to the button. The Attributes property adds a "onclick" client property corresponding to the value: "Return _doaspxboyconfirm ()".
Such a button with confirm function is basically built up.
Create a new project to test the control
Right-click on the Toolbox to select Add/Remove items, browse to select the compiled DLL file, click OK, and you will find that ConfirmButton has been added to the toolbox.
Take it to an ASPX page intrinsic property setting to Confirmmessage values for the pop-up box you want, such as "OK Delete?", press F5 to run.
When the button is clicked, a confirm dialog box pops up asking "OK to delete?" And if you click OK then execute the button's Button_Click event and do not execute if you click Cancel.
You can look at his generated HTML code to deepen the understanding of how the control works
The complete code is as follows:
Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.Text;
Namespace AspxBoy.Com.ConfirmButton
{
<summary>
button clicks, a dialog box pops up asking for confirmation.
</summary>
public class ConfirmButton:System.Web.UI.WebControls.Button
{
private string _confirmmessage;
<summary>
The contents of the message basket that pops up when the client clicks this button
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.