0

我是 ASP.NET 的新手,我正在我的应用程序中创建一个搜索框。

例如:如果用户在文本框中输入“abc”,那么文本框将从以“abc”开头的数据库中获取数据。我将此数据传递给DataTable.

它工作正常,

这是我的代码片段:

DataTable result = new DataTable();

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connString);
    conn.Open();

    string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);

    SqlCommand cmd = new SqlCommand(query, conn);

    result.Load(cmd.ExecuteReader());

    conn.Close();

    lvwItems.DataSource = result;
    lvwItems.DataBind();
}

现在我想在我的<div>标签中检索所有这些数据。所以我尝试使用 asp:ListView

这是我的代码片段,

它工作正常,但现在我想在用户选择 listView 的任何行时导航到新页面,但我无法选择任何行..

<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
    <div>
         <asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
    </div>
</LayoutTemplate>
<ItemTemplate>
    <div>
        <%# Eval("Scrip")%>
    </div>
</ItemTemplate>

提前致谢 !!

任何帮助将不胜感激。

编辑:(搜索框.aspx.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class SearchBox : System.Web.UI.Page
{
    string connString;
    DataTable result = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    { }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);
        conn.Open();

        string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);

        SqlCommand cmd = new SqlCommand(query, conn);

        result.Load(cmd.ExecuteReader());

        conn.Close();

        lvwItems.DataSource = result;
        lvwItems.DataBind();
    }



    protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
    {
        ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
        Label lablId = (Label)item.FindControl("lablId");

        if (String.IsNullOrEmpty(lablId.Text))
        {
            Response.Redirect("NextPage.aspx?id=" + lablId.Text, false);
        }
    }

(搜索框.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchBox.aspx.cs" Inherits="SearchBox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    </head>
<body>

    <form id="form1" runat="server">
    <div>

    </div>
    <asp:TextBox ID="TextBox1" runat="server" Height="30px" Width="179px" 
        OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
&nbsp;&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" Text="Go" 
        Width="62px" />
    &nbsp;<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>


  <asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging" 
 runat="server" ItemPlaceholderID="plhItems">
 <LayoutTemplate>
  <div>
       <asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
  </div>
 </LayoutTemplate>
  <ItemTemplate>
  <div>
 <%# Eval("Scrip")%>
    <asp:Label ID="lablId" visible="false" runat="server" Text='<%#Eval("Scrip") %>'/>

  </div>
</ItemTemplate>
</asp:ListView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:krunal_DBConnectionString2 %>" 
        SelectCommand="SELECT * FROM [SearchBoxData]"></asp:SqlDataSource>
    </form>
</body>
</html>
4

4 回答 4

1

您必须在 ItemTemplate 中添加一个 SELECT 按钮,请参阅完整的工作代码。

  <asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
    runat="server" ItemPlaceholderID="plhItems">
    <LayoutTemplate>
        <div>
            <asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
        </div>
    </LayoutTemplate>
    <ItemTemplate>
        <%# Eval("Scrip")%>
        <asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" />
    </ItemTemplate>
  </asp:ListView>

protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs  e)
{
    ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
    Label lablId = (Label)item.FindControl("CONTROL_ID");
}

谢谢

迪普

于 2012-04-12T05:35:06.807 回答
0

可能您将不得不为 Selected index change 添加事件:

<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging">  
于 2012-04-12T04:47:18.237 回答
0

在后面的代码中,您可以获得如下所示的选定行项。

您还可以放置标签或隐藏字段,以便您可以从这些控件中获取一些数据并将其传递到下一页..(可能是 id 或其他东西)。

   <asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging" 
 runat="server" ItemPlaceholderID="plhItems">
 <LayoutTemplate>
  <div>
       <asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
  </div>
 </LayoutTemplate>
  <ItemTemplate>
  <div>
    <%# Eval("Scrip")%>
     <asp:Label ID="lablId" visible="flase" runat="server" Text='<%#Eval("Id") %>' />
  </div>
</ItemTemplate>
</asp:ListView>

 void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
 {
   ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
   Label lablId = (Label)item.FindControl("lablId");

   if (String.IsNullOrEmpty(lablId.Text))
   {
     Response.Redirect("page.aspx?id="+lablId.Text,false);
   }
 }

谢谢

迪普

于 2012-04-12T04:55:58.650 回答
0

干得好,

还要从按钮中删除 OnClick="callMethod"。由于您有 SelectedIndex 方法,因此不需要 onClick 方法。

protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs  e)
{
  ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
  Button btn = (Button)item.FindControl("btn1");
  if(btn != null) 
  {
   var buttonText = btn.Text;  
  }
}

希望这可以帮助

谢谢

迪普

于 2012-04-12T06:18:29.487 回答