Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My source code as follows

JavaScript
<script type="text/javascript">
$(function () {
$("[id$=txtSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Default.aspx/GetCustomers") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=hfCustomerId]").val(i.item.val);
},
minLength: 1
});
}); 


html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"  runat="server">
</head>
<body

<form id="form1"  runat="server">
<asp:TextBox ID="txtSearch" runat="server" />
<asp:HiddenField ID="hfCustomerId" runat="server" />
<asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" />

</form>
</body>
</html>

Aspx code as follows
C#
public static string[] GetCustomers(string prefix)

{
    List<string> customers = new List<string>();
    using (SqlConnection conn = new SqlConnection())
    {

       // SqlConnection strconnection = new SqlConnection("Server=(local);Initial Catalog=Test;Trusted_Connection=true");

        SqlConnection strconnection = new SqlConnection("Data Source=GOD-PC;Initial Catalog=Test;Trusted_Connection=true");
 
       // conn.ConnectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;


        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select ContactName, CustomerId from customerss where ContactName like @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefix);
            cmd.Connection = strconnection;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(string.Format("{0}-{1}", sdr["ContactName"], sdr["CustomerId"]));
                }
            }
            conn.Close();
        }
    }
    return customers.ToArray();
}


    protected void Submit(object sender, EventArgs e)
    {
        string customerName = Request.Form[txtSearch.UniqueID];
        string customerId = Request.Form[hfCustomerId.UniqueID];
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + customerName + "\\nID: " + customerId + "');", true);

     
    }


In run mode as follows

Enter Search Name textbox (Submit button)

In textbox type the contacname and click the submit button. when i click the submit button the id will be displayed.

but when i click the submit button id is not displayed.

Please help me what is the problem in my above code.

What I have tried:

My source code as follows
JavaScript
<script type="text/javascript">
$(function () {
$("[id$=txtSearch]").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Default.aspx/GetCustomers") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("[id$=hfCustomerId]").val(i.item.val);
},
minLength: 1
});
}); 

HTML
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"  runat="server">
</head>
<body

<form id="form1"  runat="server">
<asp:TextBox ID="txtSearch" runat="server" />
<asp:HiddenField ID="hfCustomerId" runat="server" />
<asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" />

</form>
</body>
</html>


Aspx code as follows
C#
public static string[] GetCustomers(string prefix)

{
    List<string> customers = new List<string>();
    using (SqlConnection conn = new SqlConnection())
    {

       // SqlConnection strconnection = new SqlConnection("Server=(local);Initial Catalog=Test;Trusted_Connection=true");

        SqlConnection strconnection = new SqlConnection("Data Source=GOD-PC;Initial Catalog=Test;Trusted_Connection=true");
 
       // conn.ConnectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;


        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select ContactName, CustomerId from customerss where ContactName like @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefix);
            cmd.Connection = strconnection;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(string.Format("{0}-{1}", sdr["ContactName"], sdr["CustomerId"]));
                }
            }
            conn.Close();
        }
    }
    return customers.ToArray();
}


    protected void Submit(object sender, EventArgs e)
    {
        string customerName = Request.Form[txtSearch.UniqueID];
        string customerId = Request.Form[hfCustomerId.UniqueID];
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + customerName + "\\nID: " + customerId + "');", true);     
    }

In run mode as follows

Enter Search Name textbox (Submit button)

In textbox type the contacname and click the submit button. when i click the submit button the id will be displayed.

but when i click the submit button id is not displayed.

Please help me what is the problem in my above code.
Posted
Updated 7-Mar-16 16:23pm
v3
Comments
[no name] 6-Mar-16 9:32am    
What is problem exactly you are getting?

Just check error by pressing F12 in browser and see any error in console tab.

1 solution

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