EF的查询对博文展示和详情案例。

该博客展示了如何在ASP.NET中实现博客列表的展示,包括类型筛选和内容摘要。通过DropDownList进行类型筛选,使用Repeater控件展示博客条目,内容以省略号形式截断,并通过超链接跳转至详情页。博客详情页接收到上一页传递的ID,显示完整的博文详情。
摘要由CSDN通过智能技术生成

 

目录

展示页:

前端代码:

在Page_Load事件中绑定下拉框的数据来源和Repeater控件的数据源

使用DropDownList1_SelectedIndexChanged事件实现根据下拉框的值进行筛选查询

详情页:

前端代码:

绑定详情页数据源:

接收上一个页面传递过来的值


展示页效果:

点击内容查看博文详情:

展示页:

前端代码:

<style type="text/css">
        *{
            /*HyperLink标签去下划线*/
            text-decoration:none;
            color:black;
        }

        .auto-style2 {
            width: 617px;
            margin: 0px auto;
        }
        
        .auto-style3 {
            width: 616px;
        }

        td {
            width:610px;
            height:80px;
            white-space:nowrap;/*规定段落的文本不进行换行*/
            overflow:hidden;/*关闭滚动条*/
            text-overflow:ellipsis;/*溢出的文字显示为省略号*/
        }
        .bw{
            margin-left:60%;
        }
    </style>

 <div class="auto-style2">
            类型:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
            <asp:HyperLink ID="HyperLink2" CssClass="bw" runat="server" NavigateUrl="~/WebForm1.aspx">写博文</asp:HyperLink>
            
            <hr />
            <%--table-layout:fixed;内部布局固定大小,这个时候width就不会随着td长度变长--%>
            <table class="auto-style3" style="table-layout:fixed;">
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <tr>
                            <th class="auto-style1">
                                <h2 style="text-align: left;"><%# Eval("tide")%></h2>
                            </th>
                        </tr>
                        <tr>

                            <th class="auto-style1" style="text-align: left;">
                                <%# Eval("auathor")%>
                                <samp><%# Eval("time")%></samp>
                            </th>
                        </tr>
                        <tr>
                            <td>
                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#"~/WebForm3.aspx?id="+Eval("id")%>'><%# Eval("content")%></asp:HyperLink>
                                <hr />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>

这里要注意table、td的样式和页面传值,确保博文内容不会全部展示出来。NavigateUrl='<%#"~/WebForm3.aspx?id="+Eval("id")%>'通过?别名=“传递的值”,这种方式将值传递到下一个页面

 

在Page_Load事件中绑定下拉框的数据来源和Repeater控件的数据源

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定Repeater的数据源方法
                sele();
                //绑定DropDownList的数据源
                this.DropDownList1.DataSource = My.Catelog.ToList();
                this.DropDownList1.DataValueField = "Id";
                this.DropDownList1.DataTextField = "Name";
                this.DropDownList1.DataBind();
            }
        }
        void sele()
        {
            //使用linq查询数据上下文Article表数据
            var art = from article in My.Article
                      join catelog in My.Catelog on article.Catelogid equals catelog.Id//使用join连表查询
                      select new
                      {
                          id = article.Id,
                          tide = article.tide,
                          auathor = article.Auathor,
                          content = article.Content,
                          time = article.time,
                      };//挑选出自己要的数据字段,在用匿名对象存放
            // 将查询出的数据绑定到Repeater1控件中
            this.Repeater1.DataSource = art.ToList();
            this.Repeater1.DataBind();
        }

 

使用DropDownList1_SelectedIndexChanged事件实现根据下拉框的值进行筛选查询

