简体   繁体   中英

Get value of DevExpress ASPxComboBox via jQuery

I have a dynamically generated ASPxComboBox on my page that is comprised of several ListEditItem objects. I may have something like this as a source for the 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). Stepping through this code with in debug mode verifies that the ListEditItems are added with the correct values. However, when I do a View Source on the generated HTML, the output looks like this:

<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:)

<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? 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?

2) Is there a client-side method I can call on the control that will get that information for me? 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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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