DevExpress:如何获取控件客户端的实例并访问其客户端成员?

发布于 2024-12-11 00:18:22 字数 201 浏览 2 评论 0原文

我有一个来自 DexExpress 的 DateEdit 控件,需要使用 Javascript 从中获取日期值。从概念上讲,我正在寻找这样的东西:

var d = $("dpEndDate").GetDate();

他们的 API 参考表明 .GetDate() 是一个成员,但我只需要知道如何获取包含此成员的对象的客户端实例。

I have a DateEdit control from DexExpress and need to get the date value from it using Javascript. Conceptually, I am looking for something like this:

var d = $("dpEndDate").GetDate();

Their API reference indicates that .GetDate() is a member, but I just need to know how to acquire a client-side instance of the object that contains this member.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蛮可爱 2024-12-18 00:18:22

必须指定 ClientInstanceName 属性(对于 ASP.NET WebForms)和“Name”属性(对于 ASP.NET MVC)以启用控件的客户端编程对象:

<dx:ASPxDateEdit ... ClientInstanceName="de">
</dx:ASPxDateEdit>

var date = de.GetDate();

It is necessary to specify the ClientInstanceName property (for the ASP.NET WebForms) and the “Name” property (for the ASP.NET MVC) to enable control’s client-side programmatic object:

<dx:ASPxDateEdit ... ClientInstanceName="de">
</dx:ASPxDateEdit>

var date = de.GetDate();
匿名。 2024-12-18 00:18:22

如果您正在开发基于多个需要能够相互交互的 DevExpress 控件的用户控件,并且您将在给定页面上拥有多个用户控件实例,那么最好使用 DevExpress ' window.aspxGetControlCollection() 与 DevExpress 控件的 ClientID 如下所示:这样

window.aspxGetControlCollection().elements[clientID];

做的缺点是,您通常必须在后面的代码中构建客户端事件处理程序,以便可以轻松访问 DevExpress 控件的 ClientID,例如所以。

ASPxGridView.ClientSideEvents.EndCallback = "function(s,e) { window.aspxGetControlCollection().elements['" + SomeOtherDevExpressControl.ClientID + "'].PerformCallback('callbackArg'); }";

或者创建一个 JS 对象来处理客户端操作并重写渲染方法以添加一些脚本来创建具有所需 ClientID 的实例。

JS 实例:

window.ClientControl = function(SomeOtherDevExpressControlClientId)
{
    this.SomeOtherDevExpressControlClientId = SomeOtherDevExpressControlClientId;
    this.SomeOtherDevExpressControl() = function(){ return window.aspxGetControlCollection().elements[this.SomeOtherDevExpressControlClientId]; }
    this.GridEndCallback = function(s,e) { this.SomeOtherDevExpressControl().PerformCallback('callbackArg'); }
}

渲染覆盖:

proteced override void Render(HtmlTextWriter writer)
{
    base.Render(writer);
    writer.WriteLine();
    writer.WriteBeginTag("script");
    writer.WriteAttribute("type", "text/javascript");
    writer.Write(">");
    writer.WriteLine("window['" + UserControl.ClientInstanceName+ "'] = new ClientControl('" + SomeOtherDevExpressControl.ClientID + "');");
    writer.WriteEndTag("script");

}

使用第二种方法时,假设您将公共字符串属性 ClientInstanceName 添加到用户控件,并在标记中为其指定了一个值。

If you're developing a user control based on multiple DevExpress controls that need to be able to interact with each other and you're going to have more than one instance of your user control on a given page, then it is best to use DevExpress' window.aspxGetControlCollection() with the ClientID of the DevExpress control like so:

window.aspxGetControlCollection().elements[clientID];

The downside of this is that you've typically got to build client side event handlers in the code behind where you've got easy access to the DevExpress control ClientIDs like so.

ASPxGridView.ClientSideEvents.EndCallback = "function(s,e) { window.aspxGetControlCollection().elements['" + SomeOtherDevExpressControl.ClientID + "'].PerformCallback('callbackArg'); }";

Either that or create a JS object to handle your client side actions and override the render method to add a some script to create this instance with the needed ClientIDs.

JS Instance:

window.ClientControl = function(SomeOtherDevExpressControlClientId)
{
    this.SomeOtherDevExpressControlClientId = SomeOtherDevExpressControlClientId;
    this.SomeOtherDevExpressControl() = function(){ return window.aspxGetControlCollection().elements[this.SomeOtherDevExpressControlClientId]; }
    this.GridEndCallback = function(s,e) { this.SomeOtherDevExpressControl().PerformCallback('callbackArg'); }
}

Render Override:

proteced override void Render(HtmlTextWriter writer)
{
    base.Render(writer);
    writer.WriteLine();
    writer.WriteBeginTag("script");
    writer.WriteAttribute("type", "text/javascript");
    writer.Write(">");
    writer.WriteLine("window['" + UserControl.ClientInstanceName+ "'] = new ClientControl('" + SomeOtherDevExpressControl.ClientID + "');");
    writer.WriteEndTag("script");

}

With the second method there's an assumption that you added a public string attribute ClientInstanceName to your user control and have given that a value in the markup.

三生殊途 2024-12-18 00:18:22

尝试:

var d = $("#<%=dpEndDate %>").GetDate();

原因是 ASP.NET ClientID 由控件树中的命名容器限定,因此最终可能会类似于“ctl1_ct12_ct12_dpEndDate”。

您可以让 ASP.NET 4.0 不使用此分层命名,但默认情况下它是启用的。

Try:

var d = $("#<%=dpEndDate %>").GetDate();

The reason is that ASP.NET ClientIDs are qualified by their naming-container in the control tree, so may end up as something like "ctl1_ct12_ct12_dpEndDate".

You can get ASP.NET 4.0 to not use this hierarchical naming, but it's on by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文