Dipa Ahuja

Dipa Ahuja

  • NA
  • 3.1k
  • 715.4k

create event of button inside gridview

Mar 13 2010 11:42 AM
hello.. i have taken one buttton inside gridview..

n when that button is clickd i want to delete that particular record from gridview...

<asp:TemplateField HeaderText="Select">               
                         <ItemTemplate>
                             <asp:Button ID="btndelete" CommandName="mydelete"
                             Visible="false"
                             runat="server" Text="mydelete" />                   
                         </ItemTemplate>
                    </asp:TemplateField>
n when that button is clickd i want to delete that particular record from gridview...

i did in .cs

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);

     
        Label sid = (Label)GridView1.Rows[index].FindControl("lblsid");
        Label fid = (Label)GridView1.Rows[index].FindControl("lblfromId");

if (e.CommandName == "mydelete")
        {
            scrapobj.Open();

            string q = "Delete from scraps where sid=@sid AND fromId=@fromId";
            SqlCommand comm = new SqlCommand(q, scrapobj);

            comm.Parameters.AddWithValue("@sid", Convert.ToInt32(sid.Text.ToString()));
            comm.Parameters.AddWithValue("@fromId", p.profile_Id);
            comm.ExecuteNonQuery();
            scrapobj.Close();
            GridView1.DataBind();

        }

but it gives this error:


Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> 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



Answers (1)