Click to See Complete Forum and Search --> : How do I display on a label results of Server Side code?


viperbyte
07-10-2011, 07:29 PM
Hello everyone. I'm super stuck. I'm trying to set the value of a label on a default.aspx page with some jquery code and it's not happenng. No errors, just not happening. Below is my attempt at it. I'm trying to set lblWarning. Can someone please show me how to do this?


$(document).ready(function() {
var MaxLength = 10;
var result;

$('#txtBarCode').keypress(function(e) {
if ($(this).val().length >= MaxLength) {
//ShowBarCode();
result = ShowBarCode();
document.getElementById("lblWarning").value = result;
}
});
});

function ShowBarCode() {
$.ajax({
type: "POST",
url: "Default.aspx/RetreiveBarCode",
data: '{barcode: "' + $("#<%=txtBarCode.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: alert('success'),
failure: function(response) {
alert('failure');
}
});
}

This is the RetreiveBarCode function in the code behind file.

Public Shared Function RetreiveBarCode(ByVal barcode As Integer) As String 'List(Of testTbl)
Dim con = ConfigurationManager.ConnectionStrings("CONNSTRING").ConnectionString
Dim ListOfContacts As List(Of testTbl)
Dim contactsObj As New ContactActionLayer.ContactActionDAL
Dim theValue As Integer = Nothing
theValue = barcode
contactsObj.OpenConnection(con)
ListOfContacts = contactsObj.RetreiveTestTbl(theValue)
contactsObj.CloseConnection()

Return ListOfContacts(0).Description
End Function

jamesbcox1980
07-11-2011, 01:55 PM
jQuery ajax fails silently, if it is the code itself failing. If the code is structurally sound, and the server is returning data using the given URL and parameters, then the problem might be the format you're trying to retrieve not matching the set or default format expected my the jQuery function.


I noticed you set your data type to json, but I've had trouble getting this function to work correctly in this format. If you're gonna retrieve json that needs to perform something, use jQuery.getJSON() instead. Otherwise, simply return some text as standard ASCII and use it as if it were responseText instead of JSON. I'm pretty sure jQuery.ajax() was made for jsonp datatype to be used with jQuery.parseJSON to parse the result from a string.

http://api.jquery.com/jQuery.getJSON/

viperbyte
07-11-2011, 03:02 PM
Thanks for the lesson James. I'm new at using javascript, jquery and ajax. I was using json because that is the way it is in the code that I lifted from the internet not because I knew what I was doing. I appreciate your help :)

jamesbcox1980
07-12-2011, 02:15 PM
No problem! Were you able to get it working?

viperbyte
07-12-2011, 02:27 PM
No not yet. For now I'm depending on a submit button to get things going. It's on the back burner for now. Next up for me is to get the form to look more presentable then I'll have to get back to get this automatic behaviour to work. It's so painful.