2021-05-29

链接上一篇“筛选”

在ArticleDAO类里面编写代码

//根据类别查询
        public List<Article> SelectByCatelogId(int id)
        {
            MyDBEntities db = new MyDBEntities();
            var result = from article in db.Article
                         where article.Catelogid == id
                         select article;
            return result.ToList();
        }

然后在AritcleService类编写代码

 //根据ID查询所有
        public Article Select(int id)
        {
            return aAdo.Select(id);
        }

然后在WebApplication4里面添加一个新的web窗体Insex
在这里插入图片描述
然后双击Insex页面进去编写代码

<div>
            <div class="row">
                <label class="cols-6">类别:</label>
                <div class="cols-6">
                //添加OnSelectedIndexChanged事件
                     <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name" DataValueField="Id" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
                </div>
            </div>

            <asp:Repeater ID="Repeater1" runat="server">
               <ItemTemplate>
                   <div class="row">
                     <div class="row"> <%# Eval("Title") %></div>
                     <div class="row">
                        <label class="cols-6">作者:<%# Eval("Author") %></label>
                        <label class="cols-6">发布时间:<%# Eval("PushDate") %></label>
                     </div>
                       <div class="row">
                           <%# Eval("Coutent") %>
                       </div>
                   </div>
               </ItemTemplate>
           </asp:Repeater>

        </div>

然后在本页面编写样式

<style type="text/css"> 
        div{
            border:1px solid red;
            width:2000px;
            height:30px;
            
        }
        .row{
            display:flex;
            flex-direction:row;
        }
        .cols-6{
            flex:0 0 50%;
        }
        label.cols-6{
            text-align:right;
        }
    </style>

双击Insex打开
在这里插入图片描述
编写代码

 //私有的 实例化,只能在当前类使用
        private AritcleService aritcleService = new AritcleService();
        private CatelogService catelogService = new CatelogService();

然后再编写绑定控件

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //下拉框控件绑定查询方法
                this.DropDownList1.DataSource = catelogService.Select();
                this.DropDownList1.DataBind();

                this.Repeater1.DataSource = aritcleService.Select();
                this.Repeater1.DataBind();
            }
        }

然后编写DropDownList 控件的点击事件

int id = int.Parse(this.DropDownList1.SelectedValue);
            this.Repeater1.DataSource = aritcleService.SelectByCatelogId(id);
            this.Repeater1.DataBind();

最后效果图如下所示:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值