获取Telerik RadGrid中已编辑项目的值

时间:2013-01-09 20:39:59

标签: asp.net telerik radgrid

ASPX:

<telerik:RadGrid ID="ctlItemsGrid" runat="server" DataKeyNames="Id" AllowPaging="true" AllowSorting="true" GridLines="None" Skin="Windows7" EnableViewState="true">
    <MasterTableView AutoGenerateColumns="false" AllowPaging="true" DataKeyNames="Id,WorkflowStatus" NoMasterRecordsText="No items exist in the database." EnableViewState="true">
        <SortExpressions>
            <telerik:GridSortExpression FieldName="Id" SortOrder="Descending" />
        </SortExpressions>
        <PagerStyle Mode="NextPrevAndNumeric" />
        <Columns>            
            <telerik:GridNumericColumn ColumnEditorID="ctlColumnId" HeaderText="ID" DataField="Id" DecimalDigits="0" DataType="System.Int32" NumericType="Number" ReadOnly="true"></telerik:GridNumericColumn>
            <telerik:GridDropDownColumn UniqueName="ctlColumnGridWorkflow" ColumnEditorID="ctlColumnWorkflow" HeaderText="Workflow" DataField="WorkflowStatus" DropDownControlType="DropDownList"></telerik:GridDropDownColumn>
            <telerik:GridEditCommandColumn UniqueName="ctlColumnGridEdit" CancelText="Cancel" EditText="Edit"></telerik:GridEditCommandColumn>
            <telerik:GridButtonColumn UniqueName="ctlColumnGridDelete" CommandName="Delete" CommandArgument="" Text="Remove"></telerik:GridButtonColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:

public partial class administration_modules_item_Default : ViewPageBase<IItemAdminPresenter, IItemAdminView>, IItemAdminView
{
    private IEnumerable<WorkflowStatus> _workflowStatuses;
    private IEnumerable<IItemDbItem> _items;

    protected override void PreloadView()
    {
        this.ctlItemsGrid.ItemDataBound += new Telerik.Web.UI.GridItemEventHandler(ctlItemsGrid_ItemDataBound);
        this.ctlItemsGrid.ItemCommand += new GridCommandEventHandler(ctlItemsGrid_ItemCommand);
        //this.ctlItemsGrid.ItemUpdated += new GridUpdatedEventHandler(ctlItemsGrid_ItemUpdated);
        this.ctlItemsGrid.UpdateCommand += new GridCommandEventHandler(ctlItemsGrid_UpdateCommand);
    }

    protected override void PostLoadView()
    {
        // Doesn't work, values are all empty strings
        //foreach (GridEditableItem editItem in ctlItemsGrid.EditItems)
        //{
        //    Dictionary<string, string> newValues = new Dictionary<string, string>();
        //    ctlItemsGrid.MasterTableView.ExtractValuesFromItem(newValues, editItem);
        //    IItemDbItem w = (IItemDbItem)editItem.DataItem;
        //    if (!string.IsNullOrWhiteSpace(newValues["WorkflowStatus"]))
        //    {
        //        w.WorkflowStatus = (WorkflowStatus)Enum.Parse(typeof(WorkflowStatus), newValues["WorkflowStatus"]);
        //        this.Presenter.Update(w.Id, w.WorkflowStatus);
        //    }
        //}

        ctlItemsGrid.DataSource = _items;
        ctlItemsGrid.DataBind();

        base.PostLoadView();
    }

    void ctlItemsGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;
            IItemDbItem w = (IItemDbItem)e.Item.DataItem;

            // This doesn't work either, values are all null or empty:
            //if (editedItem.CanExtractValues)
            //{
            //    Dictionary<string, string> newValues = new Dictionary<string,string>();
            //    editedItem.ExtractValues(newValues);
            //    if (newValues["WorkflowStatus"] != null)
            //    {
            //        w.WorkflowStatus = (WorkflowStatus)Enum.Parse(typeof(WorkflowStatus), newValues["WorkflowStatus"]);
            //        this.Presenter.Update(w.Id, w.WorkflowStatus);
            //    }
            //}

