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 ());
}
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.