2014-09-02 37 views
1

我想在我的RadGrid中设置GridDropDownColumn的值。 请注意,我的GridDropDownColumn不在模板中,只是<columns>的一部分。设置GridDropDownColumn的SelectedValue

我使用InsertCommand事件来做到这一点,因为我的整个实验都是围绕着操作那里的数据。

前端:

<telerik:RadGrid ID="RadGrid1" runat="server"> 
    <MasterTableView> 
    <Columns> 
     <telerik:GridDropDownColumn DataSourceID="MySource" DataField="RowId" UniqueName="RowId" ListValueField="id" ListTextField="Name" SortExpression="RowId" HeaderText="RowId" /> 
    </Columns> 
    </MasterTableView> 
</telerik:RadGrid> 

后端:

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e) { 
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode) { 
    GridEditFormItem editItem = (GridEditFormItem)e.Item; 
    DropDownList list = (DropDownList)editItem["GridDropDownColumn"].Controls[0]; 
    list.SelectedValue = HiddenFieldIdToSave.Value; 
    } 
} 

这是给我虽然是相当恶劣的错误。

Unhandled exception at line 15, column 16485 in http://localhost:55555/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=RadScriptManager1_TSM&compress=1&_TSM_CombinedScripts_=;;System.Web.Extensions,+Version=4.0.0.0,+Culture=neutral,+PublicKeyToken=31321323135:en-N:5924cf72drgdrg-a608a92942c5:ea597d4b:b25378d2;Telerik.Web.UI,+Version=2014.2.724.45,+Culture=neutral,+PublicKeyToken=121fae781awdawggcvb31-d2d2285652a6:fghfghfghf:58366029 

0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Cannot find cell bound to column with unique name 'GridDropDownColumn' 

回答

0

我发现为什么我收到此错误。

我有2个下拉列表,它们都连接在相同的数据源上,这导致了一个错误的值被传递,这导致我的插入/更新DataObjectMethod发起错误。

另外我的例子是看着错误的UniqueName。

0

在后端C#中,你要访问一个名为GridDropDownColumn列,而是你应该使用该行的UniqueName属性,您已设置为RowId。试试这个:

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e) 
{ 
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
    { 
     GridEditFormItem editItem = (GridEditFormItem)e.Item; 

     // Use the column's unique name as the accessor: 
     DropDownList list = (DropDownList)editItem["RowId"].Controls[0]; 

     list.SelectedValue = HiddenFieldIdToSave.Value; 
    } 
} 
+0

感谢您发现,我改变了它,所以我正确地针对我的RowId,但仍然发生同样的错误。 – MichaelA 2014-09-03 06:58:06

+0

你应该在你的问题中更新你的代码。你如何调用RadGrid1_InsertCommand和/或解雇InsertCommand事件? – DanM7 2014-09-03 12:29:45

+0

嗨DanM,我发现为什么即使使用你的建议后,我得到这个错误。我有2个下拉列表,它们都连接在同一个数据源上,这导致了一个错误的值被传递,这导致我的插入/更新DataObjectMethod触发错误。 我现在已经修复它,并且没有问题,谢谢你的帮助。 – MichaelA 2014-09-04 09:03:07

相关问题