基于列中的值的数据绑定中继器

本文关键字:中继器 数据绑定 于列中 | 更新日期: 2023-09-27 18:06:06

我使用的是microsoft sql server 2005 &将下拉列表绑定到包含一些记录的表。在下拉列表的选定索引更改上,我希望我的中继器重新数据绑定,仅显示与下拉列表中所选值具有相同ID的记录。

下面是我的下拉列表:

<asp:DropDownList ID="ddlViewLabel" runat="server" Width="280px" 
 DataSourceID="sdsLabels" DataTextField="LabelName" DataValueField="LabelID" 
 onselectedindexchanged="ddlViewLabel_SelectedIndexChanged">
</asp:DropDownList>

这是我的中继器:

<asp:Repeater ID="rptDocuments" runat="server" OnItemCommand="viewDocument_ItemCommand"
 DataSourceID="sdsDocuments">
 <HeaderTemplate>
 </HeaderTemplate>
 <ItemTemplate>
  <div class="nav-rpt">
    <asp:LinkButton ID="lnkDocumentTitle" Text='<%# Bind("DocumentTitle") %>' runat="server"
                            CommandArgument='<%# Eval("DocumentID") %>' CssClass="nav-rpt-btn"></asp:LinkButton>
   <img src="Images/ARROW.png" style="float: right" />
  </div>
 </ItemTemplate>
 <SeparatorTemplate>
  <div style="border-top-style: solid; border-top-width: 1px; border-top-color: #C0C0C0;">
  </div>
 </SeparatorTemplate>
 <FooterTemplate>
   <div style="border-top-style: solid; border-top-width: 1px; border-top-color: #C0C0C0;">
   </div>
 </FooterTemplate>
</asp:Repeater>

以下是我的2个数据源:

    <asp:SqlDataSource ID="sdsDocuments" runat="server" ConnectionString="<%$ ConnectionStrings:blcDocumentationConnectionString %>"
            SelectCommand="SELECT [DocumentID], [DocumentTitle], [DocumentBody], [ModifiedDate], [CreatedDate] FROM [tblDocument]">
        </asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsLabels" runat="server" ConnectionString="<%$ ConnectionStrings:blcDocumentationConnectionString %>"
            SelectCommand="SELECT [LabelID], [LabelName] FROM [tblLabel]"></asp:SqlDataSource>
<asp:SqlDataSource ID="sdsLink" runat="server" 
  ConnectionString="<%$ ConnectionStrings:blcDocumentationConnectionString %>" 
  SelectCommand="SELECT [LabelID], [DocumentID], [LinkID] FROM [tblLink]"></asp:SqlDataSource>

我在哪里输入逻辑来过滤显示具有正确'LabelID'的'Documents'的重复器?

基于列中的值的数据绑定中继器

您需要为下拉列表上定义的ddlViewLabel_SelectedIndexChanged方法编写一些代码

protected void ddlViewLabel_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList d = (DropDownList)sender;
// psudeo code from here on out
// get the DropDownList selected index
// create the new SQL statement
// either create a new SQlDataSource or SqlCommand/SqlConnection objects, set the sql, attach them to the repeater, bind
}