简体   繁体   English

DevExpress在ClientSide中获取ASPxComboBox的选定值

[英]DevExpress Get selected value of ASPxComboBox in ClientSide

I have a ASPxGridview that contains a field with ASPxComboBox I want to get the selected value of a ASPxComboBox and display it in a label. 我有一个ASPxGridview,其中包含一个带有ASPxComboBox的字段,我想获取ASPxComboBox的选定值并将其显示在标签中。 In my code below I just store the value on alert() message. 在下面的代码中,我只是将值存储在alert()消息中。 But all I'm getting is a "null" value.Can anyone know how to solve my problem? 但是,我得到的只是一个“空”值。有人知道如何解决我的问题吗? Thank you so much. 非常感谢。

Here's what I have so far 这是我到目前为止的

<dx:ASPxGridView ID="ASPxGridView2" OnRowDataBound="ASPxGridView2_RowDataBound" ClientInstanceName="grid" runat="server" AutoGenerateColumns="False" DataSourceID="fordtl" KeyFieldName = "chndtl_no">

<dx:GridViewDataTextColumn FieldName="product" Name="dd_product" ShowInCustomizationForm="true" VisibleIndex="7">
<SettingsHeaderFilter>
<DateRangePickerSettings EditFormatString="" />
</SettingsHeaderFilter>
<EditItemTemplate>
<dx:ASPxComboBox ID="ASPxComboBoxProduct" runat="server" ClientInstanceName="avery" AutoPostBack="true" DataSourceID="Product" EnableCallbackMode="true" TextField="pd_product">
<ClientSideEvents SelectedIndexChanged="function(s, e) { OnProductChanged(s);}"></ClientSideEvents>
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataTextColumn>

</dx:ASPxGridView>

JavaScript JavaScript的

function OnProductChanged(s, e) {
   var getProduct = document.getElementById("ASPxComboBoxProduct");
   alert(getProduct);
}

Replace this in you aspx code - 替换为您的aspx代码-

<ClientSideEvents SelectedIndexChanged="function(e,s) { OnProductChanged();}">
</ClientSideEvents>

Replace your javascript function from this and check. 从此处替换您的javascript函数并进行检查。

function OnProductChanged(e, s)
{
      var selectedValue = e.lastSuccessValue;
      alert(selectedValue);
}

Change your SelectedIndexChanged JS handler to this: 将您的SelectedIndexChanged JS处理程序更改为此:

function OnProductChanged(s, e) {
   var getProduct = s.GetValue();
   alert(getProduct);
}

But if you are to change the label stored elsewhere with a newly selected data, how would you know for which particular grid's row the combobox value was changed? 但是,如果要使用新选择的数据来更改存储在其他位置的标签,您怎么知道组合框值针对哪个特定的网格行进行了更改?

Comment if this is your requirement, because DX has a way to handle this case. 如果这是您的要求,请发表评论,因为DX可以处理这种情况。

UPDATE: To be able to differentiate in which row the combobox from EditItemTemplate had selected index changed you need to assign a unique ClientInstanceName for each such combobox. 更新:为了能够区分来自EditItemTemplate的组合框已更改索引的哪一行,您需要为每个此类组合框分配一个唯一的ClientInstanceName。 This ClientInstanceName should contain the row index part, for example: avery_01 . 此ClientInstanceName应该包含行索引部分,例如: avery_01

To be able to accomplish this you need to handle the OnInit event for EACH combobox being displayed and assign ClientInstanceName there like for example the following: 为此,您需要处理所显示的每个EACH组合框的OnInit事件,并在其中分配ClientInstanceName,例如,如下所示:

...
<EditItemTemplate>
    <dx:ASPxComboBox ID="ASPxComboBoxProduct" runat="server" 
       OnInit="ASPxComboBoxProduct_Init" AutoPostBack="true" DataSourceID="Product" 
       EnableCallbackMode="true" TextField="pd_product">
    </dx:ASPxComboBox>
