Click here to Skip to main content
16,019,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an autocomplete search box that culled up the thumbnail of images stored in the database. The problem is that the .jpg extension shows up each times the image name appears in the autocomplete search box. Is there anyway I can modify these codes to prevent the .jpg extension from appearing? Below is my existing code using javascript and ashx. Thank you in advance!

What I have tried:

the aspx:

<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="scripts/jquery.autocomplete.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#<%=txtSearch.ClientID%>").autocomplete("Search.ashx", {
            width: 200,
            formatItem: function (data, i, n, value) {
                return "<img style = 'width:50px;height:50px' src= 'Images/" + value.split(",")[1] + "'/> " + value.split(",")[0];
            },
            formatResult: function (data, value) {
                return value.split(",")[0];
            }
        });
    });
</script>

<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
    Search here: 
    <asp:TextBox ID="txtSearch" runat="server" />
    <br />
</div>

<div>
<asp:FileUpload ID="fileuploadimages" runat="server" />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</div>
<div>
</div>
    <asp:Label ID="lblResult" runat="server"></asp:Label>
</form>
</body>
</html>


the ashx:

public class Search : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
string searchText = context.Request.QueryString["q"];
string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection connection = new SqlConnection(strcon);
connection.Open();
SqlCommand cmd = new SqlCommand("select ID,ImageName,ImagePath from ImagesPath where ImageName Like @Search + '%'", connection);
cmd.Parameters.AddWithValue("@Search",searchText);
StringBuilder sb = new StringBuilder();
using(SqlDataReader dr=cmd.ExecuteReader())
{
while(dr.Read())
{
sb.Append(string.Format("{0},{1}{2}",dr["ImageName"],dr["ImageName"],Environment.NewLine));
}
}
connection.Close();
context.Response.Write(sb.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
Posted
Updated 12-Jan-17 23:25pm

1 solution

You can get just the filename like this
System.IO.Path.GetFileNameWithoutExtension(dr["ImageName"]);


So whichever of your instances of dr["ImageName"] represents what is seen as the label change to the above code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900