datalist绑定数据,实现增删改查

本文详细介绍了ASP.NET中DataList控件的使用方法,包括如何实现数据绑定、编辑、更新、删除等功能,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    <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("删除失败");
}
}

 

转载于:https://www.cnblogs.com/gylspx/archive/2011/12/01/ssdd.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值