链接上一篇“筛选”
在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();
最后效果图如下所示: