GridView分页,无法获取正确的Datakey值

发布于 2024-09-13 16:46:30 字数 2295 浏览 11 评论 0原文

GridView

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" PagerSettings-Position="TopAndBottom"
    PagerSettings-Mode="Numeric" PageSize="40" CellPadding="4" DataSourceID="dsEquipmentGridView"
    ForeColor="#333333" GridLines="Horizontal" Style="font-size: x-small" AutoGenerateColumns="False"
    DataKeyNames="IREF" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated"
    OnSorted="GridView1_Sorted" OnSorting="GridView1_Sorting" OnPageIndexChanged="GridView1_PageIndexChanged1"
    OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
    OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowCommand="GridView1_RowCommand"
    BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" onload="GridView1_Load" 
    ondatabound="GridView1_DataBound">
    <PagerSettings Position="TopAndBottom" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
 <asp:CommandField ShowDeleteButton="False" ShowSelectButton="true" EditText="QuickEdit"
     HeaderText="Manage" SelectText="Manage" />

对于加载的第一页,如果我单击命令字段,它会显示正确的 DataKey 值。

但是,如果我更改到下一页结果,则在选择记录时,相应的值不正确。似乎保留了上一页的信息。我该如何克服这个问题?

编辑:

在 GridView1_SelectedIndexChanged 方法中,我使用了以下内容:

    // Determine the index of the selected row.
    int index = GridView1.SelectedIndex;

    // Display the primary key value of the selected row.
    HttpContext.Current.Response.Write("The primary key value of the selected row is " + GridView1.DataKeys[index].Value.ToString() + ".<br>");

    GridViewRow row = GridView1.Rows[index];

    int rowindex = row.RowIndex;
    HttpContext.Current.Response.Write("rowindex-> " + rowindex.ToString() + "<br>");

    int ID = (int)GridView1.DataKeys[row.DataItemIndex - (GridView1.PageIndex * GridView1.PageSize)].Value;

    HttpContext.Current.Response.Write("row.DataItemIndex" + ID.ToString()+"<br>");

    int Userid = Convert.ToInt32(GridView1.DataKeys[index].Value);

    HttpContext.Current.Response.Write("Userid" + Userid.ToString() + "<br>");

分页后无法获取正确的值。

GridView

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" PagerSettings-Position="TopAndBottom"
    PagerSettings-Mode="Numeric" PageSize="40" CellPadding="4" DataSourceID="dsEquipmentGridView"
    ForeColor="#333333" GridLines="Horizontal" Style="font-size: x-small" AutoGenerateColumns="False"
    DataKeyNames="IREF" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated"
    OnSorted="GridView1_Sorted" OnSorting="GridView1_Sorting" OnPageIndexChanged="GridView1_PageIndexChanged1"
    OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
    OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowCommand="GridView1_RowCommand"
    BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" onload="GridView1_Load" 
    ondatabound="GridView1_DataBound">
    <PagerSettings Position="TopAndBottom" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
 <asp:CommandField ShowDeleteButton="False" ShowSelectButton="true" EditText="QuickEdit"
     HeaderText="Manage" SelectText="Manage" />

For the first page loaded, it shows the correct DataKey value if I click on the command field.

However if I change to the next page of results, upon selecting a record the corresponding value is incorrect. It seems to keep the previous page information. How do I overcome this?

EDIT:

In the GridView1_SelectedIndexChanged method, I used the following:

    // Determine the index of the selected row.
    int index = GridView1.SelectedIndex;

    // Display the primary key value of the selected row.
    HttpContext.Current.Response.Write("The primary key value of the selected row is " + GridView1.DataKeys[index].Value.ToString() + ".<br>");

    GridViewRow row = GridView1.Rows[index];

    int rowindex = row.RowIndex;
    HttpContext.Current.Response.Write("rowindex-> " + rowindex.ToString() + "<br>");

    int ID = (int)GridView1.DataKeys[row.DataItemIndex - (GridView1.PageIndex * GridView1.PageSize)].Value;

    HttpContext.Current.Response.Write("row.DataItemIndex" + ID.ToString()+"<br>");

    int Userid = Convert.ToInt32(GridView1.DataKeys[index].Value);

    HttpContext.Current.Response.Write("Userid" + Userid.ToString() + "<br>");

I am unable to get the correct value after paging.

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

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

发布评论

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

评论(3

や三分注定 2024-09-20 16:46:30

使用分页时,我们可以获得准确的行索引(在gridview中从0到totalrows计数)
当获取datakey时,我们需要传递当前页面的行索引。

使用下面的行获取当前页面索引并获取 datakey:

// Get the correct row index since there is paging
int idx = gridViewRow.DataItemIndex - TestGridView.PageIndex * TestGridView.PageSize;

When using paging, we can get the exact row index (which is counted from 0 to totalrows in gridview)
and when fetching datakey, we need to pass the row index of the current page.

Use the line below to get the current page index and get the datakey:

// Get the correct row index since there is paging
int idx = gridViewRow.DataItemIndex - TestGridView.PageIndex * TestGridView.PageSize;
春夜浅 2024-09-20 16:46:30

我可能会迟到。
但对于其他人,我回答这个。

“在使用 DataKey 数组之前再次重新绑定 gridview”

它对我有用。
希望它也适用于其他人。

I may be late.
But for others, i reply this.

"Rebind gridview again before using DataKey array"

it works for me.
Hope it works for others too.

巨坚强 2024-09-20 16:46:30

前段时间修好了。正在使用第三方 GridView 帮助程序类。这破坏了 GridView 的正常功能。

Fixed a while ago. Was using a thrid party GridView helper class. Which broke the normal functionality of the GridView.

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