|
|
|
start date: Mon, 20 Aug 2007 09:00:50 -0000,
posted on: microsoft.public.dotnet.framework.aspnet
back
| Thread Index |
|
1
Froefel
|
|
2
Froefel
|
|
3
unknown
|
GridView event firing twice
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
Date:Mon, 20 Aug 2007 09:00:50 -0000
Author:
|
Re: GridView event firing twice
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;
}
}
}
Date:Mon, 20 Aug 2007 09:12:36 -0000
Author:
|
Re: GridView event firing twice
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="<img src='images/download.gif' class='borderLess' /
>" ShowSelectButton="true" />
Date:Mon, 20 Aug 2007 10:22:39 -0000
Author:
|
|
|