2010-06-08 86 views
2

我正在ASP .net 2.0中工作。我是一名学习者。我有一个网格视图,其中有一个按钮。请在下面找到asp标记使用按钮添加新行到asp .net网格视图使用按钮

<form id="form1" runat="server"> 
<div> 
    <asp:GridView ID="myGridView" runat="server"> 
     <Columns> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:Button CommandName="AddARowBelow" Text="Add A Row Below" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
</div> 
</form> 

请在下面找到代码。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Data; 
using System.Web.UI.WebControls; 

namespace GridViewDemo 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      DataTable dt = new DataTable("myTable"); 
      dt.Columns.Add("col1"); 
      dt.Columns.Add("col2"); 
      dt.Columns.Add("col3"); 
      dt.Rows.Add(1, 2, 3); 
      dt.Rows.Add(1, 2, 3); 
      dt.Rows.Add(1, 2, 3); 
      dt.Rows.Add(1, 2, 3); 
      dt.Rows.Add(1, 2, 3); 
      myGridView.DataSource = dt; 
      myGridView.DataBind(); 
     } 

     protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 

     } 
    } 
} 

我在想,当我点击命令按钮,它会触发mygridview_rowcommand(),而是它如下抛出一个错误:

无效的回发或回调参数。事件验证在配置中启用,或在页面中启用<%@ Page EnableEventValidation =“true”%>。为了安全起见,此功能验证回发或回调事件的参数来自最初呈现它们的服务器控件。如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法为注册回发或回调数据进行验证。

任何人都可以让我知道我要去哪里吗?

回答

1

首先,尝试禁用事件验证并查看结果是什么,因为这是代码中的明显问题。

<%@ Page EnableEventValidation="false" ... 

然而,如果你不想restort这样的黑客,请执行下列操作:

之后,删除按钮的属性OnCommand事件,以及OnRowCommand事件添加到GridView本身。

实施例:

<asp:GridView OnRowCommand="myGridView_RowCommand"> 
    ... 
</asp:GridView> 

这应该是处理这些命令的正确方法。
该按钮必须具有特定CommandName才能按预期工作。

See this page有关可能的值和对此问题的更多解释。在该页面上还有一个工作示例,值得你花时间检查一下。

+0

我照你说的去做了。然后我没有得到错误。但是当我点击按钮时,myGridView_RowCommand没有触发。 我其实加了

 asp:Button OnCommand="myGridView_RowCommand" .... 
它工作。 – SARAVAN 2010-06-08 18:59:51

+0

@SARAVAN - 顺便说一句,在你的代码中'GridView'和'myGridView_RowCommand'没有任何连接。 – Venemo 2010-06-08 19:08:02

+0

asp:Button OnCommand =“myGridView_RowCommand” 看看上面的代码,我实际上在你回答我的问题时加了它。无论如何,我更新了代码。 – SARAVAN 2010-06-08 19:51:26