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>";
   }
}

# re: Ok/Cancel web control

Wednesday, July 13, 2005 3:58 PM by triplez

Do you have the cs file and the bin? And some sample code on how to use? I'll upload it to my server for everyone.

# re: Ok/Cancel web control

Thursday, July 14, 2005 9:19 AM by triplez

# re: Ok/Cancel web control

Friday, October 06, 2006 10:38 AM by alvinz_c

You should include the checking of Page_ClientValidate() in the client-script, something like

if(Page_Validate())

 return confirm('Do you want to proceed?');

because your control might be working weird if there are validator server controls used at the same time. For more issue, you can check my blog at http://alvinzc.blogspot.com/2006/10/requiredfieldvalidator-onclientclick.html