<asp:DataList ID="DataList1" runat="server" CellPadding="4"
DataSourceID="ObjectDataSource1" ForeColor="#333333" Width="243px"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
onitemcommand="DataList1_ItemCommand" onupdatecommand="DataList1_UpdateCommand">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#E3EAEB" />
<SelectedItemStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
序号
<asp:Label ID="Label1" runat="server" Text='<%# Container.ItemIndex+1 %>'></asp:Label>
<br />
姓名
<asp:Label ID="Label2" runat="server" Text='<%#Eval("UName") %>' ></asp:Label>
<br />
密码
<asp:Label ID="Label3" runat="server" Text='<%#Eval("UPwd") %>'></asp:Label>
<br />
<%-- commamdName 必须是正确的delete edit update cancel--%>
<asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%#Eval("UId") %>' CommandName="edit">编辑</asp:LinkButton>
<asp:LinkButton ID="lnkDel" runat="server" CommandArgument='<%#Eval("UId") %>' CommandName="delete">删除</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
序号
<asp:Label ID="Label1" runat="server" Text='<%# Container.ItemIndex+1 %>'></asp:Label>
<br />
姓名
<asp:textBox ID="txtname" runat="server" Text='<%#Eval("UName") %>' ></asp:textBox>
<br />
密码
<asp:textBox ID="txtpwd" runat="server" Text='<%#Eval("UPwd") %>'></asp:textBox>
<br />
<asp:LinkButton ID="lnkUpdate" runat="server" CommandArgument='<%#Eval("UId") %>' CommandName="Update">更新</asp:LinkButton>
<asp:LinkButton ID="lnkCancle" runat="server" CommandArgument='<%#Eval("UId") %>' CommandName="cancel">取消</asp:LinkButton>
</EditItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="MyPhotoList.Model.User" DeleteMethod="Delete"
InsertMethod="Add" SelectMethod="GetAllList" TypeName="MyPhotoList.BLL.User"
UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="UId" Type="Int32" />
</DeleteParameters>
</asp:ObjectDataSource>
cs:
//点击编辑 显示编辑项模版
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
//取消
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
//更新
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
MyPhotoList.BLL.User bll = new MyPhotoList.BLL.User();
MyPhotoList.Model.User model = new MyPhotoList.Model.User();
model.UId = Convert.ToInt32(e.CommandArgument);
TextBox txt1 = e.Item.FindControl("txtname") as TextBox;
TextBox txt2 = e.Item.FindControl("txtpwd") as TextBox;
if (txt1!=null )
{
model.UName = txt1.Text;
}
if (txt2!=null )
{
model.UPwd = txt2.Text;
}
if (bll.Update(model))
{
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
else
{
Response.Write("更新失败");
}
}
//删除
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);
MyPhotoList.BLL.User bll = new MyPhotoList.BLL.User();
if (bll.Delete(id))
{
//删除成功重新绑定
DataList1.DataBind();
}
else
{
Response.Write("删除失败");
}
}