在RadGrid中使用就地编辑时,RadComboBox SelectedIndexChanged事件未触发

时间:2011-12-21 21:46:23

标签: c# asp.net events telerik-grid radcombobox

我在使用类文件构建的RadGrid上使用就地编辑。一切都运行良好,除了我遇到一个问题,当网格处于编辑模式时,SelectedIndexChanged事件没有触发。有什么想法吗?

private void RadGrid_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        try
        {
            if ((e.Item as GridDataItem) == null) { return; }
            ((RadNumericTextBox) (e.Item as GridDataItem)["Percentage"].Controls[0]).Width = Unit.Pixel(75);
            ((TextBox) (e.Item as GridDataItem)["Code"].Controls[0]).Width = Unit.Pixel(75);

            RadComboBox _participantList = (e.Item as GridEditableItem)["ID"].Controls[0] as RadComboBox;
            if (null == _participantList) { return; }

            _participantList.Width = Unit.Pixel(120);
            _participantList.DataValueField = "ID";
            _participantList.DataTextField = "ID";
            _participantList.AutoPostBack = true;
            _participantList.DataSource = MAASBaseInterface.ParticipantAPI.GetParticipants();
            _participantList.DataBind();
            _participantList.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(_participantList_SelectedIndexChanged);

            if (!(e.Item.DataItem is GridInsertionObject))
                _participantList.SelectedValue = ((Participant) (e.Item.DataItem)).ID.ToString();
            if (e.Item.DataItem is GridInsertionObject)
                _participantList.EmptyMessage = "-- Select --";
        }
        catch (Exception ex)
        {

            string _ex = ex.Message;
        }
    }
} 

void _participantList_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    //first reference the edited grid item through the NamingContainer attribute
    GridEditableItem editedItem = (sender as RadComboBox).NamingContainer as GridEditableItem;
    int _selectedValue = Convert.ToInt32((editedItem["ID"].Controls[0] as RadComboBox).SelectedValue);
    ParticipantList _participants = MAASBaseInterface.ParticipantAPI.GetParticipants();
    Participant _participant = _participants.Where(a => a.ID == _selectedValue) as Participant;
    RadTextBox _code = editedItem["Code"].Controls[0] as RadTextBox;
    _code.ReadOnly = false;
    _code.Text = _participant.Code;
}

2 个答案:

答案 0 :(得分:1)

您需要一个设置为CommandName="Select"的按钮。没有它,事件不会触发。这可能是问题吗?

link提供了更多详细信息

修改

问题可能是下拉列表被动态添加到网格中,因此每次绑定行时都需要添加事件。根据我的经验,radGrid和GridView在事件模型方面的工作方式相同,因此SO answer可能会将您排除在外。祝你好运 - 我最初的想法是,这不可能没有,但可能有前进的方向

答案 1 :(得分:1)

问题是我只设置了RadComboBox的Value属性而不是Text属性。即使在编辑模式下RadComboBox中显示文本值,显然它显示的是Value属性。一旦它被设置,它就像它应该做的那样开始回复。

                if (!(e.Item.DataItem is GridInsertionObject))
                {
                    _participantList.SelectedValue =
                        ((ReinsuranceAgreementParticipant) (e.Item.DataItem)).LegacyReinsurerID.ToString();
                    // I added this line
                    _participantList.Text = ((ReinsuranceAgreementParticipant)(e.Item.DataItem)).LegacyReinsurerID.ToString();
                }
                if (e.Item.DataItem is GridInsertionObject)
                    _participantList.EmptyMessage = "Select Reinsurer";