</EditItemTemplate>
...

Note I have removed the ClientInstanceName attribute and ClientSideEvents element. 注意我删除了ClientInstanceName属性和ClientSideEvents元素。

Same should be done for your ASPxLabel placed inside ASPxGridView DataItemTemplate: 对于放置在ASPxGridView DataItemTemplate中的ASPxLabel,应该执行相同的操作:

...
<dx:GridViewDataColumn Caption="Label Column Caption">
<DataItemTemplate>
   <ASPxLabel ID="ASPxLabel" runat="server" OnInit="ASPxLabel_Init" />
</DataItemTemplate>
</dx:GridViewDataColumn>
...

Then in codebehind you add: 然后在代码后面添加:

// Handle OnInit for each combobox being displayed
protected void ASPxComboBoxProduct_Init(object sender, EventArgs e) 
{     
    ASPxComboBox dvxCombo = (ASPxComboBox)sender;
    GridViewDetailRowTemplateContainer templateContainer =
                   (GridViewDetailRowTemplateContainer)ASPxGridView2.NamingContainer;
    dvxCombo.ClientInstanceName = string.Format("avery_{0}",
                                             templateContainer.VisibleIndex);

    // below client instance name should set for the relevant label in the labels 
    //own OnInit handler below
    string targetLabelClientInstanceName = string.Format("label_{0}",
                                             templateContainer.VisibleIndex);
    // Note, we're passing the ASPxLabel's ClientInstanceName as a third
    // param to the SelectedIndexChanged event handler.
    dvxCombo.ClientSideEvents.SelectedIndexChanged = 
        string.Format("function(s, e) {{ OnProductChanged(s, e, {0}); }}",
        targetLabelClientInstanceName);        
}


// Handle OnInit for each ASPxLabel which you will be updating with the
// selected combobox value
protected void ASPxLabel_Init(object sender, EventArgs e) 
{
    ASPxLabel dvxLabel = (ASPxLabel)sender;
    GridViewDetailRowTemplateContainer templateContainer =
                   (GridViewDetailRowTemplateContainer)ASPxGridView2.NamingContainer;
    dvxLabel.ClientInstanceName = string.Format("label_{0}",
                                             templateContainer.VisibleIndex);    
}

Then update you JavaScript handler like this: 然后像这样更新您的JavaScript处理程序:

function OnProductChanged(s, e, label) {
   var getProduct = s.GetValue();
   alert(getProduct);

   label.SetText(getProduct);
}

Note, above the label is not just a string or jQuery object, it's real DevExpress client side object for the ASPxLabel defined in another grid column on the same row as the current combobox for which this event will be handled. 请注意, label上方不仅是字符串或jQuery对象,它还是ASPxLabel的真正DevExpress客户端对象,该对象在与当前组合框相同的行的另一网格列中定义,此事件将针对该对象进行处理。 Therefore it should all DX client side methods like SetText() and GetText. 因此,它应该使用所有DX客户端方法,例如SetText()和GetText。

So the above approach has been verified to work. 因此,以上方法已被验证可以工作。 Note, all code snippets I've typed in directly without compiling. 请注意,我直接输入的所有代码段都未经编译。 But I trust you should be able to glue them all around. 但是我相信您应该能够将它们粘合在一起。

Similar cases: 类似情况:

  1. Checking if a aspxgridview Master-Detail has any row checked in Client Side 检查aspxgridview主详细信息是否在客户端中检查了任何行

  2. https://www.devexpress.com/Support/Center/Question/Details/Q372498 https://www.devexpress.com/Support/Center/Question/Details/Q372498

HTH HTH

Label will be updated while combobox changed event but it will lost it's value on post back. 标签会在组合框更改事件时更新,但在回发时将丢失其值。 Avoid post back on combobox index change then label will be updated as you need. 避免回传组合框索引更改,否则标签将根据需要进行更新。

AutoPostBack="false"

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

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