            GridDropDownListColumnEditor ctlColumnWorkflow = editMan.GetColumnEditor("ctlColumnGridWorkflow") as GridDropDownListColumnEditor;
            ctlColumnWorkflow.DataSource = _workflowStatuses;
            ctlColumnWorkflow.DataBind();
            ctlColumnWorkflow.SelectedValue = w.WorkflowStatus.ToString();

        }
        else if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;

            IItemDbItem w = (IItemDbItem)e.Item.DataItem;

            dataItem["ctlColumnGridWorkflow"].Text = w.WorkflowStatus.ToString();
        }
    }

    void ctlItemsGrid_ItemCommand(object sender, GridCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        IItemDbItem w = (IItemDbItem)e.Item.DataItem;

        switch (cmdName)
        {
            case "Delete":
                this.Presenter.Delete(w.Id);
                this.Presenter.LoadView();
                break;
            case "Update":
                // This doesn't work either: 
                //GridEditableItem editedItem = e.Item as GridEditableItem;
                //GridEditManager editMan = editedItem.EditManager;
                //DropDownList editWorkflow = (DropDownList)editedItem["ctlColumnGridWorkflow"].Controls[0];
                //DropDownList editEvent = (DropDownList)editedItem["ctlColumnGridEvent"].Controls[0];
                //this.Presenter.Update(w.Id, (WorkflowStatus)Enum.Parse(typeof(WorkflowStatus));
                break;
        }

        //this.Presenter.LoadView();
    }

    // Doesn't work: 
    //void ctlItemsGrid_ItemUpdated(object sender, GridUpdatedEventArgs e)
    //{
    //    GridEditableItem editedItem = e.Item as GridEditableItem;
    //    GridEditManager editMan = editedItem.EditManager;
    //    DropDownList editWorkflow = editedItem["ctlColumnGridWorkflow"].Controls[0] as DropDownList;
    //    IItemDbItem w = (IItemDbItem)e.Item.DataItem;
    //    this.Presenter.Update(w.Id, (WorkflowStatus)Enum.Parse(typeof(WorkflowStatus), editWorkflow.SelectedValue));
    //}

    // Doesn't work: 
    void ctlItemsGrid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridEditManager editMan = editedItem.EditManager;
        DropDownList editWorkflow = editedItem["ctlColumnGridWorkflow"].Controls[0] as DropDownList;
        IItemDbItem w = (IItemDbItem)e.Item.DataItem;

        this.Presenter.Update(w.Id, (WorkflowStatus)Enum.Parse(typeof(WorkflowStatus), editWorkflow.SelectedValue));
    }

    public IEnumerable<WorkflowStatus> WorkflowStatuses
    {
        set
        {
            _workflowStatuses = value;
        }
    }

    public IEnumerable<IItemDbItem> Items
    {
        get
        {
            return _items;
        }
        set
        {
            _items = value;
        }
    }

}

我有一个单独的表单来添加项目(未显示),工作正常。删除按钮也可正常工作。尝试更新保存的项目时,我可以从GridDropDownColumn获取的所有值都是null,空或默认值,而不是编辑的值。当在Visual Studio中的立即窗口中单步执行并测试内容时,我能够使用这个荒谬的语句找到正确的值:

((DropDownList)ctlItemsGrid.MasterTableView.GetItems(GridItemType.EditFormItem)[0].Controls[1].Controls[0].Controls[0].Controls[1].Controls[0].Controls[0].Controls[1].Controls[1].Controls[0]).SelectedValue

但必须有一种更简单的方法。我做错了什么?

2 个答案:

答案 0 :(得分:1)

奇怪的是初始实现不起作用 - 我在我的最后制作了一个快速示例项目,并设法从e.Item.DataItem获取所有信息(尽管只有Id列,因为我们绑定到了DropDownList稍后。)

