为什么我在GridView1_RowCommand事件里使用e.CommandArgument.ToString()会出错说回发或回调参数无效?
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
--------------------------------------------------------------------------------------
在GridView的模版列里:
<asp:TemplateField HeaderText="操作">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Button ID="hehe" CommandName="showData" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Name") %>' runat="server" Text="按呀按~~" />
</ItemTemplate>
</asp:TemplateField>
在CS里:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("showData")) {
try
{
Response.Write(e.CommandArgument.ToString());
}
catch (Exception err)
{
throw new Exception(err.ToString());
}
}
}