GridView的DataKeys集合空当GridView控件是在EditItemTemplate中 [英] GridView DataKeys Collection Empty When GridView is in an EditItemTemplate

查看:124
本文介绍了GridView的DataKeys集合空当GridView控件是在EditItemTemplate中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

仅供参考:我使用VS2005,.NET 2.0

我有一个GridView控件存在FormView控件的EditItemTemplate。不幸的是,在GridView行为不端的安装程序,它的DataKeys集合为空时,页面回和GridView控件火灾的选择命令。

下面是事件序列:

  1. 在用户浏览的页面
  2. 在用户单击编辑(FormView控件呈现EditItemTemplate中,其中GridView的是)
  3. 在用户点击搜索这再次回传并填充设在EditItemTemplate中的GridView控件(在这一点上的GridView有DataKeys)
  4. 在用户选择这引起了行选择的事件
  5. 从Gri​​dView的项目
  6. 在回传时,GridView RowCommand火灾, - 我检查的CommandName =选择,并运行我的code会抛出异常,因为在DataKeys集合是空的,在这一点上

还有一个额外的细节:GridView控件住在驻留在FormView控件的EditItemTemplate中的用户控件

请注意:

我能当我提出我的GridView了FormView控件的EditItemTemplate中,以解决此问题, - 现在DataKeys集合不为空。不幸的是,GridView控件必须处于EditItemTemplate模板供用户选择客户,他搜索。

任何有识之士将AP preciated。

事件处理code:

 保护无效ctlSearchResults_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    //用户选择了客户端从关键字搜索结果列表
    如果(e.CommandName ==选择)
    {
        GridView控件searchResultsGrid =(GridView控件)e.CommandSource;
        INT selectedRowIndex = int.Parse((串)e.CommandArgument);
        INT客户端ID =(int)的searchResultsGrid.DataKeys [selectedRowIndex] [Client.PROP_ENTITYID]
        //提高Selected事件
        _OnSelected(新ClientSelectedEventArgs(客户端ID));
    }
}
 

解决方案

的原因,DataKey集合为空是因为正在访问的收集之前,数据搜索结果的GridView的约束力。我觉得引导有用的调试命令的处理问题的时候。

All,

FYI: I am using VS2005, .net 2.0.

I have a GridView control that exists in the FormView EditItemTemplate. Unfortunately, the GridView misbehaves in that setup, its DataKeys collection is empty when the page posts back and the select command of the gridview fires.

Here is the sequence of events:

  1. User browses to the page
  2. User clicks 'edit' (FormView renders EditItemTemplate where the GridView is)
  3. The user clicks search which postbacks again and populates the GridView located in the EditItemTemplate (at this point the GridView has DataKeys)
  4. User selects item from GridView which raises row selected event
  5. On postback, the GridView RowCommand fires,- I check that the CommandName = "select" and run my code which throws exception because the DataKeys collection is empty at this point!

One more additional detail: The GridView lives in a user control that resides in the EditItemTemplate of the FormView.

NOTE:

I was able to resolve this problem when I move my GridView out of the EditItemTemplate of the FormView,- now DataKeys collection is NOT empty. Unfortunately, the GridView has to be in the EditItemTemplate for the user to select clients he searched for.

Any insight would be appreciated.

Event handling code:

protected void ctlSearchResults_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // user has selected the client from the keyword search result list
    if (e.CommandName == "select")
    {
        GridView searchResultsGrid = (GridView)e.CommandSource;
        int selectedRowIndex = int.Parse((string)e.CommandArgument);
        int clientId = (int)searchResultsGrid.DataKeys[selectedRowIndex][Client.PROP_ENTITYID];
        // raise Selected event
        _OnSelected(new ClientSelectedEventArgs(clientId));
    }
}

解决方案

The reason that the DataKey collection is empty is because you are accessing the collection prior to data binding of the search results gridview. I find this guide useful when debugging order of processing problems.

这篇关于GridView的DataKeys集合空当GridView控件是在EditItemTemplate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