Alertbutton, are you sure you want to do this?

Source: Internet
Author: User
Tags implement reset tostring
Perform
In the development of. Net Web applications, we want users to be able to ask or warn users when they are doing an important operation. Or hopefully we have a simple, practical control that can cause a server-side event to be raised after the user determines.

The principle of this control is very simple, mainly to implement the IPostBackEventHandler interface and Invoke Page.GetPostBackEventReference (this, eventargument) to implement the client __ Dopostback the invocation of the method, raising the server-side event

The following key code is the core of the implementation function:

if (alertstring!= "")//The client's __dostpostback is invoked only after the user confirms, raising the service-side event
{
Action = "Javascript:if (window.confirm (\" + alertstring + "') ==true) {";
Action + + page.getpostbackeventreference (this, "click");
Action + = ";}";
}

All code:

Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;

Namespace Booksir.webcontrols
{
<summary>
Summary description of the Alertbutton.
</summary>
[
Defaultproperty ("Text"),
ToolBoxData ("<{0}:alertbutton runat=server></{0}:alertbutton>"),
System.ComponentModel.DefaultEvent ("click"),
]
public class AlertButton:System.Web.UI.WebControls.WebControl, IPostBackEventHandler
{
Private Viewstatebag StateBag;

Public Alertbutton ()
{
StateBag = new Viewstatebag (this. ViewState);
}

public event EventHandler Click; Event handle

public enum Appearanceenum
{
Button,
ImageButton,
}

<summary>
The appearance mode of the button
</summary>
[
Bindable (False),
Category ("appearance"),
DefaultValue (Appearanceenum.button),
Description ("the button's appearance mode"),
]
Public Appearanceenum appearance
{
Get
{
Object obj;
obj = viewstate["Appearance"];
if (obj = null)
{
appearance = Appearanceenum.button;
return Appearanceenum.button;
}
Return (appearanceenum) obj;
}
Set
{
viewstate["appearance"] = value;
}
}

<summary>
In the case of DefaultValue, you can use reset ... To reset the default value of a property
</summary>
void Resetappearance ()
{
appearance = Appearanceenum.button;
}

<summary>
The existence of this method causes the system to not commit the property assignment code in the default value of the property
</summary>
<returns></returns>
BOOL Shouldserializeappearance ()
{
return appearance!= Appearanceenum.button;
}

[
Bindable (True),
Category ("appearance"),
DefaultValue ("")
]
public string Text
{
Get
{
Return statebag.getstring ("Text", this.id);
}

Set
{
viewstate["Text"] = value;
}
}

<summary>
Tips before you perform an action
</summary>
[
Bindable (True),
Category ("appearance"),
DefaultValue (""),
Description ("Prompt Before executing action"),
]
public string alertstring
{
Get
{
Return statebag.getstring ("alertstring", "whether to start execution?");
}
Set
{
viewstate["alertstring"] = value;
}
}

<summary>
Image when the button is available
</summary>
[
Description ("Image when button is available"),
Category ("appearance"),
Editor (typeof (System.Web.UI.Design.UrlEditor), typeof (System.Drawing.Design.UITypeEditor)),
]
public string Enabledimage
{
Get
{
Return statebag.getstring ("Enabledimage", "");
}
Set
{
viewstate["enabledimage"] = value;
}
}

<summary>
Image with Button unavailable
</summary>
[
Description ("Image when button is unavailable"),
Category ("appearance"),
Editor (typeof (System.Web.UI.Design.UrlEditor), typeof (System.Drawing.Design.UITypeEditor)),
]
public string Disabledimage
{
Get
{
Return statebag.getstring ("Disabledimage", "");
}
Set
{
viewstate["disabledimage"] = value;
}
}

<summary>
Renders this control to the specified output parameter.
</summary>
<param name= "Output" > HTML writer to write </param>
protected override void Render (HtmlTextWriter output)
{
if (appearance = = Appearanceenum.button)
Output. Write (getbuttonhtml ());
Else
Output. Write (getimagebuttonhtml ());
}

<summary>
Gets the HTML when the button is rendered
</summary>
<returns></returns>
private String getbuttonhtml ()
{
Const string Buttontag = "<input Type=button value= ' {0} ' onclick=\" {1}\ "style=\" {2}\ "{3} title= ' {4} ' >";
String sHtml;
String Action;
String Style = "width:{0};height:{1};";
if (alertstring!= "")
{
Action = "Javascript:if (window.confirm (\" + alertstring + "') ==true) {";
Action + + page.getpostbackeventreference (this, "click");
Action + = ";}";
}
Else
Action = "javascript:" + page.getpostbackeventreference (This, "click");

Style = String.Format
(
Style,
This. Width.tostring (),
This. Height.tostring ()
);
Style = this. attributes["Style"];
sHtml = String.Format
(
Buttontag,
Text,
Action,
Style,
Enabled? "": "Disabled",
This. ToolTip
);
return sHtml;
}

<summary>
Gets the HTML when rendering ImageButton
</summary>
<returns></returns>
private String getimagebuttonhtml ()
{
Const string Linktag = "<a onclick=\" {0}\ "title= ' {1} ' style=\" {2}\ ">{3}</a>";
Const string Imgtag = "String sHtml;
String Action;
String Image;
String Style;

if (this. Enabled)
{
if (alertstring!= "")//The client's __dostpostback is invoked only after the user confirms, raising the service-side event
{
Action = "Javascript:if (window.confirm (\" + alertstring + "') ==true) {";
Action + + page.getpostbackeventreference (this, "click");
Action + = ";}";
}
Else
Action = "javascript:" + page.getpostbackeventreference (This, "click");
if (enabledimage!= "")
Image = String.Format (Imgtag, enabledimage);
Else
Image = Text;
}
Else
{
Action = "javascript:void ()";
if (disabledimage!= "")
Image = String.Format (Imgtag, disabledimage);
Else
Image = Text;
}
Style = "Cursor:hand;";
Style = this. attributes["Style"];
sHtml = String.Format
(
Linktag,
Action,
This. ToolTip,
Style,
Image
);
return sHtml;
}

protected virtual void OnClick ()
{
if (Click!= null)
Click (this, eventargs.empty);
}

public void RaisePostBackEvent (String eventargument)
{
if (eventargument = = "click")
OnClick ();
}
}
}

OK, here's the end, compile the above code into a DLL and add it as a control, try it, is it simple and practical?


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.