首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scriptcontrol -绑定客户端和服务器属性

Scriptcontrol -绑定客户端和服务器属性
EN

Stack Overflow用户
提问于 2012-05-14 15:40:28
回答 1查看 717关注 0票数 2

是否可以在Scriptcontrol中绑定客户端和服务器端的属性,这样当我在javascript中设置属性时,更改也将在后台代码中可见,当我在后台代码中设置属性时,更改将在javascript中可见?

我不能让它像上面那样工作-它是最初设置的,当我在声明scriptcontrol的地方设置属性时,但当我稍后更改它时,它仍然和以前一样……

编辑:我尝试为我们的ASP.NET应用程序中的长回发做一个ProgressBar。我已经尝试了很多选择,但没有一个对我有效。我想在后面的代码中设置进度值,并在长任务回发期间在视图中更新它。

ScriptControl的代码: C#:

代码语言:javascript
复制
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:

代码语言: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();

我非常感谢任何实现这个进度条的方式……

EN

回答 1

Stack Overflow用户

发布于 2012-05-14 15:47:01

就我个人而言,我经常使用隐藏字段。请记住,隐藏字段是不安全的,并且可能有其他缺陷,因为它们实际上并不隐藏它们的值,只是不显示它。

ASPX标记

代码语言:javascript
复制
<asp:HiddenField ID="hiddenRequest" runat="server" ClientIDMode="Static" />

背后的ASPX.CS代码

代码语言:javascript
复制
    public string HiddenRequest
    {
        set
        {
            hiddenRequest.Value = value;
        }
        get
        {
            return hiddenRequest.Value;
        }
    }

页面JAVASCRIPT (使用jQuery)

代码语言:javascript
复制
$('#hiddenRequest').val('MyResult');

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10579248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档