我在ASP.NET中使用Linq-to-SQL& C#。我想通过点击删除按钮(即CommandField
或TemplateField
)和gridTrainingNeed_RowDeleting
事件来删除gridview的各行。
这是我在页面加载时加载gridview的代码。
int departmentId;
int branchId;
int positionId;
double month;
int levelId;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initialize();
}
}
public void Initialize()
{
ddlBranch.DataSource = TrainingManager.GetBranchName();
ddlBranch.DataBind();
ddlDepartment.DataSource = DepartmentManager.GetAllDepartments();
ddlDepartment.DataBind();
ddlPosition.DataSource = TrainingManager.GetDesignationName();
ddlPosition.DataBind();
ddlLevel.DataSource = TrainingManager.GetLevelName();
ddlLevel.DataBind();
gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
gridTrainingNeed.DataBind();
}
现在我要保留我的设计代码:
<div class="innerLR">
<div class="seperator bottom" />
<div class="widget">
<div class="widget-head">
<h4 class="heading">
Send Circulation To
</h4>
</div>
<div class="widget-body">
<table class="tableShow" cellpadding="3" cellspacing="0">
<tr>
<td>
<asp:DropDownList ID="ddlBranch" runat="server" DataTextField="Name"
DataValueField="BranchId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Branch---"/>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server" AppendDataBoundItems="True"
DataTextField="Name" Width="170px" DataValueField="DepartmentId">
<asp:ListItem Text="---Select Department---"/>
</asp:DropDownList></td>
<td>
<asp:DropDownList ID="ddlLevel" runat="server" DataTextField="Name"
DataValueField="LevelId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Level---"/>
</asp:DropDownList></td>
<td>
<asp:DropDownList ID="ddlPosition" runat="server" DataTextField="Name"
DataValueField="DesignationId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Position---"/>
</asp:DropDownList></td>
</tr>
<tr>
<td>
<br />
Send Circulation To</td>
<td>
<br />
Month</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlCirculation" runat="server" DataTextField="Name"
DataValueField="PositionId" Width="170px" AutoPostBack="true" AppendDataBoundItems="True"
onselectedindexchanged="ddlCirculation_SelectedIndexChanged">
<asp:ListItem Text="" Value="-1" />
<asp:ListItem Text="All" Value="0" />
<asp:ListItem Text="Must Have Worked For" Value="1" />
</asp:DropDownList></td>
<td>
<asp:TextBox ID="txtMonth" Width="150px" runat="server" Enabled="false"></asp:TextBox></td>
<td>
<asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" /></td>
<td>
</td>
</tr>
</table>
</div>
</div>
<div>
<asp:GridView ID="gridTrainingNeed" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" CellPadding="4" ForeColor="#333333"
GridLines="None" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="EID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Branch" HeaderText="Branch" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="Level" HeaderText="Level" />
<asp:BoundField DataField="Position" HeaderText="Position" />
<asp:CommandField HeaderText="Delete" ShowCancelButton="False"
ShowDeleteButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div>
</div>
因此,我希望使用RowDeleting事件逐行删除行,而不是从数据库中显示数据。
protected void gridTrainingNeed_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//Row delete code
}
谢谢。
答案 0 :(得分:1)
而不是像你一样直接分配DataSource
:
gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
将结果存储在页面级别列表中。定义页面级别列表,如:
//Define List at page/class level
List<Employee> gridList = new List<Employee>();
然后在您的方法Initialize
gridList = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
gridTrainingNeed.DataSource = gridList;
gridTrainingNeed.DataBind();
您需要在PostBack,使用Session / ViewState等内存中保留List<Employee>
(请参阅ASP.Net State Management)。
稍后在gridTrainingNeed_RowDeleting
中,从行或任何唯一标识员工的字段中获取ID
。查询列表并从内存中的列表中删除该对象。将DataSource重新分配给修改后的列表并调用DataBind。