Behind I send a question that I posted for a few minutes ago but it is not correct so.
I get a website completly if I delete this two rows in the ajax code contentType: "application/json; charset=utf-8", dataType: "json",
If I keep it the script doesn't work. There is 500 Internal Server Error.
Can someone help me to fix it?
Hello,
I try to populate the ListBox, but the ajax part get me the website completely and not the List Item only.
Here is the code:
HTML
<asp:ListBox runat="server" ID="ListBoxSHIPTO" OnSelectedIndexChanged="ListBoxSHIPTO_SelectedIndexChanged"
Rows="10"></asp:ListBox>
<script type="text/javascript">
$(document).ready(function () {
$('#MainContent_TextBoxSHIPTO').keyup(function () {
search_val = $(this).val(); //console.log(search_val);
if (search_val.length > 0) {
$.ajax({
url: '/Rule.aspx/GetCustomers',
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { searchTerm: search_val },
success: function (r) {
var lstCustomers = $("#MainContent_ListBoxSHIPTO");
lstCustomers.empty();
$.each(r.d, function () {
lstCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
console.log(this['Wert']);
});
$('#MainContent_PanelSHIPTO').show();
var CusInput = $('#MainContent_TextBoxSHIPTO').position();
$('#MainContent_PanelSHIPTO').css({ top: CusInput.top + 17, left: CusInput.left });
},
error: function (error) {
alert(error);
}
});
}
else {
$('#MainContent_PanelSHIPTO').hide();
}
})
});
</script>
C#
[System.Web.Services.WebMethod]
public static List<ListItem> GetCustomers(string searchTerm)
{
List<ListItem> items = new List<ListItem>();
string query = "SELECT Top 30 [CUSTOMER] AS Wert " + ",[CUSTOMER] + ' - ' + CASE WHEN [NAME] = '' OR [NAME] IS NULL THEN 'not found' ELSE [NAME] END AS Titel " + "FROM [Admin].[t_customer] " + "WHERE CASE WHEN @customer <> '' OR @customer IS NOT NULL THEN " + " CASE WHEN [CUSTOMER] like '%' + @customer + '%' OR [NAME] like '%' + @customer + '%' THEN 1 ELSE 0 END " + " ELSE 1 " + " END = 1 " + "ORDER BY [CUSTOMER]";
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = ConfigurationManager.ConnectionStrings["CPR_ConnNewCPR"].ConnectionString;
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@customer", !string.IsNullOrEmpty(searchTerm) ? searchTerm : (object)DBNull.Value);
cmd.Connection = con; con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
items.Add(new ListItem
{
Value = sdr["Wert"].ToString(),
Text = sdr["Titel"].ToString()
});
}
sdr.Close();
cmd.Dispose();
con.Close();
return items;
}
Can you help me the fix the errhor?
Thank you in advance Best regards
Iryna