Add new row to asp .net grid view using button

Posted by SARAVAN on Stack Overflow See other posts from Stack Overflow or by SARAVAN
Published on 2010-06-08T18:44:02Z Indexed on 2010/06/08 18:52 UTC
Read the original article Hit count: 292

Filed under:
|
|
|

Hi, I am working in ASP .net 2.0. I am a learner. I have a grid view which has a button in it. Please find the asp mark up below

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

Please find the code behind below.

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

        }
    }
}

I was thinking that when I click the command button, it would fire the mygridview_rowcommand() but instead it threw an error as follows:

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Can any one let me know on where I am going wrong?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about ASP.NET