GridView event firing twice

Discussion in 'ASP .Net' started by Froefel, Aug 20, 2007.

  1. Froefel

    Froefel Guest

    A common question, but I haven't found an answer on the net yet, so
    maybe someone from the group can give me some pointers.

    I have a webform with a gridview control that has a ButtonField column
    (button is an icon). When the icon is clicked, the OnRowCommand is
    fired with CommandName "DeleteProject".
    All this is working fine, except that, upon clicking the icon, the
    order of events is as follows:

    1. Page_Load (with IsPostback == true)

    2. gvProjects_RowCommand (with CommandName == "DeleteProject"
    and CommandArgument == the index of the row; this is where the project
    actually gets deleted through a stored procedure)

    3. gvProjects_RowDataBound (nothing special happens here -- only
    the icon is hidden for projects that the user doesn't have delete
    permissions for)

    4. Page_Load (with IsPostback == true)

    5. gvProjects_RowCommand (with CommandName == "DeleteProject"
    and CommandArgument == the same index as in step 2 -- THIS CAUSES
    ERRORS!!)

    The last event causes various errors:
    If the last row was being deleted, then the index from step 2 doesn't
    exist anymore.
    If a row somewhere in the middle was being deleted, then 2 projects
    are being deleted, because the index in step 5 points to the row
    following the row in step 2.

    I can send code if you need it, but for some reason if I include C#
    code in these newsgroup posts, my post doesn't make it to the group.

    Any help is greatly appreciated.
    -- Hans
     
    Froefel, Aug 20, 2007
    #1
    1. Advertising

  2. Froefel

    Froefel Guest

    Below is the relevant code to support my previous post:

    GridView definition on the ASPX page:
    -------------------------------------------------------

    <asp:GridView ID="gvProjects"
    runat="server"
    AllowPaging="false"
    AllowSorting="true"
    EnableSortingAndPagingCallbacks="false"
    AutoGenerateColumns="False"
    DataKeyNames="ProjectID"
    DataSourceID="ProjectsDataSource"
    EnableViewState="false"
    OnRowDataBound="gvProjects_RowDataBound"
    OnRowCommand="gvProjects_RowCommand">
    <Columns>
    <asp:TemplateField ShowHeader="False">
    <ItemTemplate>
    <asp:CheckBox ID="chkRowSelector" runat="server"
    OnCheckedChanged="chkRowSelector_CheckedChanged" AutoPostBack="true" /
    >

    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Project Name" SortExpression="Name">
    <ItemTemplate>
    <asp:LinkButton ID="lnkProjectName" runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "Name") %>'
    CommandName="SelectProject" CommandArgument ='<
    %#((GridViewRow)Container).RowIndex %>' ToolTip='<%#
    DataBinder.Eval(Container.DataItem, "Description") %>'
    CssClass="actionlink" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Owner" HeaderText="Created By"
    SortExpression="Owner" />
    <asp:BoundField DataField="Progress" HeaderText="Progress"
    DataFormatString="{0}%" ItemStyle-HorizontalAlign="Center"
    SortExpression="" />
    <asp:ButtonField ButtonType="Image" CommandName="DeleteProject"
    ImageUrl="~/images/icon_deleteproject.gif" >
    <ItemStyle HorizontalAlign="Center" />
    </asp:ButtonField>
    <asp:ButtonField ButtonType="Image" CommandName="EditProject" >
    <ItemStyle HorizontalAlign="Center" />
    </asp:ButtonField>
    <asp:ButtonField Text="Duplicate" CommandName="DuplicateProject" />
    </Columns>
    </asp:GridView>


    Code-behind code:
    ----------------------------
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    //only execute on initial Page_Load
    {
    ThisApplication.ClearAllSessionVariables();
    }

    else
    //only execute on PostBack
    {
    //nothing to do yet...
    }
    }

    protected void gvProjects_RowCommand(object sender,
    GridViewCommandEventArgs e)
    {
    if (e.CommandName == "DeleteProject")
    {
    int index = int.Parse(e.CommandArgument.ToString());
    uint projectID = (uint) (gvProjects.DataKeys[index].Value);

    ClearFeedback();
    Project.DeleteProject(projectID);
    //gvProjects.DataBind();
    }
    }

    protected void gvProjects_RowDataBound(object sender,
    GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    // disable checkbox if the project owner doesn't match the currently
    logged on user
    Project p = (Project)e.Row.DataItem;
    CheckBox chkb = (CheckBox)e.Row.FindControl("chkRowSelector");
    ImageButton btnViewEdit = (ImageButton)e.Row.Cells[5].Controls[0];
    ImageButton btnDelete = (ImageButton)e.Row.Cells[4].Controls[0];
    if (p.Owner != User.Identity.Name)
    {
    chkb.Enabled = false;
    btnViewEdit.CommandName = "ViewProject";
    btnViewEdit.ImageUrl = "~/images/icon_viewproject.gif";
    btnViewEdit.PostBackUrl = string.Format("~/CreateEditProject.aspx?
    mode=view&pid={0}", p.ProjectID);
    btnDelete.Visible = false;
    }
    else
    {
    chkb.Enabled = true;
    btnViewEdit.CommandName = "EditProject";
    btnViewEdit.ImageUrl = "~/images/icon_editproject.gif";
    btnViewEdit.PostBackUrl = string.Format("~/CreateEditProject.aspx?
    mode=edit&pid={0}", p.ProjectID);
    btnDelete.Visible = true;
    }
    }
    }
     
    Froefel, Aug 20, 2007
    #2
    1. Advertising

  3. Froefel

    Guest

    This is a known issue in ASP.NET. What I usually do is use a
    CommandField instead and add the path to the image in the SelectText
    property e.g.

    <asp:CommandField ButtonType="Link" HeaderText="Download"
    SelectText="&lt;img src='images/download.gif' class='borderLess' /
    &gt;" ShowSelectButton="true" />
     
    , Aug 20, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shane Bishop

    Page_Load event firing twice... wierd!!

    Shane Bishop, Jan 25, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    2,760
    Marc Schluper
    Feb 4, 2004
  2. Tim_Mac
    Replies:
    27
    Views:
    6,241
    Steven Cheng[MSFT]
    Feb 9, 2007
  3. =?Utf-8?B?Y2hlbHNlYQ==?=

    Row Command Firing twice in GridView Control

    =?Utf-8?B?Y2hlbHNlYQ==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    7,848
    Steven Cheng[MSFT]
    May 11, 2006
  4. Julia B
    Replies:
    0
    Views:
    1,389
    Julia B
    Mar 3, 2009
  5. Julia B
    Replies:
    0
    Views:
    3,174
    Julia B
    Mar 17, 2009
Loading...

Share This Page