簡體   English   中英

asp.net c#高級搜索

[英]asp.net c# advanced searching

您目前可以在網站的即時通訊上看到,您會從數據庫中重復所有配置文件。

<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())
        {

        }

該網站的圖片以獲得更好的插圖: 搜索

它幾乎可以解決這個問題,但是“ kommune”仍然無法正常工作100%

受保護的void ButtonSearch_Click(對象發送者,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;


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM