1

次のエラーが表示されます。

ポストバックまたはコールバック引数が無効です。イベントの検証は、設定で使用するか、ページで <%@ Page EnableEventValidation="true" %> を使用して有効にします。セキュリティ上の目的で、この機能は、ポストバック イベントまたはコールバック イベントへの引数が、それらを最初にレンダリングしたサーバー コントロールから発信されていることを確認します。データが有効で期待される場合は、ClientScriptManager.RegisterForEventValidation メソッドを使用して、検証のためにポストバックまたはコールバック データを登録します。

列を追加し、それにボタンを追加しました。ボタンが起動されると、次の C# コードが実行されます。

ASP.NET コード

    <Columns>
        <%-- <asp:BoundField /> Definitions here --%>
        <asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="AddButton" runat="server" 
      CommandName="AddToCart" 
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate> 
</asp:TemplateField>

    </Columns>

C#

   protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = GridView1.Rows[index];

            }

        }

このエラーを取り除くにはどうすればよいですか。

追加しまし<globalization requestEncoding="utf-8"/>たが、エラーはまだあります。`

アップデート

            <asp:GridView runat="server" ID="gdv" AutoGenerateColumns="True" OnSorting="sortRecord" AllowSorting="true" DataKeyNames="HotelName" CellPadding="4" Width="746px">
    <Columns>
        <%-- <asp:BoundField /> Definitions here --%>
        <asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="AddButton" runat="server" 
      CommandName="AddToCart" 
      CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate> 
</asp:TemplateField>

    </Columns>
</asp:GridView>
4

1 に答える 1

0

私が通常使用しているのは、GridViewRowCommand の代わりにボタンの OnClick イベントです。ほとんどの場合、作業が簡単です。したがって、ASP は次のようになります。

<asp:GridView runat="server" ID="gdv" AutoGenerateColumns="False" OnSorting="sortRecord"    AllowSorting="true" DataKeyNames="HotelName" CellPadding="4" Width="746px">
    <Columns>
      <%-- <asp:BoundField /> Definitions here --%>
        <asp:TemplateField>
           <ItemTemplate>
              <asp:Button ID="AddButton" runat="server" OnClick="AddButton_Click" Text="Add to Cart" />
           </ItemTemplate> 
        </asp:TemplateField>
    </Columns>
</asp:GridView>

そして、あなたの c# は次のようになります。

protected void AddButton_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
}

コード ビハインドでは、その行に対して必要なことは何でも実行でき、目的の行を取得できることがわかります。

お役に立てれば!

于 2013-05-22T14:21:52.863 に答える