zoukankan      html  css  js  c++  java
  • Linq to sql(三):增删改(三)

    前面创建Linq to sql Classes的时候我们输入名字GuestBook,系统就为我们自动创建了GuestBookDataContext

     (你也可以在GuestBook.Designer.cs中找到类定义)。在绑定的时候我们使用查询句法查询留言表中所有留言,

    按照发表时间倒序(天哪?这是数据访问吗?好像仅仅定义了一句SQL啊)。在发表留言按钮中,

    我们为一个tbGuestBook赋值,然后把它加入留言表,再提交更改,就这样完成了记录的插入。

           运行效果如下图:


           然后,再创建一个Admin.aspx,前台代码如下:

    <div>

            <asp:Repeater ID="rpt_Message" runat="server" OnItemCommand="rpt_Message_ItemCommand">

            <ItemTemplate>

            <table width="600px" style="border:solid 1px #666666; font-size:10pt; background-color:#f0f0f0">

                <tr>

                <td align="left" width="400px">

                <%# Eval("Message")%>

                </td>

                <td align="right" width="200px">

                <%# Eval("PostTime")%> - <%# Eval("UserName")%>

                </td>

                </tr>

                <tr>

                <td colspan="2" align="right">

                <hr width="300px" />

                <asp:Button ID="btn_DeleteMessage" runat="server" Text="删除留言" CommandName="DeleteMessage" CommandArgument='<%# Eval("ID")%>'/>

                管理员回复:<asp:TextBox runat="server" ID="tb_Reply" TextMode="MultiLine" Width="300px" Text='<%# Eval("Reply")%>'/>

                <asp:Button ID="btn_SendReply" runat="server" Text="发表回复" CommandName="SendReply" CommandArgument='<%# Eval("ID")%>'/>

                </td>

                </tr>          

            </table>

            <br/>

            </ItemTemplate>

            </asp:Repeater>

        </div>

           后台代码:

     

    public partial class Admin : System.Web.UI.Page

    {

        GuestBookDataContext ctx = new GuestBookDataContext("server=xxx;database=GuestBook;uid=xxx;pwd=xxx");

           

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                SetBind();

            }

        }

     

        private void SetBind()

        {

            rpt_Message.DataSource = from gb in ctx.tbGuestBooks orderby gb.PostTime descending select gb;

            rpt_Message.DataBind();

        }

        protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)

        {

            if (e.CommandName == "DeleteMessage")

            {

                StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);

                ctx.Log = sw;

                tbGuestBook gb = ctx.tbGuestBooks.Single(b => b.ID == new Guid(e.CommandArgument.ToString()));

                ctx.tbGuestBooks.Remove(gb);

                ctx.SubmitChanges();

                SetBind();

                sw.Close();

            }

            if (e.CommandName == "SendReply")

            {

                StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);

                ctx.Log = sw;

                tbGuestBook gb = ctx.tbGuestBooks.Single(b => b.ID == new Guid(e.CommandArgument.ToString()));

                gb.Reply = ((TextBox)e.Item.FindControl("tb_Reply")).Text;

                gb.IsReplied = true;

                ctx.SubmitChanges();

                SetBind();

                sw.Close();

            }

        }

    }

       

  • 相关阅读:
    .Net Core ----通过XUnit进行接口单元测试(带请求头及参数)并用output输出结果
    .Net Core---- 通过EPPlus批量导出
    .Net Core---- 自带Json返回日期带T格式 解决
    You need tcl 8.5 or newer in order to run the Redis test
    exec: "docker-proxy": executable file not found in $PATH
    docker 如何清理垃圾呢
    docker run 报错——WARNING: IPv4 forwarding is disabled. Networking will not work.
    go 依赖包管理工具gb安装报错
    keepalived实现nginx高可用
    php命令行查看扩展信息
  • 原文地址:https://www.cnblogs.com/kevin2013/p/1749099.html
Copyright © 2011-2022 走看看