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

What I usually find myself using is a buttons OnClick events instead of the GridViewRowCommand. It's easier to work with for the most part. So your ASP would look like;

<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>

And your c# would look like;

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

Now in your code behind you can do anything you need to do with that row, and you know you'll be getting the row you want.

Hope this helps!

于 2013-05-22T14:21:52.863 回答