【发布时间】:2013-04-09 03:25:31
【问题描述】:
我有一个列表视图,我在其中放置了 datapager,如下所示。 我正在使用 SQl 数据源并将记录绑定到 ListView。
asp:ListView runat="server" ID="ListView1" DataKeyNames="ProductId,GameName" DataSourceID="GameTable" OnItemCommand="On_Select_Item"
和布局模板中的数据分页器
在项目模板中,我放置了一个按钮,当单击它时,它会调用我试图获取 DatakeyName 值的方法。给出寻呼机时,它在第一页工作正常,但是当移动到寻呼机中的其他页面时,它会抛出异常。 这是按钮点击代码,
protected void On_Select_Item(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "AddtoCart"))
{
//checks if the user is logged in
if (User.Identity.IsAuthenticated)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
DropDownList dl = e.Item.FindControl("DropDownList") as DropDownList;
String val="";
if (dl != null)
{
val = dl.SelectedValue; //Get the selected value from DropDownList
}
String price = Convert.ToString(e.CommandArgument).Trim(); //Get the price for the selected game.
-------------下面行抛出异常---------
string ProductId =
ListView1.DataKeys[dataItem.DataItemIndex]["ProductId"].ToString(); //Product Id for the selected game.
string GameName = ListView1.DataKeys[dataItem.DataItemIndex]["GameName"].ToString(); //gamename
...............................
.............................
}
【问题讨论】:
-
有什么例外?
-
使用 DataPager 有什么特别的原因吗?您可以使用 gridview/listview 附带的内置分页和排序功能,并通过使用 sqldatasource(作为网格的数据提供者),您无需手动进行分页,它会自动处理分页、页面索引。
-
我得到的异常是 ArgumentOutOfRange,我正在使用数据分页器,因为查询返回多条记录,我将检查内置分页并回复您
-
@TwinklingStar 一定要让我知道,以防您需要代码尖晶石,我已经多次使用该功能,它就像一个魅力。
-
@VishalSachdeva 你能发一下吗。