前台代码:

<div>
<asp:DetailsView ID="gvDepart" runat="server" AutoGenerateRows="False" OnItemDeleting="gvDepart_ItemDeleting" OnItemInserting="gvDepart_ItemInserting" OnItemUpdating="gvDepart_ItemUpdating" OnModeChanging="gvDepart_ModeChanging" DataKeyNames="DepartId">
<Fields>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DepartId") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DepartId") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DepartId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DepartName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DepartName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("DepartName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ParentId") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ParentId") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ParentId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<InsertItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" Text="插入"></asp:LinkButton>
 <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="New" Text="新建"></asp:LinkButton>
<asp:LinkButton ID="btnDelete" CausesValidation="false" CommandName="Delete" CommandArgument='<%# Eval("DepartId") %>' Text="删除" runat="server" />
<asp:LinkButton ID="btnUpdate" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("DepartId") %>' Text="修改" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" CommandArgument='<%# Eval("DepartId") %>' Text="修改"></asp:LinkButton>
 <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</div>

后台代码:

public partial class WebForm4 : System.Web.UI.Page
{
DepartmentInfoBLL bll = new DepartmentInfoBLL();
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["departId"] == null) {
Response.Redirect("WebForm3.aspx");
}
if (!IsPostBack) {
InitDepart();
}
} private void InitDepart()
{
int departId = int.Parse(Request.QueryString["departId"]);
IList<DepartmentInfo> list = bll.GetByDepartId(departId);
this.gvDepart.DataSource = list;
this.gvDepart.DataBind();
} protected void gvDepart_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int departId = int.Parse(e.CommandArgument.ToString());
string departName = (this.gvDepart.Rows[1].FindControl("TextBox2") as TextBox).Text;
int parentId =int.Parse((this.gvDepart.Rows[2].FindControl("TextBox3") as TextBox).Text);
//执行修改的方法
bll.Update(new DepartmentInfo() { DepartId=departId,DepartName=departName,ParentId=parentId,Remark=""});
//返回到只读模式
this.gvDepart.ChangeMode(DetailsViewMode.ReadOnly);
//重新绑定数据
InitDepart();
} protected void gvDepart_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
int departId = int.Parse((this.gvDepart.Rows[0].FindControl("TextBox1") as TextBox).Text);
string departName = (this.gvDepart.Rows[1].FindControl("TextBox2") as TextBox).Text;
int parentId = int.Parse((this.gvDepart.Rows[2].FindControl("TextBox3") as TextBox).Text);
//执行添加的方法
bll.Insert(new DepartmentInfo() { DepartId=departId,DepartName=departName,ParentId=parentId,Remark=""});
//跳转到转到详情页面的前面一个页面
Response.Redirect("~/WebForm3.aspx");
} protected void gvDepart_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
int departId = int.Parse(e.Keys[0].ToString());
//执行删除的方法
bll.DeleteByDepartId(departId);
//跳转到转到详情页面的前面一个页面
Response.Redirect("~/WebForm3.aspx");
} protected void gvDepart_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
//改变模式
this.gvDepart.ChangeMode(e.NewMode);
InitDepart();
}
}

  

05-06 05:38