zoukankan      html  css  js  c++  java
  • datalist绑定数据,实现增删改查

        <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("删除失败");
    }
    }
  • 相关阅读:
    C#利用反射动态调用类及方法
    系统程序监控软件
    SQL server 2008 安装和远程访问的问题
    sql server 创建临时表
    IIS 时间问题
    windows 2008 安装 sql server 2008
    sql server xml nodes 的使用
    Window 7sp1 安装vs2010 sp1 打开xaml文件崩溃
    CSS资源网址
    Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0
  • 原文地址:https://www.cnblogs.com/gylspx/p/ssdd.html
Copyright © 2011-2022 走看看