前臺代碼: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 <div> <asp:GridView ID="GridView1" runa
前臺代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<div>
<asp:GridView ID= "GridView1" runat= "server" AutoGenerateColumns= "False" OnRowCancelingEdit= "GridView1_RowCancelingEdit" OnRowDeleting= "GridView1_RowDeleting" OnRowEditing= "GridView1_RowEditing" OnRowUpdating= "GridView1_RowUpdating" DataKeyNames= "DepartId" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
部門編號
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID= "TextBox1" runat= "server" Text= '<%# Bind("DepartId") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID= "Label1" runat= "server" Text= '<%# Bind("DepartId") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
部門名稱
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID= "TextBox2" runat= "server" Text= '<%# Bind("DepartName") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID= "Label2" runat= "server" Text= '<%# Bind("DepartName") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "操作" ShowHeader= "False" >
<EditItemTemplate>
<asp:LinkButton ID= "LinkButton1" runat= "server" CausesValidation= "True" CommandName= "Update" Text= "更新" ></asp:LinkButton>
<asp:LinkButton ID= "LinkButton2" runat= "server" CausesValidation= "False" CommandName= "Cancel" Text= "取消" ></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID= "LinkButton1" runat= "server" CausesValidation= "False" CommandName= "Edit" Text= "編輯" ></asp:LinkButton>
<asp:LinkButton ID= "LinkButton2" CausesValidation= "false" CommandName= "Delete" Text= "刪除" runat= "server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
|
後臺代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public partial class WebForm3 : System.Web.UI.Page
{
DepartmentInfoBLL dbll = new DepartmentInfoBLL();
protected void Page_Load( object sender, EventArgs e)
{
if (!IsPostBack)
{
InitGridView();
}
}
private void InitGridView()
{
IList<DepartmentInfo> list = dbll.GetAll();
this .GridView1.DataSource = list;
this .GridView1.DataBind();
}
protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e)
{
int departId = int .Parse(e.Keys[0].ToString());
//執行刪除的方法
dbll.DeleteByDepartId(departId);
//重新載入數據
InitGridView();
}
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
{
this .GridView1.EditIndex = e.NewEditIndex;
InitGridView();
}
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = this .GridView1.Rows[e.RowIndex];
int departId = int .Parse(e.Keys[0].ToString());
string departName = (row.FindControl( "TextBox2" ) as TextBox).Text;
//執行修改的方法
dbll.Update( new DepartmentInfo()
{
DepartId = departId,
DepartName = departName,
Remark= ""
});
//取消編輯狀態
this .GridView1.EditIndex = -1;
//重新載入一次數據
InitGridView();
}
protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e)
{
this .GridView1.EditIndex = -1;
InitGridView();
}
}
|