简体   繁体   English

通过jQuery获取DevExpress ASPxComboBox的价值

[英]Get value of DevExpress ASPxComboBox via jQuery

I have a dynamically generated ASPxComboBox on my page that is comprised of several ListEditItem objects. 我的页面上有一个动态生成的ASPxComboBox,它由几个ListEditItem对象组成。 I may have something like this as a source for the ASPxComboBox: 我可能有类似这样的内容作为ASPxComboBox的来源:

foreach (KeyValuePair<string, int> row in list)
{
    ListEditItem item = new ListEditItem(row.Key.ToString(), 
        row.Value.ToString());
    ddl.Items.Add(item);
}

In this example, row.Value contains values such as "1431, 5415, 12897, 3491", etc (ie random database ID values). 在此示例中,row.Value包含诸如“ 1431、5415、12897、3491”之类的值(即,随机数据库ID值)。 Stepping through this code with in debug mode verifies that the ListEditItems are added with the correct values. 在调试模式下单步执行此代码可验证是否已为ListEditItems添加了正确的值。 However, when I do a View Source on the generated HTML, the output looks like this: 但是,当我在生成的HTML上执行“查看源代码”时,输出看起来像这样:

<option value="1">4DAES</option>
<option value="2">631</option>
<option value="3">ACB</option>
<option value="4">ABDHP</option>

I would expect it to look instead, like this (note the change in option values to correspond to the database ID's I provided above:) 我希望它看起来像这样(请注意选项值的更改以对应于我上面提供的数据库ID :)

<option value="1431">4DAES</option>
<option value="5415">631</option>
<option value="12897">ACB</option>
<option value="3491">ABDHP</option>

My question is this: 我的问题是这样的:

Using jQuery, how can I get the ACTUAL value of the ID that I'm looking for? 使用jQuery,我如何获取要查找的ID的ACTUAL值? I see two possible options for doing this: 我看到这样做的两个可能的选择:

1) Is there a setting on the ASPxComboBox control that tells it to render the correct values and not an incrementing ID? 1)ASPxComboBox控件上是否有设置告诉它呈现正确的值,而不是递增ID?

2) Is there a client-side method I can call on the control that will get that information for me? 2)是否可以在控件上调用一个客户端方法来为我获取该信息? If so, can you provide an example? 如果可以,您可以举个例子吗?

I figured out the answer: 我想出了答案:

var clientInstanceName = this.data('clientinstancename');
var combo = ASPxClientControl.GetControlCollection().GetByName(clientInstanceName);
return combo.GetSelectedItem().value;

The ASPxComboBox control has a "ClientInstanceName" property that allows you to assign a name to this object so you can manipulate it client-side. ASPxComboBox控件具有“ ClientInstanceName”属性,该属性允许您为该对象分配名称,以便可以在客户端进行操作。 Since I'm dynamically creating these controls, I don't know the name ahead of time so I add it as a "data-" attribute like so: 由于我是动态创建这些控件的,因此我不提前知道该名称,因此我将其添加为“ data-”属性,如下所示:

ddl.ClientInstanceName = fld.FieldName;
ddl.Attributes.Add("data-clientinstancename", fld.FieldName);

Bottom line is, define a ClientInstanceName property for the control, then use the code in the first snippet to get at the value you need. 最重要的是,为控件定义ClientInstanceName属性,然后使用第一个代码段中的代码获得所需的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM