【问题标题】:How to get value from UserControl of Telerik RadGrid inside DataBound Event and Insert Event如何在 DataBound 事件和插入事件中从 Telerik RadGrid 的 UserControl 获取值
【发布时间】:2016-09-07 15:18:14
【问题描述】:

我正在使用 Telerik 网格,这里我将 UserControl 放置在 GridTemplateColumn 内的 RadGrid 中,类似这样

    <telerik:GridTemplateColumn UniqueName="HardCoded" HeaderText="HardCoded" AllowFiltering="true"
                DataField="AccountDesc">
                <ItemTemplate>
                    <asp:Label ID="HardCoded" runat="server" Text='<%# Bind("HardCoded") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <UserCtrl:UserCtrl runat="server" ID="lbl" />
                </EditItemTemplate>
                <InsertItemTemplate>
                  <UserCtrl:UserCtrl runat="server" ID="lbl" />                        
                </InsertItemTemplate>
                </telerik:GridTemplateColumn>

这里

这是我的 UserControl 组合代码

<telerik:RadComboBox runat="server" ID="HardCoded" >
    <Items>
        <telerik:RadComboBoxItem Value="1" Text="Member" Selected="true" />
        <telerik:RadComboBoxItem Value="2" Text="Employee" />
        <telerik:RadComboBoxItem Value="3" Text="All" />
    </Items>
</telerik:RadComboBox>

在数据绑定事件中,如果我按下网格上的编辑按钮,我只想设置选定值。这是我的数据绑定事件代码

  protected void RadGrid1_OnItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode)
            {
                   GridEditableItem editableItem = e.Item as GridEditableItem;
                    RadComboBox HardCoded = editableItem["HardCoded"].Controls[0] as RadComboBox;
                  // I've also tried 
 RadComboBox HardCoded = (RadComboBox)e.Item.FindControl("HardCoded");//it also didn't work

            }
    }

在这里我总是得到对象引用未设置为对象实例的错误,因为它无法找到 My UserControl RadCombo

同样,如果我想在 Insert 事件中获取值,我将无法找到我的组合 这是我的代码

    protected void RadGrid1_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
  string HardCoded = ((RadComboBox)ii["HardCoded"].FindControl("HardCoded")).SelectedValue;
   }

未设置对象引用的类似异常

【问题讨论】:

    标签: c# asp.net user-controls telerik radgrid


    【解决方案1】:

    不要在OnItemDataBound事件处理程序中使用GridEditableItem,而是使用GridEditFormItem,如下所示:

    protected void RadGrid1_OnItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//the grid is about to Edit.
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            RadComboBox combo = (RadComboBox)item.FindControl("HardCoded");
            combo.SelectedValue = "Something";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多