Example #1
0
 /// <summary>
 /// Creates a drop-down list.
 /// </summary>
 /// <param name="items">The items in the list. There must be at least one.</param>
 /// <param name="selectedItemId">The ID of the selected item. This must either match a list item or be the default value of the type, unless an unlisted
 /// selected item label getter is passed.</param>
 /// <param name="width">The width of the list. This overrides any value that may be specified via CSS. If no width is specified via CSS and you pass null
 /// for this parameter, the list will be just wide enough to show the selected item and will resize whenever the selected item is changed.</param>
 /// <param name="unlistedSelectedItemLabelGetter">A function that will be called if the selected item ID does not match any list item and is not the default
 /// value of the type. The function takes the selected item ID and returns the label of the unlisted selected item, which will appear before all other
 /// items in the list. The string " (invalid)" will be appended to the label.</param>
 /// <param name="defaultValueItemLabel">The label of the default-value item, which will appear first, and only if none of the list items have an ID with the
 /// default value. Do not pass null. If you pass the empty string, no default-value item will appear.</param>
 /// <param name="placeholderIsValid">Pass true if you would like the list to include a default-value placeholder that is considered a valid selection.
 /// This will only be included if none of the list items have an ID with the default value and the default-value item label is the empty string. If you pass
 /// false, the list will still include a default-value placeholder if the selected item ID has the default value and none of the list items do, but in this
 /// case the placeholder will not be considered a valid selection.</param>
 /// <param name="placeholderText">The default-value placeholder's text. Do not pass null.</param>
 /// <param name="postBack">The post-back that will occur when the user hits Enter on the drop-down list.</param>
 /// <param name="autoPostBack">Pass true if you want a post-back to occur when the selection changes.</param>
 public static SelectList <ItemIdType> CreateDropDown <ItemIdType>(
     IEnumerable <SelectListItem <ItemIdType> > items, ItemIdType selectedItemId, System.Web.UI.WebControls.Unit?width = null,
     Func <ItemIdType, string> unlistedSelectedItemLabelGetter = null, string defaultValueItemLabel = "", bool placeholderIsValid = false,
     string placeholderText = "Please select", PostBack postBack = null, bool autoPostBack = false)
 {
     return(new SelectList <ItemIdType>(
                null,
                width,
                unlistedSelectedItemLabelGetter,
                defaultValueItemLabel,
                placeholderIsValid,
                placeholderText,
                items,
                null,
                selectedItemId,
                postBack,
                autoPostBack));
 }
Example #2
0
 /// <summary>
 /// Creates a radio button list.
 /// </summary>
 /// <param name="items">The items in the list. There must be at least one.</param>
 /// <param name="selectedItemId">The ID of the selected item. This must either match a list item or be the default value of the type, unless an unlisted
 /// selected item label getter is passed.</param>
 /// <param name="useHorizontalLayout">Pass true if you want the radio buttons to be laid out horizontally instead of vertically.</param>
 /// <param name="unlistedSelectedItemLabelGetter">A function that will be called if the selected item ID does not match any list item and is not the default
 /// value of the type. The function takes the selected item ID and returns the label of the unlisted selected item, which will appear before all other
 /// items in the list. The string " (invalid)" will be appended to the label.</param>
 /// <param name="defaultValueItemLabel">The label of the default-value item, which will appear first, and only if none of the list items have an ID with the
 /// default value. Do not pass null. If you pass the empty string, no default-value item will appear and therefore none of the radio buttons will be
 /// selected if the selected item ID has the default value and none of the list items do.</param>
 /// <param name="disableSingleButtonDetection">Pass true to allow just a single radio button to be displayed for this list. Use with caution, as this
 /// violates the HTML specification.</param>
 /// <param name="postBack">The post-back that will occur when the user hits Enter on a radio button.</param>
 /// <param name="autoPostBack">Pass true if you want a post-back to occur when the selection changes.</param>
 public static SelectList <ItemIdType> CreateRadioList <ItemIdType>(
     IEnumerable <SelectListItem <ItemIdType> > items, ItemIdType selectedItemId, bool useHorizontalLayout = false,
     Func <ItemIdType, string> unlistedSelectedItemLabelGetter = null, string defaultValueItemLabel = "", bool disableSingleButtonDetection = false,
     PostBack postBack = null, bool autoPostBack = false)
 {
     return(new SelectList <ItemIdType>(
                useHorizontalLayout,
                null,
                unlistedSelectedItemLabelGetter,
                defaultValueItemLabel,
                null,
                null,
                items,
                disableSingleButtonDetection,
                selectedItemId,
                postBack,
                autoPostBack));
 }
 /// <summary>
 /// Creates a check box. Do not pass null for label.
 /// </summary>
 public EwfCheckBox(bool isChecked, string label = "", PostBack postBack = null)
 {
     checkBoxFormValue = GetFormValue(isChecked, this);
     this.label        = label;
     this.postBack     = postBack;
 }
        internal static void AddCheckBoxAttributes(WebControl checkBoxElement, Control checkBox, FormValue <bool> checkBoxFormValue,
                                                   FormValue <CommonCheckBox> radioButtonFormValue, string radioButtonListItemId, PostBack postBack, bool autoPostBack,
                                                   IEnumerable <string> onClickJsMethods)
        {
            checkBoxElement.Attributes.Add("type", checkBoxFormValue != null ? "checkbox" : "radio");
            checkBoxElement.Attributes.Add("name", checkBoxFormValue != null ? checkBox.UniqueID : ((FormValue)radioButtonFormValue).GetPostBackValueKey());
            if (radioButtonFormValue != null)
            {
                checkBoxElement.Attributes.Add("value", radioButtonListItemId ?? checkBox.UniqueID);
            }
            if (checkBoxFormValue != null
                                    ? checkBoxFormValue.GetValue(AppRequestState.Instance.EwfPageRequestState.PostBackValues)
                                    : radioButtonFormValue.GetValue(AppRequestState.Instance.EwfPageRequestState.PostBackValues) == checkBox)
            {
                checkBoxElement.Attributes.Add("checked", "checked");
            }

            PostBackButton.EnsureImplicitSubmission(checkBoxElement, postBack);
            var isSelectedRadioButton = radioButtonFormValue != null &&
                                        radioButtonFormValue.GetValue(AppRequestState.Instance.EwfPageRequestState.PostBackValues) == checkBox;
            var postBackScript = autoPostBack && !isSelectedRadioButton
                                                     ? PostBackButton.GetPostBackScript(postBack ?? EwfPage.Instance.DataUpdatePostBack, includeReturnFalse : false)
                                                     : "";

            var customScript = StringTools.ConcatenateWithDelimiter("; ", onClickJsMethods.ToArray());

            checkBoxElement.AddJavaScriptEventScript(JsWritingMethods.onclick, StringTools.ConcatenateWithDelimiter("; ", postBackScript, customScript));
        }
