This is a migrated thread and some comments may be shown as answers.

Cannot get Selected Value of Dropdownlist Added to Grid in Code behind

1 Answer 8 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Beryl
Top achievements
Rank 1
Beryl asked on 19 Sep 2019, 05:59 PM
I added a dropdownlist to an autogenerated grid in code behind, however, I am unable to get the selected value when the row is updated.  I add the dropdownlist as follows:

protected void grdAssetImport_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem edit = (GridEditableItem)e.Item;
 
        TextBox txt = (TextBox)edit["AssetTypeName"].Controls[0];
        txt.Visible = false;
        DropDownList rddl = new DropDownList();
        PortalView.LookupListBO list = LookupListBA.LookupList_GetByKey(DB_Context, "SITE_ASSETTYPE_LIST", UtilityBA.IsActiveChoice.Active);
        List<PortalView.LookupListItemBO> oList = LookupListBA.LookupListItem_GetList_ByLookupListId(DB_Context, list.LookupListId, (Guid)Current.Employee.SiteId);
        var AssetList = oList.Select(l => new { AssetTypeName = l.Name });

        rddl.ID = "ddlAssetTypeName";

        rddl.AutoPostBack = false;
        rddl.DataSource = AssetList;
        rddl.DataTextField = "AssetTypeName";
        rddl.DataValueField = "AssetTypeName";
        rddl.DataBind();
 
        edit["AssetTypeName"].Controls.Add(rddl);
    }
}

 

 

I try to use the follow update command, but neither attempt to capture the dropdownlist works and when it gets to the actual column it only sees the textbox in the column and never finds the dropdownlist at all:

protected void grdAssetImport_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem editedItem = e.Item as GridEditableItem;
    GridEditManager editMan = editedItem.EditManager;
    foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
    {
        GridEditableItem editableItem = e.Item as GridEditableItem;
        DropDownList ddl = editableItem.FindControl("ddlAssetTypeName") as DropDownList;
        if(ddl != null)
        {
            string assetType = ddl.SelectedValue;
 
        }
 
            DropDownList ddl2 = editableItem["AssetTypeName"].Controls[0] as DropDownList;
            if(ddl2 != null)
            {
                string assetType = ddl.SelectedValue;
            }
 
 
        if (column is IGridEditableColumn)
        {
            IGridEditableColumn editableCol = (column as IGridEditableColumn);
            if (editableCol.IsEditable)
            {
                IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);
                string editorText = "unknown";
                object editorValue = null;
                if (editor is GridTextColumnEditor)
                {
                    editorText = (editor as GridTextColumnEditor).Text;
                    editorValue = (editor as GridTextColumnEditor).Text;
                }
                if (editor is GridBoolColumnEditor)
                {
                    editorText = (editor as GridBoolColumnEditor).Value.ToString();
                    editorValue = (editor as GridBoolColumnEditor).Value;
                }
                if (editor is GridDropDownColumnEditor)
                {
                    editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " +
                     (editor as GridDropDownColumnEditor).SelectedValue;
                    editorValue = (editor as GridDropDownColumnEditor).SelectedValue;
                }
                try
                {
                    DataRow[] changedRows = this.AssetGridDataSource.Select("Id = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Id"].ToString());
                    changedRows[0][column.UniqueName] = editorValue;
                    this.AssetGridDataSource.AcceptChanges();
                    GetSearchColumns();
                }
                catch (Exception ex)
                {
                    // Label1.Text = "<strong>Unable to set value of column '" + column.UniqueName + "'</strong> - " + ex.Message;
                    e.Canceled = true;
                    break;
                }
            }
        }
    }
}

 

Any assistance is greatly appreciated as I am on day 4 of trying to get this working.

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 24 Sep 2019, 11:43 AM

Hello Beryl,

 

I've already provided a reply to your query in the other forum thread:

https://www.telerik.com/forums/radgrid-auto-generated-edit-mode-drop-down-list-selection-not-binding-to-database#HiPNm7wf2UeUIE0oNcyQYA

 

In addition to the samples and suggestions there, you can try these 2 things:

 

1. Ensure that the EditMode of the MasterTableView tag is not set to Batch

2. Send us the aspx mark-up definition so we can see the DropDownList code - try removing settings like AllowCustomText or EnableLoadOnDemand

3. Send us an entire runnable sample as suggested in the other forum post so we modify it accordingly to fix the issues and send it back to us.

 

Regards,
Eyup
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
General Discussions
Asked by
Beryl
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or