c# - Telerik:无法将下拉列表的选定值添加到自动生成的 Radgrid
问题描述
我在后面的代码中向自动生成的 RadGrid 添加了一个下拉列表,但是,当行更新时,我无法获取选定的值。我添加下拉列表如下:
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);
}
}
我尝试在 UpdateCommand 中检索选定的值,但没有成功:
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;
}
}
}
}
}
它永远不会找到下拉列表,并且总是只看到列的 GridTextColumnEditor 所以我得到的唯一值是编辑模式之前的原始值。
非常感谢任何帮助!
解决方案
项目不会每次都绑定,因此 ItemDataBound 事件的触发次数比您想象的要少。要在每次创建项目时添加控件,您需要改用 ItemCreated 事件。
我已经测试了以下并且有效。
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode)
{
GridEditableItem edititem = e.Item as GridEditableItem;
DropDownList rddl = new DropDownList();
edititem["AssetTypeName"].Controls[0].Visible = false;
rddl.ID = "ddlAssetTypeName";
rddl.AutoPostBack = false;
rddl.DataSource = Enumerable.Range(1, 3).Select(x => "Item" + x).ToList();
rddl.DataBind();
edititem["AssetTypeName"].Controls.Add(rddl);
}
}
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
DropDownList ddl = editedItem["AssetTypeName"].FindControl("ddlAssetTypeName") as DropDownList;
string selectedValue = ddl.SelectedValue;
}
推荐阅读
- regex - sed 使用正则表达式进行多行选择
- angular - ASP.NET CORE Angular 6 添加第三方组件
- jquery - 分页数据表后,自定义类在输入类型复选框中不起作用
- python - Python 代理抓取:url 超出了最大重试次数(仅在 Mac 上,Windows 工作正常)
- list - Unity3D:当count为1时,GridLayout不断在最后一个元素下添加1个元素
- ansible - ansible-playbook 计时器仅在某些节点上 10 秒后过期,但在其他节点上工作正常
- javascript - 从本地 JavaScript 应用程序调用 Python 脚本
- mysql - 基于JOIN的列复制MySQL SUM
- info.plist - Info.plist 中未定义的字典
- php - assert() 不会自动评估代码