RadGrid ItemDataBound 不显示网格中第一项的更改

发布于 2024-12-11 05:18:50 字数 1931 浏览 0 评论 0原文

我知道我一定错过了什么。如果我在 RadGrid 的 ItemDataBound 事件中对 datagriditem 进行更改,则第一次加载页面时,更改不可见,在通过 CommandItem 刷新网格之前,我不会看到对 DataItem 的更改。我已验证 ItemDataBound 事件已触发,并且我要替换的值实际上具有正确的值。

背景: 我有一个创建 RadGrid 的类。然后它被实例化并通过 .aspx 后面的代码加载到 .aspx 页面中。如果有什么区别的话,这是一个主/详细数据网格。

protected void Page_Init(object source, EventArgs e)
{
    this.__activeBatchesRadGrid = ActiveBatchesRadGrid.GridDefinition("ActiveBatchesRadGrid");
    this.PlaceHolder1.Controls.Add(this.__activeBatchesRadGrid);
    this.__activeBatchesRadGrid.ItemDataBound += new GridItemEventHandler(ActiveBatchesRadGrid_ItemDataBound);
}

private void ActiveBatchesRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    GridDataItem _dataItem = e.Item as GridDataItem;
    if (_dataItem == null) return;

    BatchStatusType _batchStatus = EnumUtils.GetValueFromName<BatchStatusType>(_dataItem["BatchStatusName"].Text);

    Dictionary<BatchStatusType, BatchStatusType> _batchStatusTypes = 
        BatchTransitions.GetBatchStatusTransition(_batchStatus);

    GridButtonColumn _btnPromote =
        ((GridButtonColumn) this.__activeBatchesRadGrid.MasterTableView.GetColumn("MasterPromoteRecord"));

    GridButtonColumn _btnDelete =
        ((GridButtonColumn)this.__activeBatchesRadGrid.MasterTableView.GetColumn("MasterDeleteRecord"));

    foreach (KeyValuePair<BatchStatusType, BatchStatusType> _item in _batchStatusTypes)
    {
        _btnPromote.Text = _item.Value.ToString();
        _btnPromote.ConfirmText = string.Format("Are you sure you want to promote this batch to {0} status?",
                                               _item.Value);

        _btnDelete.Text = string.Format("Demote batch to {0} status.", _item.Key.ToString());
        _btnDelete.ConfirmText = string.Format("Are you sure you want to demote this batch to {0} status?",
                                              _item.Key);
    }
}

I know I must be missing something. If I make changes to a datagriditem in the ItemDataBound event of the RadGrid the changes are not visible the first time the page loads, I don't see the changes to the DataItem until I refresh the grid via the CommandItem for refresh. I have verified that the ItemDataBound event is fired and the values that I am replacing do infact have the correct values.

background:
I have a class that creates the RadGrid. It is then instantiated and loaded into the .aspx page via the code behind for the .aspx. This is a master/detail datagrid if that makes any difference.

protected void Page_Init(object source, EventArgs e)
{
    this.__activeBatchesRadGrid = ActiveBatchesRadGrid.GridDefinition("ActiveBatchesRadGrid");
    this.PlaceHolder1.Controls.Add(this.__activeBatchesRadGrid);
    this.__activeBatchesRadGrid.ItemDataBound += new GridItemEventHandler(ActiveBatchesRadGrid_ItemDataBound);
}

