【问题标题】:Using DataList to display search results使用 DataList 显示搜索结果
【发布时间】:2019-08-05 14:30:50
【问题描述】:

我正在使用 datalist 来显示搜索结果,例如我正在检索此字段示例

我在相应标签中使用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 的方式填充。

附:四个标签合二为一<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")

【问题讨论】:

  • 您是否获得了 dt2 中的所有值?能发下完整代码吗?
  • @AA 我刚刚意识到我没有获得 dt2 中的所有值,我将如何获得所有 id,我尝试遍历所有列,但结果相同,我只得到最后一个 id在 dt2 表中搜索结果中的第一项,而其余的仍为空白
  • 您没有遍历数据集。所以它只绑定一行。也不是使用 2 个数据集,而是内部连接表并将所有值获取到单个数据集
  • 即使我这样做没有任何改变,我更新了我的代码。
  • 循环 dt2 也。您的类别和用户在 dt2 中,不是吗?

标签: asp.net vb.net datalist


【解决方案1】:

我在 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")

更改查询喜欢

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>

【讨论】:

  • 语法没问题,但还是同样的问题,最后一条记录显示在第一项上,其余为空白。
  • 非常感谢,我不知道为什么我没有想到使用 JOIN ♥
猜你喜欢
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 2023-04-07
  • 2013-01-14
相关资源
最近更新 更多