使用Repeater控件从SQL数据库下拉列表搜索

时间:2015-07-17 12:49:46

标签: c# jquery sql asp.net search

尝试在数据库中搜索下拉列表中显示的项目,然后单击按钮时我希望它将结果输出到转发器控件。我有这个用于文本框,但我不确定如何为下拉列表。

SPROC

CREATE PROCEDURE dbo.spiSearchByString
(
    @HouseNearSearch VARCHAR(50) = NULL,
    @HouseNameSearch VARCHAR(50) = NULL,
    @HouseFamSearch VARCHAR(50) = NULL,
    @HouseTownSearch VARCHAR(50) = NULL
)
AS
    SELECT
        Houses.Name
        ,Houses.Id
        ,Houses.Townland
        ,Houses.Near
        ,Houses.Status
        ,Houses.Built
        ,Houses.IMAGE
    FROM Houses
    WHERE (Houses.Near IS NULL OR (Houses.Near LIKE '%' + @HouseNearSearch + '%' OR Houses.Id LIKE '%' + @HouseNearSearch + '%'))
 OR   (Houses.Name IS NULL OR  (Houses.Name LIKE '%' + @HouseNameSearch + '%' OR Houses.Id LIKE '%' + @HouseNameSearch + '%'))
 OR   (Houses.Families IS NULL OR  (Houses.Families LIKE '%' + @HouseFamSearch + '%' OR Houses.Id LIKE '%' + @HouseFamSearch + '%'))
 OR   (Houses.Townland IS NULL OR  (Houses.Townland LIKE '%' + @HouseTownSearch + '%' OR Houses.Id LIKE '%' + @HouseTownSearch + '%'))
    ORDER BY Houses.Name
    RETURN

Aspx Page

<div>
                    <form id="Name" style="margin-left: 30%" runat="server">
                        Name of House:
                        <asp:TextBox ID="txtName" runat="server" style="margin-left:7%" ></asp:TextBox>
                    <br />
                        <br />
                        Townland:
                        <asp:DropDownList ID="lstTown" runat="server" Width="174" style="margin-left:13.5%" ></asp:DropDownList>        
                    <br />
                        <br />
                        Near:
                        <asp:TextBox ID="txtNear" runat="server" style="margin-left:19.6%"></asp:TextBox>     
                    <br />
                        <br />
                        Associated Families:
                        <asp:TextBox ID="txtFam" runat="server" style="margin-left:1.7%"></asp:TextBox> 
                        <br />
                        <br />
                        <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" CssClass="button"  style="margin-left:46%"/>
                        </form>
                        <br />
                    <br />
                        <h2><asp:Label ID="lblStatus" runat="server" Text=""></asp:Label></h2>
                    </div> 
                <ul id="rooms">
                    <asp:Repeater ID="rptDataSearch" runat="server">
                        <ItemTemplate>
                            <li>
                               <a href='<%# "HouseInfo.aspx?HouseId=" + Eval("Id").ToString() %>'>
                                    <img src='<%#DataBinder.Eval(Container.DataItem, "Image")%>' alt="img" width="398" height="287"/>
                                <h2>
                                   <a href='<%# "HouseInfo.aspx?HouseId=" + Eval("Id").ToString() %>'>
                                        <asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label></a></h2>
                                <p>
                                    <b>ID: </b>
                                    <%#DataBinder.Eval(Container.DataItem, "Id")%>
                                    <br />
                                    <b>Name of House: </b> 
                                    <%#DataBinder.Eval(Container.DataItem, "Name")%>
                                    <br />
                                    <b>Townland: </b>
                                    <%#DataBinder.Eval(Container.DataItem, "Townland")%>
                                    <br />
                                    <b>Near: </b>
                                    <%#DataBinder.Eval(Container.DataItem, "Near")%>
                                    <br />
                                    <b>Status/Public Access: </b>
                                    <%#DataBinder.Eval(Container.DataItem, "Status")%>
                                    <br />

                                    <b>Date Built: </b>
                                    <%#DataBinder.Eval(Container.DataItem, "Built")%>
                                </p>
                            </li>
                        </ItemTemplate>
                    </asp:Repeater>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT * FROM [Houses]" ProviderName="System.Data.SqlClient"></asp:SqlDataSource>
                </ul>

代码背后

protected void Page_Load(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            SqlCommand com = new SqlCommand("SELECT DISTINCT Townland FROM Houses ORDER BY Townland ASC", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            lstTown.DataTextField = ds.Tables[0].Columns["Townland"].ToString();
            lstTown.DataValueField = ds.Tables[0].Columns["Townland"].ToString();
            lstTown.DataSource = ds.Tables[0];
            lstTown.DataBind();
            lstTown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
            lstTown.SelectedIndex = 0;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            // Name textbox search
            if (txtName.Text == "" | txtName.Text == null)
            {
                lblStatus.Text = "Search Results";
            }
            else
            {
                DisplaySearchResults(txtName.Text);
                lblStatus.Text = "";
            }
            txtName.Visible = true;
            // Near textbox search
            if (txtNear.Text == "" | txtNear.Text == null)
            {
                lblStatus.Text = "Search Results";
            }
            else
            {
                DisplaySearchResults(txtNear.Text);
                lblStatus.Text = "";
            }
            txtNear.Visible = true;
            // Family textbox search
            if (txtFam.Text == "" | txtFam.Text == null)
            {
                lblStatus.Text = "Search Results";
            }
            else
            {
                DisplaySearchResults(txtFam.Text);
                lblStatus.Text = "";
            }
            txtFam.Visible = true;
            // Dropdown list search
            if (lstTown.SelectedValue == "" | lstTown.SelectedValue == null)
            {
                lblStatus.Text = "Search Results";
            }
            else
            {
                DisplaySearchResults(lstTown.SelectedValue);
                lblStatus.Text = "";
            }
            lstTown.Visible = true;
        }

        public void DisplaySearchResults(string strSearch)
        {
            //Display search results in repeater
            SqlCommand cmd = new SqlCommand("spiSearchByString", new SqlConnection(""));
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@HouseNameSearch", strSearch);
            cmd.Parameters.AddWithValue("@HouseNearSearch", strSearch);
            cmd.Parameters.AddWithValue("@HouseFamSearch", strSearch);
            cmd.Parameters.AddWithValue("@HouseTownSearch", strSearch);
            cmd.Connection.Open();

            rptDataSearch.DataSource = cmd.ExecuteReader();
            rptDataSearch.DataBind();
        }

文本框搜索

enter image description here

显示结果。

下拉列表搜索

enter image description here

这没有显示任何内容。

1 个答案:

答案 0 :(得分:1)

您遇到的问题是您绑定并在每个PostBack上设置SelectedIndex = 0。如果您在Page_Load中放置一个断点,您会注意到每次单击“搜索”按钮时,您的断点都会出现。

您需要做的是将Page_Load中的逻辑包裹在IsPostBack条件下。这样你就不会每次都重新绑定,因此将SelectedIndex保持在你期望的索引上。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();

        SqlCommand com = new SqlCommand("SELECT DISTINCT Townland FROM Houses ORDER BY Townland ASC", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        lstTown.DataTextField = ds.Tables[0].Columns["Townland"].ToString();
        lstTown.DataValueField = ds.Tables[0].Columns["Townland"].ToString();
        lstTown.DataSource = ds.Tables[0];
        lstTown.DataBind();
        lstTown.Items.Insert(0, new ListItem(string.Empty, string.Empty));
        lstTown.SelectedIndex = 0;
    }
}