//获取DropDownList1下标
            int id = this.DropDownList1.SelectedIndex+1;
            if (id == 1)
            {
                sele();
            }
            else
            {
                //使用linq查询数据上下文Article表数据
                var art = from article in My.Article
                          join catelog in My.Catelog on article.Catelogid equals catelog.Id//使用join连表查询
                          where catelog.Id == id//筛选条件
                          select new
                          {
                              id = article.Id,
                              tide = article.tide,
                              auathor = article.Auathor,
                              content = article.Content,
                              time = article.time,
                          };//挑选出自己要的数据字段,在用匿名对象存放
                            // 将查询出的数据绑定到Repeater1控件中
                this.Repeater1.DataSource = art.ToList();
                this.Repeater1.DataBind();

这里要注意DropDownList中的AutoPostBack属性值为"True",不然点了没反应

详情页:

前端代码:

<style type="text/css">
        * {
            /*HyperLink标签去下划线*/
            text-decoration: none;
            color: black;
        }

        .box {
            width: 1000px;
            margin: 0px auto;
        }

        .auto-style1 {
            width: 728px;
        }

        .auto-style2 {
            width: 797px;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="box">
            <table class="auto-style2">
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <tr>
                            <th class="auto-style1">
                                <h2 style="text-align: left;"><%# Eval("tide")%></h2>
                            </th>
                        </tr>
                        <tr>

                            <th class="auto-style1" style="text-align: left;">
                                <%# Eval("auathor")%>
                                <samp><%# Eval("time")%></samp>
                            </th>
                        </tr>
                        <tr>
                            <td>
                                <%# Eval("content")%>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>
    </form>
</body>

绑定详情页数据源:

接收上一个页面传递过来的值

 

int id = int.Parse(Request.QueryString["id"]);

使用传过来的值筛选出博文的详情

//创建一个EF数据上下文对象
        MyBKEntities1 My = new MyBKEntities1();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //接收get提交过来的值
                int id = int.Parse(Request.QueryString["id"]);
                //使用linq查询数据上下文Article表数据
                var art = from article in My.Article
                          join catelog in My.Catelog on article.Catelogid equals catelog.Id//使用join连表查询
                          where article.Id == id//筛选条件
                          select new
                          {
                              id = article.Id,
                              tide = article.tide,
                              auathor = article.Auathor,
                              content = article.Content,
                              time = article.time,
                          };//挑选出自己要的数据字段,在用匿名对象存放
                            // 将查询出的数据绑定到Repeater1控件中
                this.Repeater1.DataSource = art.ToList();
                this.Repeater1.DataBind();
            }
        }

 

案例到此结束,如有问题欢迎评论,如有更好方案可以一起讨论,万分感谢

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EF6 是 Entity Framework 6 的简称,它是微软开发的一种对象关系映射(ORM)工具。T-SQL 是 Transact-SQL 的简称,是一种用于 SQL Server 数据库的查询语言。 在 EF6 查询性能和 T-SQL 对比方面,有以下几点需要考虑: 1. 执行计划:EF6 会将 LINQ 查询转换为相应的 SQL 查询语句,然后由数据库服务器执行。而 T-SQL 直接在数据库服务器上执行,因此在执行计划的生成和优化方面,T-SQL 通常更加高效。 2. 数据库适应性:T-SQL 可以充分利用数据库的特性和优化策略,如索引、分区等。而 EF6 在生成 SQL 查询语句时,可能无法充分利用数据库的特性,导致性能较低。 3. 缓存机制:EF6 提供了查询结果的缓存机制,可以在一定程度上提高查询性能。而 T-SQL 没有内置的缓存机制,需要手动实现。 4. 查询表达能力:EF6 使用 LINQ 查询语法,提供了强类型的查询表达能力,可以更加直观和灵活地进行查询。而 T-SQL查询表达能力相对较弱,需要手动编写 SQL 查询语句。 综上所述,EF6 在查询性能方面可能不如 T-SQL,特别是在复杂查询和大数据量的情况下。如果对性能要求较高,可以考虑直接使用 T-SQL 编写查询语句。但是,EF6 提供了更方便和灵活的开发方式,并且在简单查询和开发效率方面具有优势。所以,在选择使用 EF6 还是 T-SQL 时,需要根据具体场景和需求进行权衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值