Let's use Asp.net together for confirmation dialog box

Source: Internet
Author: User

Path:

Inherit the system. Web. UI. webcontrols. Button Control

Add the "confirmmessage" attribute to display the prompt information in the confirmation box.

Write a piece of JavaScript to the page before the server control is displayed on the page

The content is as follows:

<Script language = "JavaScript">

<! --

Function _ doaspxboyconfirm ()

{

Return confirm ("are you sure you want to delete/save ?? ")

}

// -->

</SCRIPT>

Check the description of the Control. onprerender method in msdn.

You can get "This method notifies the Server Control to execute any necessary pre-rendering steps before saving the view status and rendering content ".

Therefore, we only need to use page in the onprerender method. registerclientscriptblock sends the JavaScript code to the client and sends it to the button. the corresponding value of the client attribute for adding an "onclick" to the attributes attribute is: "Return _ doaspxboyconfirm ()".

For details, refer

MS-help: // Ms. vscc.2003/ms. msdnqtr.2003feb. 2052/cpref/html/frlrfsystemwebuiwebcontrolswebcontrolclassattributestopic.htm

Such a button with the confirm function is basically set up.

Create a project to test the control

Right-click the toolbox and select "Add/Remove items". Click Browse to select the compiled DLL file and click OK. You will find that confirmbutton has been added to the toolbox.

Place it to An ASPX page and set the confirmmessage value in the attribute settings to the pop-up box you want, for example, "Are you sure you want to delete it ?", Press F5 to run.

When you click this button, a confirm dialog box will pop up asking "are you sure you want to delete it ?", If you click OK, The button_click event of the button is executed. If you click Cancel, the event is not executed.

You can view the generated HTML code to better understand 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

{

/// <Summary>

/// A confirmation dialog box is displayed when the button is clicked.

/// </Summary>

Public class confirmbutton: system. Web. UI. webcontrols. Button

{

Private string _ confirmmessage;

/// <Summary>

/// When the client clicks this button, the message basket content is displayed.

/// </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/thuobazi, wumeibo ");

SB. append (system. environment. newline );

SB. append ("Copyright:/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.