[英]Invalid postback or callback argument error when button click - Beginner
我收到以下錯誤;
無效的回發或回調參數。 使用配置或頁面中的<%@頁面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>
我通常發現自己使用的是按鈕OnClick事件而不是GridViewRowCommand。 在大多數情況下,使用起來更容易。 因此您的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;
}
現在,在后面的代碼中,您可以執行該行所需的任何操作,並且知道將獲得所需的行。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.