2

Is it possible to bind properties on the client and server side in Scriptcontrol, so when I set property in javascript, change will be visible also in code behind and when I set property in code behind, change will be visible in javascript?

I can't get it work like above - it is set initially, when I set property where scriptcontrol is declared, but when I change it later it is still the same as before...

EDIT: I try to do a ProgressBar for long postbacks in our ASP.NET application. I have tried many options but none works for me... I want to set progress value in code behind and has it updated in view during long task postback.

Code for ScriptControl: C#:

public class ProgressBar : ScriptControl
{
    private const string ProgressBarType = "ProgressBarNamespace.ProgressBar";
    public int Value { get; set; }
    public int Maximum { get; set; }

    protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()
    {
        this.Value = 100;
        this.Maximum = 90;
        var descriptor = new ScriptControlDescriptor(ProgressBarType, this.ClientID);

        descriptor.AddProperty("value", this.Value);
        descriptor.AddProperty("maximum", this.Maximum);

        yield return descriptor;
    }

    protected override IEnumerable<ScriptReference> GetScriptReferences()
    {
        yield return new ScriptReference("ProgressBar.cs.js");          
    }
}

Javascript:

Type.registerNamespace("ProgressBarNamespace");

ProgressBarNamespace.ProgressBar = function(element) {
    ProgressBarNamespace.ProgressBar.initializeBase(this, [element]);
    this._value = 0;
    this._maximum = 100;
};

ProgressBarNamespace.ProgressBar.prototype = {
    initialize: function () {
        ProgressBarNamespace.ProgressBar.callBaseMethod(this, "initialize");
        this._element.Value = this._value;
        this._element.Maximum = this._maximum;

        this._element.show = function () {
            alert(this.Value);
        };
    },
    dispose: function () {
        ProgressBarNamespace.ProgressBar.callBaseMethod(this, "dispose");
    },
    get_value: function () {
        return this._value;
    },
    set_value: function (value) {
        if (this._value !== value) {
            this._value = value;
            this.raisePropertyChanged("value");
        }
    },
    get_maximum: function () {
        return this._maximum;
    },
    set_maximum: function (value) {
        if (this._maximum !== value) {
            this._maximum = value;
            this.raisePropertyChanged("maximum");
        }
    }
};

ProgressBarNamespace.ProgressBar.registerClass("ProgressBarNamespace.ProgressBar", Sys.UI.Control);
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

I'll appreciate any way to implement this progress bar...

4

1 回答 1

1

就个人而言,我经常使用隐藏字段来执行此操作。请记住,隐藏字段不安全并且可能有其他缺点,因为它们实际上并没有隐藏它们的值,只是简单地不显示它。

ASPX 标记

<asp:HiddenField ID="hiddenRequest" runat="server" ClientIDMode="Static" />

ASPX.CS 后面的代码

    public string HiddenRequest
    {
        set
        {
            hiddenRequest.Value = value;
        }
        get
        {
            return hiddenRequest.Value;
        }
    }

页面 JAVASCRIPT(使用 jQuery)

$('#hiddenRequest').val('MyResult');

这样,我可以使用一个变量访问相同的字段,从客户端和服务器端访问。

于 2012-05-14T07:47:01.477 回答