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!

Ideas are as follows:

Inheriting System.Web.UI.WebControls.Button controls

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 ()".

Detailed information can be consulted

Ms-help://ms. Vscc.2003/ms. Msdnqtr.2003feb.2052/cpref/html/frlrfsystemwebuiwebcontrolswebcontrolclassattributestopic.htm

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

</summary>

public string Confirmmessage

{

Get

{

return _confirmmessage;

}



Set

{

_confirmmessage = value;

}

}



protected override void OnPreRender (System.EventArgs e)

{

StringBuilder sb = new StringBuilder ();

Sb. Append ("<script language=\" javascript\ ">");

Sb. Append (System.Environment.NewLine);

Sb. Append ("<!--");

Sb. Append (System.Environment.NewLine);

Sb. Append ("* *--------------------------------------------");

Sb. Append (System.Environment.NewLine);

Sb. Append ("Controlname:\t\taspxboy.com.confirmbutton");

Sb. Append (System.Environment.NewLine);

Sb. Append ("Authorname:\t\t\thuobazi,wumeibo");

Sb. Append (System.Environment.NewLine);

Sb. Append ("copyright:\t\t\twww.aspxboy.com");

Sb. Append (System.Environment.NewLine);

Sb. Append ("---------------------------------------------* *");

Sb. Append (System.Environment.NewLine);

Sb. Append ("function _doaspxboyconfirm ()");

Sb. Append (System.Environment.NewLine);

Sb. Append ("{");

Sb. Append (System.Environment.NewLine);

Sb. Append ("return confirm");

Sb. Append (Confirmmessage);

Sb. Append ("\"));

Sb. Append (System.Environment.NewLine);

Sb. Append ("}");

Sb. Append (System.Environment.NewLine);

Sb. Append ("//-->");

Sb. Append ("</script>");

Page.registerclientscriptblock ("_doaspxboyconfirm", sb.) ToString ());

This. Attributes.Add ("onclick", "Return _doaspxboyconfirm ()");

Base. OnPreRender (e);

}

public override void RenderBeginTag (HtmlTextWriter writer)

{

Writer. WriteLine ();

Writer. Write ("<!-------------------");

Writer. Write ("AspxBoy.Com.ConfirmButton Start");

Writer. Write ("\tauthorname: \thuobazi");

Writer. WriteLine ("--------------------->");

Writer. Write ("<!--------------------");

Writer. Write ("copyright:2004 Huobazi (www.AspxBoy.com)");

Writer. Write ("---------------------");

Writer. WriteLine (">");

Base. RenderBeginTag (writer);

}

public override void RenderEndTag (HtmlTextWriter writer)

{

Base. RenderEndTag (writer);

Writer. WriteLine ();

Writer. Write ("<!-------------------------------");

Writer. Write ("AspxBoy.Com.ConfirmButton end");

Writer. Write ("--------------------------------");

Writer. WriteLine (">");

Writer. WriteLine ();

}

}

}


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.