• 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("删除失败");
    }
    }
  • 相关阅读:
    学习类的网站链接
    聊聊Web App、Hybrid App与Native App的设计差异
    Telnet是什么意思又是什么协议 Telnet有什么作用及功能
    telnet命令的使用方法
    HTTP协议详解(真的很经典)(转载)
    数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题
    Mysql mysql lost connection to server during query 问题解决方法
    MySQL 分组之后如何统计记录条数 gourp by 之后的 count()
    学习生活必须知道的网站或者App
    shell脚本使用## or %%
  • 原文地址:https://www.cnblogs.com/gylspx/p/ssdd.html
Copyright © 2020-2023  润新知