JavaScript<%= MyProperty%>

时间:2011-05-24 17:54:43

标签: javascript asp.net

在JavaScript中使用此类构造有哪些限制。在我的codeBehind中我有属性,并且在获取它时有相当复杂的逻辑。它调用其他方法等等,我得到的结果是空字符串尽管在调试它时显示返回良好的值。我想知道我能做什么,因为这种类型的代码在render方法中运行。

2 个答案:

答案 0 :(得分:2)

使用该方法呈现JavaScript没有任何限制。您只需要确保您的属性在各种情况下可用,例如标准HTTP get,回发等。当然,您必须确保属性已根据需要正确转义/ JavaScript编码,即如果它是一个包含引号等的字符串值。

答案 1 :(得分:0)

我一直都在使用这个......

我还建议您执行以下操作:

this.txtProjectName = $('#<%=this.txtProjectName.ClientID %>');

...这种方式txtProjectName是一个jQuery对象。

以下是一个广泛的例子:
请注意,下面课程中的一些调用是针对此处未显示的对象。

<script type="text/javascript">

    var projectDialog = (function($) {
        var publicInstances = {};

        // ***********************
        // form
        publicInstances.form = form;
        function form(controller) {

            /// <summary></summary>
            var self = this;

            /// <summary></summary>
            this.controller = controller;
            /// <summary></summary>
            this.validationController = null;

            this.hidAsOf = $('#<%=this.hidAsOf.ClientID %>');
            this.hidProjectKey = $('#<%=this.hidProjectKey.ClientID %>');
            this.hidMode = $('#<%=this.hidMode.ClientID %>');
            this.ddlClients = $('#<%=this.ddlClients.ClientID %>');

            this.txtProjectName = $('#<%=this.txtProjectName.ClientID %>');
            this.txtProjectNumber = $('#<%=this.txtProjectNumber.ClientID %>');
            this.txtProjectManager = $('#<%=this.txtProjectManager.ClientID %>');
            this.txtProjectDescription = $('#<%=this.txtProjectDescription.ClientID %>');
            this.ddlEMREProductivityCurve = $('#<%=this.ddlEMREProductivityCurve.ClientID %>');
            this.txtStartDate = $('#<%=this.txtStartDate.ClientID %>');
            this.txtEndDate = $('#<%=this.txtEndDate.ClientID %>');
            this.txtEngineeringHours = $('#<%=this.txtEngineeringHours.ClientID %>');
            this.txtDirectHours = $('#<%=this.txtDirectHours.ClientID %>');
            this.txtIndirectHours = $('#<%=this.txtIndirectHours.ClientID %>');
            this.txtOtherHours = $('#<%=this.txtOtherHours.ClientID %>');
            this.txtARRThreshold = $('#<%=this.txtARRThreshold.ClientID %>');
            this.txtAppropriationHours = $('#<%=this.txtAppropriationHours.ClientID %>');
            this.txtAppropriationDollars = $('#<%=this.txtAppropriationDollars.ClientID %>');
            this.ibtnSaveProject = $('#<%=this.ibtnSaveProject.ClientID %>');
            this.imgClose = $('#<%=imgClose.ClientID %>');
            this.imgStartDate = $('#<%=imgStartDate.ClientID %>');
            this.imgEndDate = $('#<%=imgEndDate.ClientID %>');

            this.disabledCssClass = "textbox-locked";

            /// <summary></summary>
            this.initialize = function() {

                var context = $(self.controller.container);
                var contentArea = $(self.controller.contentArea, context);

                self.validationController = new validation.controller(contentArea);

                if(self.controller.readOnly == true)
                    self.disableForm();

                /// <summary>Set defaults.</summary>
                self.ddlClients.attr('disabled', 'disabled');

                /// <summary>Register click events.</summary>
                self.ibtnSaveProject.click(function() { self.controller.save(); });
                self.imgClose.click(function() { self.close(); });

                /// <summary>Register rounders.</summary>
                $('input.rounder-decimal', context).numericRounder();

                /// <summary>Register keypress events.</summary>
                $('input.rounder-decimal', context).keypress(function(e) {
                    return enforceValueAsDollar(e, this.val);
                });

                /// <summary>Register change events.</summary>
                $('input.rounder-decimal', context).change(function() {
                    var element = $(this);
                    var value = element.extractValue();
                    var formatted = $.formatNumber(value.toString(), { format: "#,###.00", locale: "us" });

                    if (isNaN(value))
                        formatted = 0;

                    element.val(formatted);

                    if (!element.is('.textbox-total'))
                        self.calculateAppropriationHours();
                });
            };
            /// <summary></summary>
            this.disableStartDate = function(){
                self.imgStartDate.remove();
                self.txtStartDate.removeClass(self.disabledCssClass);
                self.txtStartDate.addClass('TextBoxLocked');
            };
            /// <summary></summary>
            this.disableForm = function() {

                self.disableStartDate();

                self.ddlEMREProductivityCurve.attr('disabled', true);

                self.txtProjectName.attr('disabled', true);
                self.txtProjectNumber.attr('disabled', true);
                self.txtProjectManager.attr('disabled', true);
                self.txtProjectDescription.attr('disabled', true);

                self.txtEndDate.attr('disabled', true);
                self.txtEngineeringHours.attr('disabled', true);
                self.txtDirectHours.attr('disabled', true);
                self.txtIndirectHours.attr('disabled', true);
                self.txtOtherHours.attr('disabled', true);
                self.txtARRThreshold.attr('disabled', true);
                self.txtAppropriationHours.attr('disabled', true);
                self.txtAppropriationDollars.attr('disabled', true);

                self.txtProjectName.addClass(self.disabledCssClass);
                self.txtProjectNumber.addClass(self.disabledCssClass);
                self.txtProjectManager.addClass(self.disabledCssClass);
                self.txtProjectDescription.addClass(self.disabledCssClass);

                self.txtEndDate.addClass(self.disabledCssClass);
                self.txtEngineeringHours.addClass(self.disabledCssClass);
                self.txtDirectHours.addClass(self.disabledCssClass);
                self.txtIndirectHours.addClass(self.disabledCssClass);
                self.txtOtherHours.addClass(self.disabledCssClass);
                self.txtARRThreshold.addClass(self.disabledCssClass);
                self.txtAppropriationHours.addClass(self.disabledCssClass);
                self.txtAppropriationDollars.addClass(self.disabledCssClass);

                self.ibtnSaveProject.css('display', 'none');
            };
            /// <summary></summary>
            this.close = function() {
                self.clear();
                closeSubForm('<%=divProjectDialog.ClientID %>');
            };
            /// <summary></summary>
            this.clear = function() {

                self.validationController.clear();

                self.hidProjectKey.val('0');

                self.txtProjectName.val('');
                self.txtProjectNumber.val('');
                self.txtProjectManager.val('');
                self.txtProjectDescription.val('');

                self.txtStartDate.val('');
                self.txtEndDate.val('');
                self.txtEngineeringHours.val('0.00');
                self.txtDirectHours.val('0.00');
                self.txtIndirectHours.val('0.00');
                self.txtOtherHours.val('0.00');
                self.txtARRThreshold.val('0.00');
                self.txtAppropriationHours.val('0.00');
                self.txtAppropriationDollars.val('0.00');
            };
            /// <summary></summary>
            this.isOpen = function() {
                var context = $(self.controller.container)
                return (context.css('opacity') == '0') ? false : true;
            };
            /// <summary></summary>
            this.isValid = function() {

                self.validationController.clear();

                var key = parseInt(self.hidProjectKey.val());

                if (self.ddlClients.val().length == 0) {
                    var validator = self.validationController.getValidator(self.ddlClients.id());
                    validator.appendMessage("Please choose an option.");
                }

                if (self.txtProjectName.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtProjectName.id());
                    validator.appendMessage("Cannot be empty.");
                }

                if (self.hidProjectKey.val().length > 0) {

                    var key = parseInt(self.hidProjectKey.extractValue());
                    if (key > 0) 
                    {
                        if (self.ddlEMREProductivityCurve.val().toLowerCase() == 'none') {
                            var validator = self.validationController.getValidator(self.ddlEMREProductivityCurve.id());
                            validator.appendMessage("Please choose an option.");
                        }

                        if (self.txtStartDate.val().length == 0) {
                            var validator = self.validationController.getValidator(self.txtStartDate.id());
                            validator.appendMessage("Click calendar to choose a valid date.");
                        }

                        if (self.txtEndDate.val().length == 0) {
                            var validator = self.validationController.getValidator(self.txtEndDate.id());
                            validator.appendMessage("Click calendar to choose a valid date.");
                        }

                        if ((self.txtStartDate.val().length > 0) && (self.txtEndDate.val().length > 0)) {
                            var startDate = new Date(self.txtStartDate.val());
                            var endDate = new Date(self.txtEndDate.val());

                            if (startDate > endDate) {
                                var validator = self.validationController.getValidator(self.txtEndDate.id());
                                validator.appendMessage("End date must br greater than start date.");
                            }
                        }
                    }
                }

                if (self.txtARRThreshold.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtARRThreshold.id());
                    validator.appendMessage("Cannot be empty.");
                }
                else if (isNaN(self.txtARRThreshold.extractValue())) {
                    var validator = self.validationController.getValidator(self.txtARRThreshold.id());
                    validator.appendMessage("Is not a valid number.");
                }

                if (self.txtEngineeringHours.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtEngineeringHours.id());
                    validator.appendMessage("Cannot be empty.");
                }
                else if (isNaN(self.txtEngineeringHours.extractValue())) {
                    var validator = self.validationController.getValidator(self.txtEngineeringHours.id());
                    validator.appendMessage("Is not a valid number.");
                }

                if (self.txtDirectHours.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtDirectHours.id());
                    validator.appendMessage("Cannot be empty.");
                }
                else if (isNaN(self.txtDirectHours.extractValue())) {
                    var validator = self.validationController.getValidator(self.txtDirectHours.id());
                    validator.appendMessage("Is not a valid number.");
                }

                if (self.txtIndirectHours.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtIndirectHours.id());
                    validator.appendMessage("Cannot be empty.");
                }
                else if (isNaN(self.txtIndirectHours.extractValue())) {
                    var validator = self.validationController.getValidator(self.txtIndirectHours.id());
                    validator.appendMessage("Is not a valid number.");
                }

                if (self.txtOtherHours.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtOtherHours.id());
                    validator.appendMessage("Cannot be empty.");
                }
                else if (isNaN(self.txtOtherHours.extractValue())) {
                    var validator = self.validationController.getValidator(self.txtOtherHours.id());
                    validator.appendMessage("Is not a valid number.");
                }

                if (self.txtAppropriationHours.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtAppropriationHours.id());
                    validator.appendMessage("Cannot be empty.");
                }
                else if (isNaN(self.txtAppropriationHours.extractValue())) {
                    var validator = self.validationController.getValidator(self.txtAppropriationHours.id());
                    validator.appendMessage("Is not a valid number.");
                }

                if (self.txtAppropriationDollars.val().length == 0) {
                    var validator = self.validationController.getValidator(self.txtAppropriationDollars.id());
                    validator.appendMessage("Cannot be empty.");
                }
                else if (isNaN(self.txtAppropriationDollars.extractValue())) {
                    var validator = self.validationController.getValidator(self.txtAppropriationDollars.id());
                    validator.appendMessage("Is not a valid number.");
                }

                self.validationController.displayAll();

                return (self.validationController.hasErrors() == true) ? false : true;
            };
            /// <summary></summary>
            this.calculateAppropriationHours = function() {

                var engineeringHours = (+self.txtEngineeringHours.extractValue()) || 0;
                var directHours = (+self.txtDirectHours.extractValue()) || 0;
                var indirectHours = (+self.txtIndirectHours.extractValue()) || 0;
                var otherHours = (+self.txtOtherHours.extractValue()) || 0;

                var total = engineeringHours + directHours + indirectHours + otherHours;

                self.txtAppropriationHours.val(total).change();
            };
            /// <summary></summary>
            this.populateFrom = function(project) {

                self.clear();

                self.hidProjectKey.val(project.key);

                // Drop-Down
                self.ddlClients.val(project.clientKey);

                self.txtProjectName.val(project.projectName);
                self.txtProjectNumber.val(project.number);
                self.txtProjectManager.val(project.projectManager);
                self.txtProjectDescription.val(project.description);

                // Drop-Down
                self.ddlEMREProductivityCurve.val(project.productivityCurveType);

                if(project.startDate != null)
                    self.txtStartDate.val(project.startDate);

                if(project.startDate != null)
                    self.txtEndDate.val(project.endDate);

                self.txtEngineeringHours.val(project.engineeringHours);
                self.txtDirectHours.val(project.directHours);
                self.txtIndirectHours.val(project.indirectHours);
                self.txtOtherHours.val(project.otherHours);
                self.txtARRThreshold.val(project.arrThreshold);
                self.txtAppropriationHours.val(project.appropriationHours);
                self.txtAppropriationDollars.val(project.appropriationAmount);

                self.txtEngineeringHours.change();
                self.txtDirectHours.change();
                self.txtIndirectHours.change();
                self.txtOtherHours.change();
                self.txtARRThreshold.change();
                self.txtAppropriationHours.change();
                self.txtAppropriationDollars.change();
            };
        };

        return publicInstances;
    })(jQuery);
</script>