Compare and Confirm Button

Discussion in 'ASP .Net Web Controls' started by gover, Jul 2, 2007.

  1. gover

    gover Guest

    Hello,
    I have a requirement to display a Confirmation pop-up message (' i.e. a
    are you sure yes/no) when a user clicks a submit button AND one date field
    on my page is less than another date field on my page. I have written the
    JavaScript to do this on one page, and am now trying to encapsulate it into a
    reusable control. (Source Below) The control is based on a submit button
    but also has the functionality of a validator (i.e. control to validate,
    control to compare etc. ) The goal is that when the user clicks the button ,
    I call the ValidatorCompare JavaScript function and if the condition is not
    met, a JavaScript Confirm box is displayed.

    I'm having difficulty calling into the WebUIValidation.js function
    ValidatorCompare(operand1, operand2, operator, val) . The fourth argument is
    JavaScript validator and I don't know how to create one to pass to the
    function. I tried this
    var val = new Validator();
    but I got an error message "Microsoft JScript runtime error: 'Validator' is
    undefined".
    Can someone tell Me if the fourth argument is in fact a JavaScript
    Validator object and where/how to define one?
    Russ


    CODE
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ConfirmButton runat=server Text=\"ConfirmButton\"
    Message=\"ConfirmationMessage\"></{0}:ConfirmButtonw>")]
    public class ConfirmButton : System.Web.UI.WebControls.Button
    {
    string message;
    #region atteributes

    [Bindable(true),Category("Appearance"),DefaultValue(""),Localizable(true)]
    public string Message
    {
    get
    { return message; }
    set
    { message = value; }
    }
    [TypeConverter(typeof(ValidatedControlConverter)),
    Category("Behavior"), Themeable(false), DefaultValue(""),
    Description("CompareValidator_ControlToValidate")]
    public string ControlToValidate
    {
    get
    {
    object controlToValidate =
    this.ViewState["ControlToValidate"];
    if (controlToValidate != null)
    {
    return (string)controlToValidate;
    }
    return string.Empty;
    }
    set
    {
    this.ViewState["ControlToValidate"] = value;
    }
    }
    [TypeConverter(typeof(ValidatedControlConverter)),
    Category("Behavior"), Themeable(false), DefaultValue(""),
    Description("CompareValidator_ControlToCompare")]
    public string ControlToCompare
    {
    get
    {
    object ControlToValidate = this.ViewState["ControlToCompare"];
    if (ControlToValidate != null)
    {
    return (string)ControlToValidate;
    }
    return string.Empty;
    }
    set
    {
    this.ViewState["ControlToCompare"] = value;
    }
    }
    [Category("Behavior"), Themeable(false), DefaultValue(""),
    Description("CompareValidator_ValueToCompare")]
    public string ValueToCompare
    {
    get
    {
    object ValueToCompare = this.ViewState["ValueToCompare"];
    if (ValueToCompare != null)
    {
    return (string)ValueToCompare;
    }
    return string.Empty;
    }
    set
    {
    this.ViewState["ValueToCompare"] = value;
    }
    }



    [DefaultValue(0), Themeable(false), Category("Behavior"),
    Description("CompareValidator_Operator")]
    public ValidationCompareOperator Operator
    {
    get
    {
    object Operator = this.ViewState["Operator"];
    if (Operator != null)
    {
    return (ValidationCompareOperator)Operator;
    }
    return ValidationCompareOperator.Equal;
    }
    set
    {
    if ((value < ValidationCompareOperator.Equal) || (value >
    ValidationCompareOperator.DataTypeCheck))
    {
    throw new ArgumentOutOfRangeException("value");
    }
    this.ViewState["Operator"] = value;
    }
    }
    #endregion attributes
    protected string GetControlRenderID(string name)
    {
    Control control = this.FindControl(name);
    if (control == null)
    {
    return string.Empty;
    }
    return control.ClientID;
    }
    protected override void OnPreRender(EventArgs e)
    {

    string ControlToCompareClientID =
    GetControlRenderID(this.ControlToCompare);
    message += GetControlRenderID(this.ControlToCompare);
    message += GetControlRenderID(this.ControlToCompare);
    string vbCrLf = "\n\r";
    Page.RegisterClientScriptBlock("__doAlert",
    "<script language=\"javascript\">" + vbCrLf +
    "function __doConfirm(btn){" + vbCrLf +
    // lots of test stuff here, need to call WEBUI Validation to compare
    "var valuetocompare;" + vbCrLf +
    // this gets the value as a string
    "valuetocompare=ValidatorGetValueRecursive(" + ControlToCompareClientID +
    ");" + vbCrLf +
    // need to get value as an object by calling ValidatorConvert, but to do
    this I need a javascript 'val'
    //How do I create the second parm
    "alert( ValidatorConvert(valuetocompare,\"Date\",null));" + vbCrLf +

    "var answer = confirm(\"" + message + "\");" + vbCrLf +
    "if (answer){" + vbCrLf +
    "return true;}" + vbCrLf +
    "else{" + vbCrLf +
    "return false;}}" + vbCrLf +
    "</script>"
    );

    this.Attributes["onclick"] = "return __doConfirm(this);";

    base.OnPreRender(e);

    }
     
    gover, Jul 2, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul W
    Replies:
    3
    Views:
    559
    Steve C. Orr [MVP, MCSD]
    Nov 15, 2004
  2. =?Utf-8?B?VGFydW4gVXBhZGh5YXlh?=

    Problem showing confirm dialog on button webcontrol's onclick even

    =?Utf-8?B?VGFydW4gVXBhZGh5YXlh?=, Jun 1, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,567
    Peter Blum
    Jun 2, 2005
  3. ad
    Replies:
    3
    Views:
    3,994
    Lucas Tam
    Sep 23, 2005
  4. Replies:
    3
    Views:
    1,266
    Eliyahu Goldin
    Apr 4, 2006
  5. Indu
    Replies:
    9
    Views:
    359
Loading...

Share This Page