Tutorials to .com

Tutorials to .com » Dotnet » Control-and-components » Can teach you a confirmation dialog pop-up custom Web server control ConfirmButton

Can teach you a confirmation dialog pop-up custom Web server control ConfirmButton

Print View , by: iSee ,Total views: 12 ,Word Count: 975 ,Date: Thu, 21 May 2009 Time: 8:32 AM

Often seen in forums like this question: "... how to click the Delete button when the pop-up a dialog box to confirm the deletion."

Now we come to write such a custom Web server control!

Ideas are as follows:

System.Web.UI.WebControls.Button inherited control

Add a property "ConfirmMessage" to confirm that the pop-up message box above.

Controls on the server before the page shows javascript wrote a section of the page

Reads as follows:

<script language="JavaScript">

<! --

function _doAspxBoyConfirm ()

(

return confirm ( "you confirm the deletion / save it??")

)

//-->

</ script>

Msdn check in the description of the method Control.OnPreRender

Can be, "this method to inform the server control view state in the preservation and presentation content, the implementation of any necessary steps to pre-show."

So long as we use in the OnPreRender method in this javascript to Page.RegisterClientScriptBlock sent to the client, and to Button. Attributes attribute to add a "onclick" attribute of the corresponding value of the client to: "return _doAspxBoyConfirm ()".

Details can be found

ms-help: / / MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemwebuiwebcontrolswebcontrolclassattributestopic.htm

Such a function with Button on ConFirm basically established.

Create a new project to test the control

Points in the toolbox on the right to choose "Add / Remove items", click Browse to select the compiled dll file, click OK, you will find ConFirmButton has been added to the toolbox in the

Be entrusted to an intrinsic property Aspx pages give you ConfirmMessage value of the contents of the pop-up box such as "OK to delete it?", According to F5 to run.

When the point of the button will pop up a confirm dialog box asked "OK to delete it?", If click the button to determine the implementation of the Button_Click events, click Cancel if not the implementation.

You can view the html code he generated in order to deepen the understanding of the principles of work control

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 click will pop up a dialog box asking you to confirm

/ / / </ Summary>

public class ConfirmButton: System.Web.UI.WebControls.Button

(

private string _confirmMessage;

/ / / <summary>

/ / / When the client click on the Button when the message pop-up hints of the contents of basket

/ / / </ 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 ();

)

)

)


.Net Control and components Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.