gpt4 book ai didi

asp.net - GridView 分页问题

转载 作者:行者123 更新时间:2023-12-04 23:45:06 25 4
gpt4 key购买 nike

我正在使用 gridview 控件并手动执行分页和排序。
以下是分页的方法:

protected void gdvMainList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdvMainList.PageIndex = e.NewPageIndex;
gdvMainList.DataSource = dtConsentReleaseList;
gdvMainList.DataBind();
}

我有一个具有列 ID 的静态数据表:
dtConsentReleaseList.Columns.Add("Id");
dtConsentReleaseList.Columns.Add("StartDate");
dtConsentReleaseList.Columns.Add("EndDate");
dtConsentReleaseList.Columns.Add("Contact");

我在我的 GridView 中分配数据键名“Id”。
而且我在每一行还有一个打印按钮。当我单击该按钮时,将执行此代码:
else if (e.CommandName == "New")
{
int selectedIndex = Convert.ToInt32(e.CommandArgument);
int consentReleaseId = Convert.ToInt32(gdvMainList.DataKeys[selectedIndex].Value);
string openReportScript = Utility.OpenReport(ResolveClientUrl("~/Reports/Consumer/ConsentReleaseReport.aspx?Id=" + consentReleaseId + "&ReportTitle=ConsentForRelease"));
ScriptManager.RegisterClientScriptBlock(upConsentRelease, upConsentRelease.GetType(), "Pop up", openReportScript, true);
}

但是当我更改页面并单击打印按钮时,此行出现异常:
int consentReleaseId = Convert.ToInt32(gdvMainList.DataKeys[selectedIndex].Value);

异常(exception)是:
Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index

我想我在分页方法中做错了什么。

请问有什么帮助吗?

最佳答案

您正在尝试根据任意 ID 而不是实际索引从数组中获取值。但你根本不需要这样做。您无需将 ID 存储在 DataKeys 中并且您不需要使用项目的索引访问任何内容。只需从 CommandArgument 中取出您的 ID .

<asp:ImageButton CommandName="New" CommandArgument='<%# Eval("Id") %>' ID="ibtnPrint" runat="server" ImageUrl="~/App_Themes/Default/images/print.png" />

然后在代码隐藏中:
int consentReleaseId = int.Parse(e.CommandArgument);

关于asp.net - GridView 分页问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5977928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com