Milon Sarker
Milon Sarker

Reputation: 478

GridView Value Pass

I'm added a template field and made it a button click event. Then I'm trying to sent the value to event method and want to some operation with that value. I wrote following code. It has no compilation error but when i click Present browser shows following message:

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.

Code:

 <div>
                <asp:Panel ID="PanelAllEmployee" runat="server" Height="400px">
                  <asp:GridView ID="GridViewAllEmployee" runat="server" AutoGenerateColumns="False" style="position:absolute;left:219px; top:202px;" >
                      <Columns>
                          <asp:BoundField DataField="Id" HeaderText="Id" />
                          <asp:BoundField DataField="Name" HeaderText="Name" />
                          <asp:BoundField DataField="Department" HeaderText="Department" />
                          <asp:BoundField DataField="EmailID" HeaderText="Email ID" />
                          <asp:BoundField DataField="Position" HeaderText="Position" />
                          <asp:TemplateField HeaderText="Present">
                              <ItemTemplate>
                                  <asp:Button ID="Button1" runat="server" CommandArgument='<%#Eval("Id") %>' OnClick="Button1_Click" Text="Present" />
                              </ItemTemplate>
                          </asp:TemplateField>
                      </Columns>
                  </asp:GridView>

                    <asp:Label ID="Label1" runat="server" Text="Label"> </asp:Label>

               </asp:Panel>
              </div>

Event Method:

protected void Button1_Click(object sender, EventArgs e)
    {
        LinkButton lnk = (LinkButton)sender;
        string id = lnk.CommandArgument.ToString();
        Label1.Text = id;
    }

Please be kind to help me. I'm beginner. Elaborate answer is much appreciated. Thanks for your time.

Upvotes: 1

Views: 2228

Answers (1)

Dgan
Dgan

Reputation: 10295

set

EnableEventValidation="false" in the page directive in code-behind

for ex:

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

EDIT: .aspx :

  <asp:Button ID="Button1" runat="server" 
 OnClick = "Button1_Click" CommandArgument='<%#Eval("Id") %>'  Text="Present" />

cs code:

  protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

        string id = btn.CommandArgument.ToString();


    }

using ButtonField and an OnRowCommand Event

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" 

    OnRowCommand = "OnRowCommand">

 <Columns>

  <asp:ButtonField CommandName = "ButtonField"  DataTextField = "CustomerID"

        ButtonType = "Button"/>

 </Columns>

</asp:GridView>

Code Behind:

protected void OnRowCommand(object sender, GridViewCommandEventArgs e)

{

    int index = Convert.ToInt32(e.CommandArgument);

    GridViewRow gvRow = GridView1.Rows[index]; 

}

Upvotes: 1

Related Questions