0

3 つの異なるページに 3 つの GridView コントロールがあります。以前は正常に機能していました。突然、次のエラー メッセージが表示されました。

ポストバックまたはコールバック引数が無効です。イベントの検証は次を使用して有効にします<pages enableeventvalidation="true" />

<@pages enableeventvalidation="false" />というわけで、ページに入れてみました。それでもうまくいきませんでした。このコマンドをページから削除しました。次に、すべての 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>
4

1 に答える 1

0

pageload!ispostbackメソッドで Gridview をバインドしてみてください。

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

これを試して

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

:また使用EnableeventValidation="false"

于 2013-03-11T11:30:06.687 に答える