在列表视图中的数据寻呼机问题
我有一个列表视图在我放置datapager如下。 我正在使用SQl数据源并将记录绑定到ListView。在列表视图中的数据寻呼机问题
asp:ListView runat="server" ID="ListView1" DataKeyNames="ProductId,GameName" DataSourceID="GameTable" OnItemCommand="On_Select_Item"
和DataPager的在LayoutTemplate模板
而在项目模板,我放置一个按钮,点击时,它调用其中我试图获取DatakeyName值的方法。当pager被给出时,它在第一页中工作正常,但是当移动到寻呼机中的其他页面时,它引发了一个异常。 下面是按钮点击代码,
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
...............................
.............................
}
首先定义一个网格,底部(你可以在控制名称更改为ListView的,但实行的是相同的)
<asp:GridView runat="server" ID="grdResult"
CellPadding="2"
OnPageIndexChanging="grdResult_PageIndexChanging"
GridLines="None"
Width="100%"
AllowSorting="True"
AllowPaging="True"
然后现在在代码中定义数据源
<asp:SqlDataSource ID="sqlGridData" runat="server"></asp:SqlDataSource>
身后数据加载sqlGridData控制(它接受许多参数,如数据表,odbcrecordset可以使用.provider财产这里提到http://tinyurl.com/bllyjsz)如果你有静态数据他们,你甚至提在设计时(如这里http://tinyurl.com/c8b6mbh完成)
private void BindDataGrid()
{
sqlGridData.Provider = dataReader;
grdResult.DataSourceID = "xmlGridData";
//grdResult.PageIndex = 0;
}
试试这个,你让我知道如果您有任何疑问。
问题与ListView1.DataKeys [dataItem.DataItemIndex] [“ProductId”]。ToString(); //所选游戏的产品ID。 string GameName = ListView1.DataKeys [dataItem.DataItemIndex] [“GameName”]。ToString(); //游戏名称 我无法获取datakey值:( – 2013-04-08 19:54:15
@TwinklingStar:请在此处发布您的代码,我会尝试查看它(您可以编辑您的原始帖子以添加代码)并尝试解析而不是铸造(只是为了避免null验证)像:string GameName =(string)ListView1.DataKeys [dataItem.DataItemIndex] [“GameName”];或者尝试convert.tostring()。 – Vishal 2013-04-09 03:43:48
什么是例外? – Melanie 2013-04-08 18:44:27
使用DataPager的任何特定原因?您可以使用gridview/listview中的inbuild分页和排序功能,以及使用sqldatasource(作为网格的数据提供程序),无需手动执行分页,它会自动处理页面索引页面。 – Vishal 2013-04-08 18:46:32
我得到的异常是ArgumentOutOfRange和我正在使用datapager,因为查询返回多个记录,我会检查内置的分页,并返回给你 – 2013-04-08 18:49:02