private void ActiveBatchesRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    GridDataItem _dataItem = e.Item as GridDataItem;
    if (_dataItem == null) return;

    BatchStatusType _batchStatus = EnumUtils.GetValueFromName<BatchStatusType>(_dataItem["BatchStatusName"].Text);

    Dictionary<BatchStatusType, BatchStatusType> _batchStatusTypes = 
        BatchTransitions.GetBatchStatusTransition(_batchStatus);

    GridButtonColumn _btnPromote =
        ((GridButtonColumn) this.__activeBatchesRadGrid.MasterTableView.GetColumn("MasterPromoteRecord"));

    GridButtonColumn _btnDelete =
        ((GridButtonColumn)this.__activeBatchesRadGrid.MasterTableView.GetColumn("MasterDeleteRecord"));

    foreach (KeyValuePair<BatchStatusType, BatchStatusType> _item in _batchStatusTypes)
    {
        _btnPromote.Text = _item.Value.ToString();
        _btnPromote.ConfirmText = string.Format("Are you sure you want to promote this batch to {0} status?",
                                               _item.Value);

        _btnDelete.Text = string.Format("Demote batch to {0} status.", _item.Key.ToString());
        _btnDelete.ConfirmText = string.Format("Are you sure you want to demote this batch to {0} status?",
                                              _item.Key);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2024-12-18 05:18:50

我想我应该发布我整理的解决方案来解决这个问题。然而,我仍然相信我最初发布的正确实现应该有效。如果它适用于除第一个数据网格行之外的所有项目,那么我相信该控件存在缺陷。

private void ActiveBatchesRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    GridDataItem _dataItem = e.Item as GridDataItem;
    if (_dataItem == null) return;
    if (_dataItem.KeyValues == "{}") { return; }
    int _counter = 0;

    Dictionary<String, String> _batchStatusTypes =
        BatchTransitions.GetBatchStatusTransition(
            EnumUtils.GetValueFromName<BatchStatusType>(_dataItem["BatchStatusName"].Text));

    //accessing the cell content directly rather than trying to access the property of the GridEditCommandColumn
    ((ImageButton)(((GridEditableItem)e.Item)["MasterEditrecord"].Controls[0])).ImageUrl = "/controls/styles/images/editpencil.png";

    //accessing the cell content directly rather than trying to access the property of the GridButtonColumn            
    ImageButton _imgbtnPromote = (ImageButton)((GridDataItem)e.Item)["MasterPromoteRecord"].Controls[0];
    ImageButton _imgbtnDelete = (ImageButton)((GridDataItem)e.Item)["MasterDeleteRecord"].Controls[0];
    foreach (KeyValuePair<String, String> _kvp in _batchStatusTypes)
    {
        if (_counter == 0)
        {
            const string _jqueryCode = "if(!$find('{0}').confirm('{1}', event, '{2}'))return false;";
            const string _confirmText = "Are you sure you want to change the status of this batch {0}?";
            _imgbtnPromote.Attributes["onclick"] = string.Format(_jqueryCode, ((Control) sender).ClientID,
                                                                 string.Format(_confirmText, _kvp.Value),
                                                                 _kvp.Value);
            _imgbtnDelete.Attributes["onclick"] = string.Format(_jqueryCode, ((Control) sender).ClientID,
                                                                string.Format(_confirmText, _kvp.Key), _kvp.Key);
            _counter++;
            continue;
        }

        _imgbtnPromote.ImageUrl = "~/controls/styles/images/approve.png";
        _imgbtnPromote.ToolTip = string.Format("{0} batch", _kvp.Value);
        _imgbtnDelete.ImageUrl = "/controls/styles/images/decline.png";
        _imgbtnDelete.ToolTip = string.Format("{0} batch", _kvp.Key);

    }
}

I thought I would post the solution I put together that takes care of this problem. I do however, still believe that the proper implementation that I originally posted should work. If it works for all items other than the first datagrid row then I believe there is a shortcoming in the control.

private void ActiveBatchesRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    GridDataItem _dataItem = e.Item as GridDataItem;
    if (_dataItem == null) return;
    if (_dataItem.KeyValues == "{}") { return; }
    int _counter = 0;

    Dictionary<String, String> _batchStatusTypes =
        BatchTransitions.GetBatchStatusTransition(
            EnumUtils.GetValueFromName<BatchStatusType>(_dataItem["BatchStatusName"].Text));

    //accessing the cell content directly rather than trying to access the property of the GridEditCommandColumn
    ((ImageButton)(((GridEditableItem)e.Item)["MasterEditrecord"].Controls[0])).ImageUrl = "/controls/styles/images/editpencil.png";

    //accessing the cell content directly rather than trying to access the property of the GridButtonColumn            
    ImageButton _imgbtnPromote = (ImageButton)((GridDataItem)e.Item)["MasterPromoteRecord"].Controls[0];
    ImageButton _imgbtnDelete = (ImageButton)((GridDataItem)e.Item)["MasterDeleteRecord"].Controls[0];
    foreach (KeyValuePair<String, String> _kvp in _batchStatusTypes)
    {
        if (_counter == 0)
        {
            const string _jqueryCode = "if(!$find('{0}').confirm('{1}', event, '{2}'))return false;";
            const string _confirmText = "Are you sure you want to change the status of this batch {0}?";
            _imgbtnPromote.Attributes["onclick"] = string.Format(_jqueryCode, ((Control) sender).ClientID,
                                                                 string.Format(_confirmText, _kvp.Value),
                                                                 _kvp.Value);
            _imgbtnDelete.Attributes["onclick"] = string.Format(_jqueryCode, ((Control) sender).ClientID,
                                                                string.Format(_confirmText, _kvp.Key), _kvp.Key);
            _counter++;
            continue;
        }

        _imgbtnPromote.ImageUrl = "~/controls/styles/images/approve.png";
        _imgbtnPromote.ToolTip = string.Format("{0} batch", _kvp.Value);
        _imgbtnDelete.ImageUrl = "/controls/styles/images/decline.png";
        _imgbtnDelete.ToolTip = string.Format("{0} batch", _kvp.Key);

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文