GridView的DataKeys集合空当GridView控件是在EditItemTemplate中是在、空当、控件、GridView

2023-09-05 04:04:57 作者:~呮洧寂寞財浍萇汏

所有,

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

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

下面是事件序列:

在用户浏览的页面 在用户单击编辑(FormView控件呈现EditItemTemplate中,其中GridView的是) 在用户点击搜索这再次回传并填充设在EditItemTemplate中的GridView控件(在这一点上的GridView有DataKeys) 在用户选择这引起了行选择的事件从Gri​​dView的项目 在回传时,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));
    }
}
 
在ASP.NET 2.0中操作数据 在GridView控件中使用TemplateField

解决方案

的原因,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:

User browses to the page User clicks 'edit' (FormView renders EditItemTemplate where the GridView is) The user clicks search which postbacks again and populates the GridView located in the EditItemTemplate (at this point the GridView has DataKeys) User selects item from GridView which raises row selected event 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.