Reputation: 51
I am trying to use images representing arrows to allow the user to change the order in which items appear in a list in a grid view in ASP.NET.
I have a class which has a value named "position", the class is displayed inside the GridView and is ordered by position. In each of the rows of the gridview are an up and down arrow which i want to change the value of "position" for the object represent by the row of the gridview. Whats the easiest way to do this?
ASP -
<br /><strong>Previous Employment: </strong> <br />
Pick one of your previous employers listed below to update/delete or <asp:HyperLink ID="PreviousEmploymentLink" runat="server" />.
<asp:GridView runat="server" ID="EmploymentDataGrid" AutoGenerateColumns="false" OnRowDeleting="EmploymentDataGrid_onDeleting"
DataKeyNames="EmployerId" SkinID="FullWidthGrid" >
<EmptyDataTemplate>
<p>No Previous Employment added yet</p>
</EmptyDataTemplate>
<Columns>
<asp:BoundField HeaderText="Dates" DataField="Dates" />
<asp:BoundField HeaderText="Employer's Name" DataField="EmployerName" />
<asp:BoundField HeaderText="Job Description" DataField="JobDescription" />
<asp:BoundField HeaderText="Job Title" DataField="JobTitle" />
<asp:HyperLinkField HeaderStyle-Width="30px" DataNavigateUrlFields="EmployerId"
HeaderText="Edit" Text="<img src='../../../Images/edit.gif' alt='Edit Employment' border='0'/>"
DataNavigateUrlFormatString="UpdatePreviousEmployment.aspx?PreviousEmploymentId={0}" />
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="45px">
<ItemTemplate>
<asp:ImageButton AlternateText="Delete User" ID="DeleteButton" runat="server" CommandName="Delete" ImageUrl="~/Images/delete.gif" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Position" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="45px">
<ItemTemplate>
<asp:ImageButton AlternateText="Move Up" ID="UpPositionButton" runat="server" CommandName="MoveEmploymentUp" OnClick="MoveEmploymentUp" ImageUrl="~/Images/arrow_up_green.gif" />
<asp:ImageButton AlternateText="Move Down" ID="DownPositionButton" runat="server" CommandName="MoveEmploymentDown" ImageUrl="~/Images/arrow_down_green.gif" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Upvotes: 4
Views: 1289
Reputation: 97
I have a few small remarks:
Nitpicking: movingEmployment whould be movingEmployee
//the following assignement is not used.
Employer emp = (Employer)g.DataItem;
//This object should not be constructed by default.
Employer otherEmployment = new Employer();
eDao.SaveOrUpdate(movingEmployment);
//eDao.CommitChanges();
//I did not commit because if something went wrong right now,
//then the data for one would be updated but the other not.
eDao.SaveOrUpdate(otherEmployment);
eDao.CommitChanges();
The revised code (slightly tweaked for performance) could be.
protected void MoveEmploymentUp(object sender, ImageClickEventArgs e)
{
ICVDao cvdao = DaoFactory.GetCVDao();
CV currentCv = cvdao.GetById(Int32.Parse(Request.Params["CVID"]), false);
//Can only work with at least two items in the list.
if(currentCv.Employment.Count > 1)
{
#region Get to the selected Employee reference
IEmployerDao eDao = DaoFactory.GetEmployerDao();
ImageButton i = (ImageButton)sender;
GridViewRow g = (GridViewRow)i.Parent.Parent;
EmploymentDataGrid.SelectedIndex = g.RowIndex;
Employer selectedEmployee = eDao.GetById(int.ParseEmploymentDataGrid.SelectedDataKey.Value.ToString()), false);
#endregion
int selectedPosition = selectedEmployee.Position;
if (selectedPosition != 1)
{
#region find employee to swap with
Employer swapWithEmployee = null;
//I assume currentCv.Employment is only an IEnumerable.
//If it was an Collection we would not have to loop.
foreach (Employer findSwapEmployee in currentCv.Employment)
{
if (findSwapEmployee.Position == selectedPosition- 1)
{
swapWithEmployee= findSwapEmployee;
break;
}
}
#endregion
#region perform and commit swap
if(swapWithEmployee != null)
{
selectedEmployee.Position -= 1;
swapWithEmployee.Position = selectedPosition;
eDao.SaveOrUpdate(selectedEmployee);
eDao.SaveOrUpdate(swapWithEmployee);
eDao.CommitChanges();
EmploymentGridBind();
}
#endregion
}
}
Upvotes: 1
Reputation: 51
protected void MoveEmploymentUp(object sender, ImageClickEventArgs e)
{
ICVDao cvdao = DaoFactory.GetCVDao();
CV currentCv = cvdao.GetById(Int32.Parse(Request.Params["CVID"]), false);
IEmployerDao eDao = DaoFactory.GetEmployerDao();
ImageButton i = (ImageButton)sender;
/*
* if chap above
* Swap numbers with chap above
* if not
* dont swap
*/
GridViewRow g = (GridViewRow)i.Parent.Parent;
Employer emp = (Employer)g.DataItem;
EmploymentDataGrid.SelectedIndex = g.RowIndex;
Employer movingEmployment = eDao.GetById(int.Parse(EmploymentDataGrid.SelectedDataKey.Value.ToString()), false);
Employer otherEmployment = new Employer();
if (movingEmployment.Position != 1 && currentCv.Employment.Count > 1)
{
foreach (Employer em in currentCv.Employment)
{
if (em.Position == movingEmployment.Position - 1)
otherEmployment = em;
}
movingEmployment.Position -= 1;
otherEmployment.Position += 1;
eDao.SaveOrUpdate(movingEmployment);
eDao.CommitChanges();
eDao.SaveOrUpdate(otherEmployment);
eDao.CommitChanges();
EmploymentGridBind();
//InstructionsLabel.Text = "Mover Dates:" +movingEmployment.Dates+ " ID:" + movingEmployment.EmployerId + " Position:" + movingEmployment.Position +
// "<br />Other Dates:" + otherEmployment.Dates + " ID:" + otherEmployment.EmployerId + " Position:" + otherEmployment.Position;
}
}
Had to get the datakey value via the gridview object as opposed to the gridviewrow, using that, the rest was easy
Upvotes: 1