是否有严格要求从ItemDataBound事件中获取此内容?一个更好的事件是UpdateCommand。您可以使用一些快速代码轻松获取该项目:

protected void ctlItemsGrid_UpdateCommand(object sender, GridCommandEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        string Employee = (item["ctlColumnGridWorkflow"].Controls[0] as DropDownList).SelectedItem.Text;
    }
}

然后,您可以使用this documentation article中讨论的技术提取所需的值。

如果您仍想像第一次尝试那样接近它,可能需要一个独立的示例项目才能确切了解正在发生的事情。

答案 1 :(得分:0)

我不确定这是否是修复它的正确方法,但这对我有用:

public partial class administration_modules_item_Default : ViewPageBase<IItemAdminPresenter, IItemAdminView>, IItemAdminView
{
    private IEnumerable<WorkflowStatus> _workflowStatuses;
    private IEnumerable<IItemDbItem> _items;

    protected override void PreloadView()
    {
        this.ctlItemsGrid.ItemDataBound += new Telerik.Web.UI.GridItemEventHandler(ctlItemsGrid_ItemDataBound);
        this.ctlItemsGrid.ItemCommand += new GridCommandEventHandler(ctlItemsGrid_ItemCommand);
        this.ctlItemsGrid.NeedDataSource += new GridNeedDataSourceEventHandler(ctlItemsGrid_NeedDataSource);
    }

    void ctlItemsGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        ctlItemsGrid.DataSource = _items;
    }

    protected override void PostLoadView()
    {
        if (!IsPostBack)
        {
            ctlItemWorkflow.DataSource = _workflowStatuses;
            ctlItemWorkflow.DataBind();
        }

        base.PostLoadView();
    }

    protected override void OnLoadComplete(EventArgs e)
    {
        base.OnLoadComplete(e);
        ctlItemsGrid.Rebind();
    }

    void ctlItemsGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            GridEditManager editMan = editedItem.EditManager;
            IItemDbItem w = (IItemDbItem)e.Item.DataItem;

            GridDropDownListColumnEditor ctlColumnWorkflow = editMan.GetColumnEditor("ctlColumnGridWorkflow") as GridDropDownListColumnEditor;
            ctlColumnWorkflow.DataSource = _workflowStatuses;
            ctlColumnWorkflow.DataBind();
            ctlColumnWorkflow.SelectedValue = w.WorkflowStatus.ToString();
        }
        else if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;

            IItemDbItem w = (IItemDbItem)e.Item.DataItem;

            dataItem["ctlColumnGridWorkflow"].Text = w.WorkflowStatus.ToString();
        }
    }

    void ctlItemsGrid_ItemCommand(object sender, GridCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        IItemDbItem w = (IItemDbItem)e.Item.DataItem;

        switch (cmdName)
        {
            case "Delete":
                GridDataItem item = e.Item as GridDataItem;
                int id = Convert.ToInt32(item.GetDataKeyValue("Id"));
                this.Presenter.Delete(id);
                this.Presenter.LoadView();
                break;
            case "Update":
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                DropDownList editWorkflow = (DropDownList)editedItem["ctlColumnGridWorkflow"].Controls[0];

                int wid = Convert.ToInt32(editedItem.GetDataKeyValue("Id"));

                this.Presenter.Update(wid, (WorkflowStatus)Enum.Parse(typeof(WorkflowStatus), editWorkflow.SelectedValue));
                this.Presenter.LoadView();
                break;
        }
    }

    public IEnumerable<WorkflowStatus> WorkflowStatuses
    {
        set
        {
            _workflowStatuses = value;
        }
    }

    public IEnumerable<IItemDbItem> Items
    {
        get
        {
            return _items;
        }
        set
        {
            _items = value;
        }
    }

}

使用NeedDataSourceRebind似乎让ItemCommand开始按预期运行。