使用数据列表显示搜索结果

时间:2019-08-03 15:13:01

标签: asp.net vb.net datalist

我正在使用数据列表显示搜索结果,例如,我正在检索此字段示例 enter image description here

我正在使用Text='<%# Eval("field") %>'在其相应标签中检索名称和描述,并使用像这样的后面的代码检索类别和用户

Dim CategoryLabel As Label = DirectCast(DataList1.Items(0).FindControl("CategoryLabel"), Label)
CategoryLabel.Text = DirectCast(dt2.Rows(0)(0), String)
Dim UserLabel As Label = DirectCast(DataList1.Items(0).FindControl("UserLabel"), Label)
UserLabel.Text = DirectCast(dt2.Rows(0)(1), String)

仅当您看到该功能时,它才能对第一个项目起作用,但是该项目的其余部分显示为空白。 原因是使用这种方法,即我将用户和类别的值作为外键(ID'S)存储到除将数据列表绑定到该表以外的其他表上,并且我想在其中显示文本而不是ID号搜索结果。如何使这两个标签(类别和用户)以类似的方式填充,例如带有Eval的标签。

P.S。所有四个标签都放在一个<ItemTemplate>

完整代码

  If (con.State = ConnectionState.Closed) Then
        con.Open()
    End If
    Dim command As SqlDataAdapter
    command = New SqlDataAdapter("select * FROM Lostitem WHERE city=@city AND datelost=@datelost AND (name Like '%' + @name + '%' OR name Like '%' + '""' + '%') ", con)
    Dim ide2 As Integer = ddlCities.SelectedValue
    command.SelectCommand.Parameters.AddWithValue("@City", ide2)
    command.SelectCommand.Parameters.AddWithValue("@datelost", TextBox1.Text)
    command.SelectCommand.Parameters.AddWithValue("@name", TextBox2.Text)
    Dim DetailsAdapter2 As SqlDataAdapter

    Dim dt1 As New DataTable

    command.Fill(dt1)
    DataList1.DataSource = dt1
    DataList1.DataBind()
    Dim rowcount As Integer = dt1.Rows.Count
    Dim idd As Integer
    For row = 0 To rowcount - 1
        idd = dt1.Rows(row)(0)



        DetailsAdapter2 = New SqlDataAdapter(" select Category.subcategory, Users.username from [Category]  JOIN [LostItem] ON (Category.CategoryID = LostItem.CategoryID) JOIN [Users] ON (LostItem.[User] = Users.[userid] ) WHERE (LostItem.LostId=" & idd & "    ) ", con)
        Dim dt2 As New DataTable

        DetailsAdapter2.Fill(dt2)
        Dim CategoryLabel As Label = DirectCast(DataList1.Items(0).FindControl("CategoryLabel"), Label)
        CategoryLabel.Text = DirectCast(dt2.Rows(0)(0), String)
        Dim UserLabel As Label = DirectCast(DataList1.Items(0).FindControl("UserLabel"), Label)
        UserLabel.Text = DirectCast(dt2.Rows(0)(1), String)

    Next
    con.Close()
    ResultPanel.Style.Add("display", "block")

1 个答案:

答案 0 :(得分:0)

我在dt2中又添加了一个循环。我对vb代码不熟悉。所以请检查语法。

For row = 0 To rowcount - 1
    idd = dt1.Rows(row)(0)



    DetailsAdapter2 = New SqlDataAdapter(" select Category.subcategory, Users.username from [Category]  JOIN [LostItem] ON (Category.CategoryID = LostItem.CategoryID) JOIN [Users] ON (LostItem.[User] = Users.[userid] ) WHERE (LostItem.LostId=" & idd & "    ) ", con)
    Dim dt2 As New DataTable

    DetailsAdapter2.Fill(dt2)

 Dim rowcount1 As Integer = dt2.Rows.Count
 For row1 = 0 To rowcount1 - 1


    Dim CategoryLabel As Label = DirectCast(DataList1.Items(0).FindControl("CategoryLabel"), Label)
    CategoryLabel.Text = DirectCast(dt2.Rows(row1)(0), String)
    Dim UserLabel As Label = DirectCast(DataList1.Items(0).FindControl("UserLabel"), Label)
    UserLabel.Text = DirectCast(dt2.Rows(row1)(1), String)
 Next
Next
con.Close()
ResultPanel.Style.Add("display", "block")

OR

更改查询类似

SELECT CATEGORY.SUBCATEGORY, USERS.USERNAME 
           FROM [CATEGORY]  
           JOIN [LOSTITEM] ON (CATEGORY.CATEGORYID = LOSTITEM.CATEGORYID) 
           JOIN [USERS] ON LOSTITEM.[USER] = USERS.[USERID] 
           WHERE [LOSTITEM].CITY=@CITY AND [LOSTITEM].DATELOST=@DATELOST AND ([LOSTITEM].NAME LIKE '%' + @NAME + '%' OR [LOSTITEM].NAME LIKE '%' + '""' + '%'

您将获得一个新的数据集

 DataList1.DataSource = dt1
 DataList1.DataBind()

在设计器页面

 <asp:DataList id="DataList1" runat="server">

<HeaderTemplate>
 Your Header
</HeaderTemplate>
<ItemTemplate>

<%#Container.DataItem("subcategory")%> -
$<%#Container.DataItem("username ")%>
 </ItemTemplate>

<FooterTemplate>
Footer comes here
</FooterTemplate>

</asp:DataList>