By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,726 Members | 2,465 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,726 IT Pros & Developers. It's quick & easy.

Row Command Firing twice in GridView Control

P: n/a

I am using a GridView control with a ButtonField and receiving 2 row command
events for each click of the button. Is there a configuration switch that I
can turn that will ensure I only receive one call to my handler? I'm using
the most basic wiring as it comes out of VS 2005.. Thanks in advance for your
help!

ex/

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

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1"
runat="server" OnRowCommand="ApprovalGrid_RowCommand"
AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="EventEmployeeId"
HeaderText="Employee" ItemStyle-Width="80" />
<asp:BoundField DataField="EventTypeId" ItemStyle-Width="100"
HeaderText="Type"/>
<asp:ButtonField buttontype="Image"
ImageUrl="images/icon-approve.gif" HeaderText="Approve" Text="Approve this
Event"
CommandName="ApproveEvent" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:AttendanceManagement %>"
SelectCommand="SELECT [EventId], [EventStartDate],
[EventEndDate], [EventDescription], [EventTypeId], [EventHours],
[EventEmployeeId], [EventCreatorId], [EventCreationDate],
[EventLastModifierId], [EventLastModifiedDate], [EventApproved],
[EventApproverId], [EventApprovedDate] FROM [Events]">
</asp:SqlDataSource>

</div>
</form>
</body>
</html>

-------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
protected void ApprovalGrid_RowCommand(object sender,
GridViewCommandEventArgs e)
{
// Get the index of the clicked row
int rowindex = Convert.ToInt32(e.CommandArgument);

switch (e.CommandName)
{
case "ApproveEvent":

break;
case "EditEvent":

break;
}
}
}

--
chelsea
May 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Chelsea,

Thank you for posting.

From your description, on one of your ASP.NET 2.0 web page which contains a
Gridview control, the GridView control's Rowcommand event handler will be
called twice each time it is postback, correct?

As for such event handler executing twice problem, I've also encountered
some formerly and there may exists different causes on this. First, I'd
like to make sure whether this is a page/application or machine specific
issue. To isolate the issue, you can creating other simple test page with
GridView to test or event test the same page on different machines to
verify the behavior.

Also, in addition to the Gridview's RowCommand, does any other events of
the Gridview control (like load..) or event the Page's event like Init,
Load fires twice each time the page post back. Such behavior will also
provide some clues on the problem.

If there's any other finding, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 10 '06 #2

P: n/a
Steven- Thank you so much for the attention to this issue. The problem does
happen on both my development workstation and the deployment server. It
seems to be a bit intermittent in that sometimes a click of the button will
only fire the rowcommand once, but other times it fires twice. The sequence
of events that I observe when the problem happens is:

1.) Page_Load Event fires
2.) -- Grid is displayed and I click the button
3.) Page_Load Post Back fires
4.) Page_Load Post Back fires
5.) Row Command fires
6.) Row Command fires

I'm capturing this activity by setting breaks in the code under VS 2005.
Also, the above results were received when AutoEventWireup="true". My
original post showed wireup="false". Thanks again for your help.

--
chelsea
"Steven Cheng[MSFT]" wrote:
Hi Chelsea,

Thank you for posting.

From your description, on one of your ASP.NET 2.0 web page which contains a
Gridview control, the GridView control's Rowcommand event handler will be
called twice each time it is postback, correct?

As for such event handler executing twice problem, I've also encountered
some formerly and there may exists different causes on this. First, I'd
like to make sure whether this is a page/application or machine specific
issue. To isolate the issue, you can creating other simple test page with
GridView to test or event test the same page on different machines to
verify the behavior.

Also, in addition to the Gridview's RowCommand, does any other events of
the Gridview control (like load..) or event the Page's event like Init,
Load fires twice each time the page post back. Such behavior will also
provide some clues on the problem.

If there's any other finding, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 10 '06 #3

P: n/a
Hi Chelsea,

Thank you for your posting.

It seems that when the page postback, even the page level load (or init?)
event handler will be executed twice. I'm wondering whether there has come
two postback requests to the page. You can check the IIS log to see whether
there exists two requests from client when the page is postback.

Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.