C# .NET - Can my results of a search be links to their own pages?

Asked By veni on 08-May-12 03:13 AM
Earn up to 30 extra points for answering this tough question.
Hi,

I have a textbox and autoextender that will allow a search through my database for product names. I need the product name that is chosen to link to it's page. Is it possible to do this without adding a submit button? It won't fit next to my search and it wouldn't look right if I put one there anyway.  I want the client side scripting in jquery..Its urgent pls anyone reply me immediately ..i want the soln ..

<

ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" ></ajaxToolkit:ToolkitScriptManager>



<div>

<asp:TextBox runat="server" ID="myTextBox" Width="300" CssClass="stylesheet" autocomplete="off" />

<ajaxToolkit:AutoCompleteExtender

runat="server"

BehaviorID="AutoCompleteEx"

ID="autoComplete1"

TargetControlID="myTextBox"

ServicePath="AutoComplete.asmx"

ServiceMethod="GetCompletionList"

MinimumPrefixLength="1"

CompletionInterval="10"

EnableCaching="true"

CompletionSetCount="20"

CompletionListCssClass="autocomplete_completionListElement"

CompletionListItemCssClass="autocomplete_listItem"

CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"

DelimiterCharacters=";, :"

ShowOnlyCurrentWordInCompletionListItem="true" >



<WebMethod()> _

public

string[] GetCompletionList(string prefixText, int count)

{


//ADO.Net

SqlConnection cn =new SqlConnection();

DataSet ds = new DataSet();

DataTable dt = new DataTable();

//String strCn = "data source=.;Initial Catalog=MyDB;Integrated Security=True";

String strCn ="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\vsubram\\Documents\\Visual Studio 2010\\WebSites\\AutoDB\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True";

//Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\vsubram\Documents\Visual Studio 2010\WebSites\AutoDB\App_Data\Database.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True

cn.ConnectionString = strCn;


SqlCommand cmd = new SqlCommand();

cmd.Connection = cn;

cmd.CommandType =

CommandType.Text;

//Compare String From Textbox(prefixText) AND String From Column in DataBase(CompanyName)

//If String from DataBase is equal to String from TextBox(prefixText) then add it to return ItemList

//-----I Defined a parameter instead of passing value directly to prevent sql injection--------//

cmd.CommandText = "select * from tblCustomer Where CompanyName like @myParameter";

cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");

try

{

cn.Open();

cmd.ExecuteNonQuery();


SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);

}


catch

{

}


finally

{

cn.Close();

}

dt = ds.Tables[0];


//Then return List of string(txtItems) as result

List<string> txtItems =new List<string>();

String dbValues;

foreach (DataRow row in dt.Rows)

{


//String From DataBase(dbValues)

dbValues = row["CompanyName"].ToString();

dbValues = dbValues.ToLower();

txtItems.Add(dbValues);


}


return txtItems.ToArray();



}




[)ia6l0 iii replied to veni on 09-May-12 10:38 AM
The code post was not necessary, as your problem description was self-explanatory. 

Poles apart, the AutoCompleteExtender control as-is, does not expose any event for the select operation that you perform on it. However, you can use its "OnClientItemSelected" event to fire a server side handler that would redirect to the page you need to. 

So add the OnClientItemSelected event like below:
<ajaxToolkit:AutoCompleteExtender runat="server" BehaviorID="AutoCompleteEx" ID="autoComplete1" TargetControlID="myTextBox"
ServicePath="AutoComplete.asmx"  ServiceMethod="GetCompletionList" MinimumPrefixLength="1" CompletionInterval="10" EnableCaching="true"
CompletionSetCount="20"  CompletionListCssClass="autocomplete_completionListElement" CompletionListItemCssClass="autocomplete_listItem" CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"  DelimiterCharacters=";, :" ShowOnlyCurrentWordInCompletionListItem="true" 
OnClientItemSelected="OnItemSelected">
</ajaxtoolkit:autocompleteextender>


Add the client side function.
<script type="text/javascript">
function OnItemSelected()
{
var selectedIndex=$find("AutoCompleteEx")._selectIndex;
//fire a method that does the redirect. 
//Or set the window.location to the redirect url
}
</script>


Hope this helps.