I have a grid view which uses a Datasource to populate it. There is also a OnRowDataBound method which does lots of work using templatefields etc.
I have a button which runs a delete method in the code behind, but when i use it the row i delete is still displayed on postback. I tried to set EnableViewState to false for the grid view, but then parts of the OnRowDataBound method failed where a variable set in the Page_Load is used.
Any ideas where i am going wrong? hope this is enough (and not too much) info - if you need more, let me know.
cheers
monkey
The row is updating in the DB.
Code behind:
public partial class team_teammembers : System.Web.UI.Page
{
Permissions curPermissions;
UserDetails curUser;
protected int[] queryOutcome = { 0, 0 };
protected void Page_Load(object sender, EventArgs e)
{
curPermissions = (Permissions)Session["CurrentSystemPermission"];
curUser = (UserDetails)Session["LoggedInDetails"];
}
protected void gvTeam_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//set the colour and role/area for each row (the sort order is set as 1=inspector, 2=CAPT sergeant, 3=NPT Sergeant, 4=CBM, 5=all others (PositionTtitle is used in these instances).
if ((int)DataBinder.Eval(e.Row.DataItem, "Sort") == 1)
{
e.Row.BackColor = System.Drawing.Color.FromName("#FFFF99");
((Label)e.Row.FindControl("lblRoleArea")).Text = "<strong>Sector:</strong>" + (string)DataBinder.Eval(e.Row.DataItem, "SectorName");
((Button)e.Row.FindControl("lbtnDelete")).CommandArgument = "1;" + DataBinder.Eval(e.Row.DataItem, "SectorID");
}
else if ((int)DataBinder.Eval(e.Row.DataItem, "Sort") == 2)
{
e.Row.BackColor = System.Drawing.Color.FromName("#FF99CC");
((Label)e.Row.FindControl("lblRoleArea")).Text = "<strong>CAPT:</strong>" + (string)DataBinder.Eval(e.Row.DataItem, "CAPTName");
((Button)e.Row.FindControl("lbtnDelete")).CommandArgument = "2;" + DataBinder.Eval(e.Row.DataItem, "CAPTID");
}
else
{
((Label)e.Row.FindControl("lblRoleArea")).Text = "<strong>Other</strong>";
((Button)e.Row.FindControl("lbtnDelete")).CommandArgument = "5;" + DataBinder.Eval(e.Row.DataItem, "ColNo");
}
((Label)e.Row.FindControl("lblName")).Text = (string)DataBinder.Eval(e.Row.DataItem, "Title") + " " + (string)DataBinder.Eval(e.Row.DataItem, "Forename") + " " + (string)DataBinder.Eval(e.Row.DataItem, "Surname");
if ((string)DataBinder.Eval(e.Row.DataItem, "TelExt") != "")
{
((Label)e.Row.FindControl("lblTelExt")).Text = (string)DataBinder.Eval(e.Row.DataItem, "TelExt").ToString();
}
if ((curPermissions.PermissionID == 1))
{
((Button)e.Row.FindControl("lbtnEdit")).Visible = true;
((Button)e.Row.FindControl("lbtnDelete")).Visible = true;
((Button)e.Row.FindControl("lbtnMove")).Visible = true;
}
}
}
protected void gvTeam_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "deleteMember")
{
if (currentArg[0] == "1")
{
queryOutcome = UserDetails.UpdateSector(int.Parse(currentArg[1]));
}
else if (currentArg[0] == "2")
{
queryOutcome = UserDetails.UpdateCAPT(int.Parse(currentArg[1]));
}
else
{
queryOutcome = UserDetails.DeleteTeamMember(int.Parse(currentArg[1]));
}
}
}
}
Page:
<asp:GridView ID="gvTeam" runat="server" DataSourceID="odsTeam" AutoGenerateColumns="False" CellPadding="5" OnRowDataBound="gvTeam_RowDataBound" AllowSorting="True" AllowPaging="true" PageSize="50" OnRowCommand="gvTeam_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Role/Area" SortExpression="Sort" ItemStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="lblRoleArea" runat="server" Text='<strong>Role</strong><br />Area' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Surname">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact">
<ItemTemplate>
<strong>Extension:</strong> <asp:Label ID="lblTelExt" runat="server" Text='Not Available'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
<asp:Label ID="lblActions" runat="server" Text='To make changes to this persons<br />details or role, please<br />contact them direct'></asp:Label>
<asp:Button ID="lbtnEdit" runat="server" Visible="false" Text="Edit" />
<asp:Button ID="lbtnDelete" runat="server" Visible="false" CommandName="deleteMember" CommandArgument="" Text="Delete" />
<asp:Button ID="lbtnMove" runat="server" Visible="false" Text="Move" />
<
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsTeam" runat="server" SelectMethod="GetTeam" TypeName="UserDetails" DataObjectTypeName="UserDetails" UpdateMethod="UpdateTeamMember" SortParameterName="sortExpression">
<SelectParameters>
<asp:QueryStringParameter Name="ID" QueryStringField="id" Type="string" />
<asp:Parameter Name="sortExpression" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</asp:Content>
Have you confirmed that the row is deleted from the database? If you have then maybe showing some of your codes will be a good starting point
is that all the code, because I think you need to re-bind the gridview in order for the deleted row to no longer be seen on postback.