Example #5
0
        /// <summary>
        /// Creates a form item with a change based check box list, which is a check box list that is based on changes to the selections rather than the absolute
        /// set of selected items.
        /// </summary>
        /// <typeparam name="ItemIdType"></typeparam>
        /// <param name="label"></param>
        /// <param name="items"></param>
        /// <param name="modificationMethod">A method that executes the change handlers of the items that were selected or deselected on this post back.</param>
        /// <param name="caption"></param>
        /// <param name="includeSelectAndDeselectAllButtons"></param>
        /// <param name="numberOfColumns"></param>
        /// <param name="postBack"></param>
        /// <param name="cellSpan"></param>
        /// <param name="textAlignment"></param>
        /// <param name="validationPredicate"></param>
        /// <param name="validationList"></param>
        /// <returns></returns>
        public static FormItem GetFormItem <ItemIdType>(
            FormItemLabel label, IEnumerable <ChangeBasedListItemWithSelectionState <ItemIdType> > items, out Action modificationMethod, string caption = "",
            bool includeSelectAndDeselectAllButtons = false, byte numberOfColumns = 1, PostBack postBack = null, int?cellSpan = null,
            TextAlignment textAlignment             = TextAlignment.NotSpecified, Func <bool> validationPredicate = null, ValidationList validationList = null)
        {
            var itemArray         = items.ToArray();
            var selectedItemIds   = itemArray.Where(i => i.IsSelected).Select(i => i.Item.Item.Id);
            var uiSelectedItemIds = itemArray.Where(i => i.IsSelectedInUi).Select(i => i.Item.Item.Id);
            var checkBoxList      = new ChangeBasedCheckBoxList <ItemIdType>(
                itemArray.Select(i => i.Item),
                selectedItemIds,
                caption,
                includeSelectAndDeselectAllButtons,
                numberOfColumns,
                uiSelectedItemIds,
                postBack);

            modificationMethod = checkBoxList.ModifyData;
            return(FormItem.Create(
                       label,
                       checkBoxList,
                       cellSpan: cellSpan,
                       textAlignment: textAlignment,
                       validationGetter: control => new Validation(
                           (pbv, validator) => {
                if (validationPredicate != null && !validationPredicate())
                {
                    return;
                }
                control.Validate(pbv);
            },
                           validationList ?? EwfPage.Instance.DataUpdate)));
        }
        /// <summary>
        /// Creates an in-line radio button that is part of the group.
        /// </summary>
        public EwfCheckBox CreateInlineRadioButton(bool isSelected, string label = "", PostBack postBack = null, bool autoPostBack = false)
        {
            var checkBox = new EwfCheckBox(formValue, label, postBack)
            {
                AutoPostBack = autoPostBack
            };

            checkBoxesAndSelectionStates.Add(Tuple.Create <CommonCheckBox, bool>(checkBox, isSelected));
            return(checkBox);
        }
