Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击按钮时出现无效回发或回调参数错误-初学者_C#_Asp.net - Fatal编程技术网

C# 单击按钮时出现无效回发或回调参数错误-初学者

C# 单击按钮时出现无效回发或回调参数错误-初学者,c#,asp.net,C#,Asp.net,我得到以下错误 回发或回调参数无效。已启用事件验证 在配置或页面中使用。为了安全 为此,此功能验证回发或回调的参数是否正确 事件源自最初呈现的服务器控件 他们如果数据有效且符合要求,请使用 ClientScriptManager.RegisterForEventValidation方法,以便 注册回发或回调数据以进行验证 我添加了一个列,并在其中添加了一个按钮,当启动按钮时,将执行以下C代码 ASP.NET代码 <Columns> <%-- <as

我得到以下错误

回发或回调参数无效。已启用事件验证 在配置或页面中使用。为了安全 为此,此功能验证回发或回调的参数是否正确 事件源自最初呈现的服务器控件 他们如果数据有效且符合要求,请使用 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>
我如何摆脱这个错误

我添加了,但错误仍然存在。 `

更新


我通常使用的是按钮OnClick events,而不是GridViewRow命令。在大多数情况下,使用它更容易。所以你的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>
现在,在代码背后,您可以对该行执行任何需要执行的操作,并且您知道您将获得所需的行


希望这有帮助

是否动态添加任何控件?您是否也可以显示GridView开始标记?我已经添加了代码我不太确定,但您可以尝试将AutoGenerateColumns设置为false吗?是否使用页面加载集IsPostBack属性中的“更新面板”。
            <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>
<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>
protected void AddButton_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
}