Custom WEB Server Control ConfirmButton with Confirmation Dialog
I often see a problem like this in the forum: "How to pop up a confirmation delete dialog when you click the Delete button.
Below we come from yourself writing a custom web server control!
The idea is as follows:
Inherit System.Web.ui.WebControls.Button control
Add a property "confirmMessage" to indicate the prompt information above the pop-up confirmation.
Write a JavaScript to the page before the server control is presented before the page.
The content is as follows:
Function _doaspxBoyConfirm ()
{
Return Confirm ("Do you confirm delete / save ??")
}
// ->
script>
Check a description of the Control.onPrender method in MSDN
You can get "This method notifies the server control before the view status and presence content," before saving the view status and presenting content ".
So we just send this JavaScript to the client with Page.RegisterClientScriptBlock in the OnPrender method, and add a "onclick" client property corresponding to the Button. Attributes property: "Return_DoaspxBoyConfirm ()".
Details can be found
MS-help: //ms.vscc.2003/ms.msdnqtr.2003feb.2052/cpref/html/frlrfsystemwebuiwebControlsWebControlClassAttributestopic.htm
Such a CONFIRM function is basically established.
New project testing the control
In the toolbox, right click to select "Add / Transfer", click Browse to select the compiled DLL file, click OK, you will find that CONFIRMBUTTON has been added to the toolbox.
Trust it to an ASPX page to give the confirmMessage value for the CONFIRMMESSAGE value in the property setting, such as "OK to delete?", Press F5 to run.
When you point this button, a confirm dialog box will pop up "OK to delete?", If you click OK, execute the Button's Button_Click event, if you click Cancel, do not execute.
You can view the HTML code he generated to deepen the understanding of the working principle of the control.
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
{
///
/// Button Click to pop up a dialog requires confirmation
/// summary>
Public class confirmButton: System.Web.ui.WebControls.Button
{
Private string _confirmmessage;
///
/// When the client clicks on this button, the content of the prompt message basket is bounced.
/// summary>
Public String ConfirmMessage
{
get
{
Return_ConfirmMessage;
}
set
{
_confirmmessage = value;
}
}
Protected Override Void OnPrender (System.EventArgs E)
{