Wednesday, July 13, 2005 2:28 PM
hannes
Ok/Cancel web control
I built this silly control for Ok/Cancel Javascript prompts initiated from the server side. This is useful in cases where you would like to check whether you do want the prompt to pop up first, eg. to check whether the user is allowed to delete an item or not.
To use the control, the Show method is called, and the Click event will fire if the user clicks Ok.
The source code is small enough. :)
[ Designer("epc_webcontrols.popupDesigner, epc_webcontrols") ]
public class popup : WebControl
{
public event EventHandler Click;
private LinkButton _Button;
private string FmtJSConstant(string s)
{ return s.Replace("\\", "\\\\").Replace("'", "\\'"); }
public void Show(string prompt)
{
string JID = this.UniqueID.Replace(":", "_");
string script =
"<script>" +
" if(confirm('%%TEXT%%')) __doPostBack('%%CID%%','');" +
"</script>";
script = script.Replace("%%JID%%", JID);
script = script.Replace("%%TEXT%%", FmtJSConstant(prompt));
script = script.Replace("%%CID%%", FmtJSConstant(_Button.ClientID));
if (!Page.IsClientScriptBlockRegistered("pppopup_" + JID))
Page.RegisterStartupScript("pppopup_" + JID, script);
}
protected override void OnInit(EventArgs e)
{
_Button = new LinkButton();
this.Controls.Add(_Button);
_Button.Click += new EventHandler(_Button_Click);
base.OnInit (e);
}
private void _Button_Click(object sender, EventArgs e)
{
if (Click != null) Click(this, e);
}
}
public class popupDesigner : System.Web.UI.Design.ControlDesigner
{
public override string GetDesignTimeHtml()
{
popup ctl = (popup) Component;
return @"<DIV style='background-color:blue;color:white;'>Ok / Cancel</DIV>";
}
}