Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Gridview rowcommand not working after Gridview Bind coding

if i add source using gui rowcommand working but when i bind using code rowcommand give me this error 

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.


and if i put enableEventValidation="true" in page, rowcommand not working 

how do i make it possible ?


What I have tried:

Gridview

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm13.aspx.cs" Inherits="WebApplication3.WebForm13"  enableEventValidation="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" 
            onrowcommand="GridView1_RowCommand">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField AccessibleHeaderText="AID" HeaderText="AID">
            <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("AID") %>'></asp:Label>
            </EditItemTemplate>
            <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("AID") %>'></asp:Label>
            </ItemTemplate>
            
            </asp:TemplateField>
            <asp:BoundField HeaderText="FIRST NAME" DataField= "FirstName" />
            <asp:BoundField HeaderText="LAST NAME"  DataField= "LastName"/>
            <asp:BoundField HeaderText="EMP. CODE" DataField= "EMPID" />
            <asp:BoundField HeaderText="GENDER" DataField= "GENDER" />
            <asp:BoundField HeaderText="EMAIL" DataField= "EMAILID" />
            <asp:TemplateField HeaderText="VIEW">
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="VIEW"  CommandArgument="<%#((GridViewRow) Container).RowIndex %>" CommandName="viewuser" CausesValidation="True" Height="24px" Width="20px" Font-Size="11px" BackColor="#6699FF" BorderColor="Black" BorderWidth="0.5px" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="EDIT">
                <ItemTemplate>
                    <asp:Button ID="Button2" runat="server" Text="EDIT" CommandArgument="<%#((GridViewRow) Container).RowIndex %>" CommandName="viewuser" CausesValidation="True" Height="24px" Width="20px" Font-Size="11px" BackColor="#6699FF" BorderColor="Black" BorderWidth="0.5px" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" 
            HorizontalAlign="Left" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
</div>
</form>





    
</body>
</html>

-------------------------------------------------------------------
Page load

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand
                    (@"SELECT [AID],[FirstName], [LastName], [EMPID], [GENDER], [EMAILID] FROM USERSMST", con);
                    cmd.Connection = con;
                    DataTable dt = new DataTable();
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }



            }
            catch (SqlException EX)
            {
                Response.Write(EX.Message);
            }
        }


-----------------------------------------
Rowcommand

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       if (e.CommandName == "viewuser")
           {
               Response.Redirect("userlist.aspx");
           }
Posted
Updated 15-Nov-18 18:05pm
v6
Comments
akshay_zz 15-Nov-18 12:01pm    
Can you put your GridView1_RowCommand function code. One more thing can you put a debugger in GridView1_RowCommand in the function and can check that the debugger is getting hit or not.
Member 14041670 15-Nov-18 22:56pm    
i have updated my question and code.
akshay_zz 15-Nov-18 23:30pm    
Can you also show your pageload function.
Member 14041670 15-Nov-18 23:49pm    
I have already put page load.
this is my name spaces.

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

namespace WebApplication3
{
public partial class WebForm13 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
akshay_zz 15-Nov-18 23:52pm    
What are you doing buddy. That's only page load definition, show the body part

Look like the button click is causing a Postback, based on the code, the Page_Load method will get data from the database and rebind the Grid each time a PostBack occur.

On Page_Load method, update the code to wrap the logic inside the !IsPostBack

C#
if (!IsPostBack)
{
   try  {
       ....
   }
}

This thread explained the situation.
GridView Along With Page Index Changing and Button Inside | The ASP.NET Forums[^]
 
Share this answer
 
I think you are missing ispostback check in pageload. ispostback check whether is page is loading first time or it just a postback from a control.

C#
protected void Page_Load(object sender, EventArgs e)
{
  if(!Page.IsPostBack)
  {
   //Control Initialization
   //Databinding
   bind_grid()
  }
}

private void bind_grid()
{
  // Bind your grid here
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
       if (e.CommandName == "viewuser")
           {
               Response.Redirect("userlist.aspx");
           }
}


I think this should work for you. If it helps please mark this as answer.
 
Share this answer
 
Comments
Member 14041670 16-Nov-18 1:22am    
Its working
Thank You Brother
akshay_zz 16-Nov-18 6:54am    
You can also go for if(!IsPostBack) in place of if(!Page.IsPostBack)
Member 14041670 16-Nov-18 22:35pm    
yes i tried both, its works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900