Example #7
0
        /// <summary>
        /// Returns a JavaScript function call getter that opens a Stripe Checkout modal window. If the window's submit button is clicked, the credit card is
        /// charged or otherwise used. Do not execute the getter before all controls have IDs.
        /// </summary>
        /// <param name="testPublishableKey">Your test publishable API key. Will be used in non-live installations. Do not pass null.</param>
        /// <param name="livePublishableKey">Your live publishable API key. Will be used in live installations. Do not pass null.</param>
        /// <param name="name">See https://stripe.com/docs/checkout. Do not pass null.</param>
        /// <param name="description">See https://stripe.com/docs/checkout. Do not pass null.</param>
        /// <param name="amountInDollars">See https://stripe.com/docs/checkout, but note that this parameter is in dollars, not cents</param>
        /// <param name="testSecretKey">Your test secret API key. Will be used in non-live installations. Do not pass null.</param>
        /// <param name="liveSecretKey">Your live secret API key. Will be used in live installations. Do not pass null.</param>
        /// <param name="successHandler">A method that executes if the credit-card submission is successful. The first parameter is the charge ID and the second
        /// parameter is the amount of the charge, in dollars.</param>
        /// <param name="prefilledEmailAddressOverride">By default, the email will be prefilled with AppTools.User.Email if AppTools.User is not null. You can
        /// override this with either a specified email address (if user is paying on behalf of someone else) or the empty string (to force the user to type in the
        /// email address).</param>
        public static Func <string> GetCreditCardCollectionJsFunctionCall(
            string testPublishableKey, string livePublishableKey, string name, string description, decimal?amountInDollars, string testSecretKey, string liveSecretKey,
            Func <string, decimal, StatusMessageAndDestination> successHandler, string prefilledEmailAddressOverride = null)
        {
            if (!EwfApp.Instance.RequestIsSecure(HttpContext.Current.Request))
            {
                throw new ApplicationException("Credit-card collection can only be done from secure pages.");
            }
            EwfPage.Instance.ClientScript.RegisterClientScriptInclude(
                typeof(PaymentProcessingStatics),
                "Stripe Checkout",
                "https://checkout.stripe.com/v2/checkout.js");

            if (amountInDollars.HasValue && amountInDollars.Value.DollarValueHasFractionalCents())
            {
                throw new ApplicationException("Amount must not include fractional cents.");
            }

            ResourceInfo successDestination = null;
            var          postBack           = PostBack.CreateFull(
                id: PostBack.GetCompositeId("ewfCreditCardCollection", description),
                actionGetter: () => new PostBackAction(successDestination));
            var token = new DataValue <string>();

            Func <PostBackValueDictionary, string> tokenHiddenFieldValueGetter;            // unused
            Func <string> tokenHiddenFieldClientIdGetter;

            EwfHiddenField.Create("", postBackValue => token.Value = postBackValue, postBack, out tokenHiddenFieldValueGetter, out tokenHiddenFieldClientIdGetter);

            postBack.AddModificationMethod(
                () => {
                // We can add support later for customer creation, subscriptions, etc. as needs arise.
                if (!amountInDollars.HasValue)
                {
                    throw new ApplicationException("Only simple charges are supported at this time.");
                }

                var apiKey       = AppTools.IsLiveInstallation ? liveSecretKey : testSecretKey;
                dynamic response = new StripeClient(apiKey).CreateCharge(
                    amountInDollars.Value,
                    "usd",
                    new CreditCardToken(token.Value),
                    description: description.Any() ? description : null);
                if (response.IsError)
                {
                    if (response.error.type == "card_error")
                    {
                        throw new DataModificationException(response.error.message);
                    }
                    throw new ApplicationException("Stripe error: " + response);
                }

                try {
                    var messageAndDestination = successHandler((string)response.id, amountInDollars.Value);
                    if (messageAndDestination.Message.Any())
                    {
                        EwfPage.AddStatusMessage(StatusMessageType.Info, messageAndDestination.Message);
                    }
                    successDestination = messageAndDestination.Destination;
                }
                catch (Exception e) {
                    throw new ApplicationException("An exception occurred after a credit card was charged.", e);
                }
            });

            EwfPage.Instance.AddPostBack(postBack);
            return(() => {
                var jsTokenHandler = "function( res ) { $( '#" + tokenHiddenFieldClientIdGetter() + "' ).val( res.id ); " +
                                     PostBackButton.GetPostBackScript(postBack, includeReturnFalse: false) + "; }";
                return "StripeCheckout.open( { key: '" + (AppTools.IsLiveInstallation ? livePublishableKey : testPublishableKey) + "', name: '" + name +
                "', description: '" + description + "', " + (amountInDollars.HasValue ? "amount: " + amountInDollars.Value * 100 + ", " : "") + "token: " +
                jsTokenHandler + ", email: '" + (prefilledEmailAddressOverride ?? (AppTools.User == null ? "" : AppTools.User.Email)) + "' } )";
            });
        }
 /// <summary>
 /// Creates a check box. Do not pass null for label.
 /// </summary>
 public BlockCheckBox(bool isChecked, string label = "", bool highlightWhenChecked = false, PostBack postBack = null)
 {
     checkBoxFormValue         = EwfCheckBox.GetFormValue(isChecked, this);
     this.label                = label;
     this.highlightWhenChecked = highlightWhenChecked;
     this.postBack             = postBack;
     NestedControls            = new List <Control>();
 }