2013-03-11 153 views
0

我在3个不同的页面中有3个GridView控件。他们以前工作得很好。 突然我得到这个错误信息:Gridview:索引超出范围

无效的回发或回调参数。事件验证使用<pages enableeventvalidation="true" />

所以我试着把<@pages enableeventvalidation="false" />在页面中。它仍然没有工作。 我从页面删除了这个命令。然后当单击gridview中所有girdview控件的按钮时,另一条消息显示“索引超出范围”。

int id = Convert.ToInt32(myGridView.DataKeys[e.RowIndex].Value) 

它看起来像在GridView找不到datakey(e.rowIndex的价值是OK),的DataKeyNames已设置。

protected void gvItemCategory_RowDeleting(object sender, GridViewDeleteEventArgs e) { 

    int categoryId = Convert.ToInt32(gvItemCategory.DataKeys[e.RowIndex].Value); 

    CollectionCategory category = new CollectionCategory(); 
    category.CategoryId = categoryId; 
    category.Delete(); 
    ItemCateogryShowData(); 
} 

HTML:

<asp:GridView ID="gvItemCategory" runat="server" AutoGenerateColumns="False" 
OnRowCancelingEdit="gvItemCategory_RowCancelingEdit" 
    OnRowDeleting="gvItemCategory_RowDeleting" OnRowEditing="gvItemCategory_RowEditing" 
    DataKeyNames="CategoryId" OnRowUpdating="gvItemCategory_RowUpdating"> 
    <Columns> 
     <asp:TemplateField HeaderText="CateogoryName"> 
      <ItemTemplate> 
       <asp:Label ID="lblItemCategoryName" runat="server" Text='<%#Eval("CategoryName") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox ID="tbItemCateogryName" runat="server" Text='<%#Eval("CategoryName") %>'></asp:TextBox> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField ShowEditButton="True" ButtonType="Button" /> 
     <asp:CommandField ShowDeleteButton="True" /> 
    </Columns> 
    </asp:GridView> 
+0

你是否通过javascript操作任何东西(例如,添加项目到下拉菜单)?你在回发数据绑定网格?然后索引变为-1,这将超出范围。 – 2013-03-11 11:13:26

+0

当您尝试enableeventValidation =“false”时会发生什么。 – coder 2013-03-11 11:26:16

+0

在页面中放入enableeventValidation =“false”后,“无效回传或回调参数”错误消息消失,我可以看到页面内容。但gridview按钮有问题 – yilee 2013-03-11 11:37:38

回答

0

尝试在页面加载!ispostback方法绑定你的GridView ..

If(!isPostback) 
{ 
//your code here 
} 

试试这个

int categoryId = Convert.ToInt32(gvItemCategory.DataKeys[e.RowIndex].Value.Tostring()); 

注意:同样使用EnableeventValidation="false"

+0

我试过了,它不起作用。 – yilee 2013-03-11 11:38:08

+0

@ user2156481你得到的错误是什么。 – coder 2013-03-11 11:39:33

+0

指数超出范围。必须是非负数且小于集合的大小。字符串id = myGridView.DataKeys [row.rowIndex] .value.ToString() – yilee 2013-03-11 11:46:14