【问题标题】:asp.net c# advanced searchingasp.net c# 高级搜索
【发布时间】:2014-09-16 14:46:33
【问题描述】:

目前在一个网站上,您可以看到重复出数据库中的所有配置文件。

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">

    <ItemTemplate>

               <div class="profilbox">
                    <div id="billedeSmall">
                        <img alt="" src="prod_image/<%#Eval ("bruger_billede") %>" width="100" height="100" />
                        </div>
                <h3><%# Eval("bruger_fornavn") %><br /><%# Eval("bruger_efternavn") %></h3>
                <p>Alder: <%# Eval("bruger_alder") %>år.<br />Søger: <%# Eval("bruger_søger") %><br />Kommune: <%# Eval("bruger_kommune") %>
                   <br /><a href="profilNormal.aspx?id=<%#Eval("bruger_id") %>">...Vis profil</a></p>

            </div>

        </ItemTemplate>

    </asp:Repeater>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" SelectCommand="SELECT  * FROM brugere ORDER BY NEWID()"></asp:SqlDataSource>

这里有一个表格,其中有 1 个下拉菜单,用于“显示正在搜索的配置文件”,一行有 2 个 texbox,它们被认为是 textbox1 和 textbox2 之间的年龄,最后一个文本框可以在其中搜索区域。

<table class="auto-style3">
    <tr>
        <td class="auto-style4">vis profiler der søger:</td>
        <td>
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>mand</asp:ListItem>
                <asp:ListItem>kvinde</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td class="auto-style4">age between: </td>
        <td>
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            og<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style5">i kommunen:</td>
        <td class="auto-style6">
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="auto-style4">&nbsp;</td>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Søg" OnClick="Button1_Click" />
        </td>
    </tr>
</table>

遗憾的是,这就是我所拥有的一切,因为我不知道从后面的代码开始。我希望它像这样工作:如果您在下拉列表中选择“男性”并单击按钮,则在名为“正在寻找”的数据库列中显示所有具有“男性”的个人资料,或者相反。

然后,如果您输入的年龄介于: textbox4 和 textbox5 之间,则显示所有男性的个人资料,如果这是用户在两个文本框中输入的内容,则显示在 20 到 30 之间。这有意义吗?我仍然是 ASP.net c# 的初学者。如果这不是一个真正的问题,我很抱歉。

我现在开始使用代码隐藏,这是正确的想法吗?谁能告诉我应该如何在我的页面上显示结果?我还是新手,所以请多多包涵。

 protected void ButtonSearch_Click(object sender, EventArgs e)
    {



        string statement = @"SELECT * FROM brugere";

        string whereConcatenator = "WHERE ";

        if (DropDownListSøger.SelectedItem != null)
        {
            statement += whereConcatenator;
            statement += "bruger_søger= '" + DropDownListSøger.SelectedItem.Value + "' ";

            whereConcatenator = "OR ";
        }

       if (TextBoxKommune.Text.Length > 0)
        {
            statement += whereConcatenator;
            statement += "bruger_kommune LIKE '%" + TextBoxKommune.Text + "%' ";

            whereConcatenator = "OR ";
        }


        Response.Write(statement);

        SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = Conn;
        cmd.CommandType = CommandType.Text;
        cmd = new SqlCommand(statement, Conn);

        Conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();


        while (reader.Read())
        {

        }

网站图片以获得更好的插图:

【问题讨论】:

  • 我认为您需要在后面的代码上开始您的 Button1_Click 事件。基本上,您可以通过检查下拉选择值、文本框输入并根据这些值在数据库中搜索来构建逻辑。

标签: c# asp.net search


【解决方案1】:

它几乎可以用这个,但是“kommune”仍然不能 100% 工作

protected void ButtonSearch_Click(object sender, EventArgs e) {

    string statement = @"SELECT * FROM brugere";

    string whereConcatenator = " WHERE ";

    if (DropDownListSøger.SelectedItem != null)
    {
        statement += whereConcatenator;
        statement += "bruger_søger ='" + DropDownListSøger.SelectedItem.Value + "' ";

        whereConcatenator = "AND ";
    }


    if (TextBox_AlderFra.Text != "")
      {

         statement += whereConcatenator;
         statement += " bruger_alder BETWEEN '" + TextBox_AlderFra.Text + "' AND '" + TextBox_AlderTil.Text + "'";
         whereConcatenator = "AND ";

     }
    if (TextBoxKommune.Text.Length > 0)
    {
        statement += whereConcatenator;
        statement += "bruger_kommune = '%" + TextBoxKommune.Text + "%' ";

    }

   SqlDataSource1.SelectCommand = statement;


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2012-03-11
    • 2018-07-13
    • 2015-04-17
    • 2012-04-09
    • 2015-04-06
    • 2018-11-05
    相关资源
    最近更新 更多