Unable to call web service 2nd time From jQuery
I am unable to call web service 2nd time from jQuery function code is here.
Page:
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<scri开发者_JS百科pt type="text/javascript">
function getCityByPinCode() {
var path = '<%=ConfigurationManager.AppSettings["AutoComplete"].ToString() %>';
$.ajax({
type: "POST",
url: "" + path + "/CityBasedOnPinCode",
data: "{pincode: '" + $("#<%=ntxbPostCode.ClientID%>").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: OnError
});
}
function OnSuccess(data, status) {
var city = document.getElementById('<%= tbxCity.ClientID %>');
var val1 = data.d;
document.getElementById("ContentPlaceHolder1_CompanyDetails1_tbxCity").value = val1;
}
function OnError(request, status, error) {
alert(request.statusText);
}
</script>
</telerik:RadCodeBlock>
<body>
<telerik:RadNumericTextBox ID="ntxbPostCode" runat="server" DataType="System.Int32"
MaxLength="4" TabIndex="4" Width="95%" DecimalDigits="0" ClientEvents-OnValueChanged="getCityByPinCode()">
</telerik:RadNumericTextBox>
<asp:TextBox ID="tbxCity" runat="server" Width="95%" TabIndex="5"></asp:TextBox>
</body>
Web service:
public string CityBasedOnPinCode(string pincode)
{
int pcode = 0;
int.TryParse(pincode, out pcode);
List<usp_CityBasedOnPinCodeResult> lstCity = new CityDA().CityBasedOnPincode(pcode);
return lstCity[0].CityName.ToString();
}
I want to fill city textbox on the base of pincode entered.So when User Enters pincode,he/she gets city name automatically.When I put value of pincode predefined it gives me perfect result but when i am entering new value i got error as follows
Microsoft JScript runtime error: Object expected.
Note that if i have value of pincode in textbox at page load time(edit time) Then i am getting proper value but not getting value after changing value of textbox. jQuery is included in masterpage. so not included here.
Since you are subscribing to the client-side event offered by the RadNumericTextBox it might be easier to use the event arguments in your code as opposed to using a selector to get a hold of the value. You can see an example of how to get the new value from the OnValueChanged event right here.
Along with this, when I changed the code to the following everything worked fine on my end:
<script type="text/javascript">
function getCityByPinCode(sender, eventArgs) {
//do stuff
}
</script>
<telerik:RadNumericTextBox ID="ntxbPostCode" runat="server" DataType="System.Int32"
MaxLength="4" TabIndex="4" Width="95%" DecimalDigits="0" ClientEvents-OnValueChanged="getCityByPinCode">
</telerik:RadNumericTextBox>
<br />
<asp:TextBox ID="tbxCity" runat="server" Width="95%" TabIndex="5"></asp:TextBox>
This prevented the event from firing on initial page load (which it was with your code) and also ensure that I managed to get the new value of the RadNumericTextBox with the get_newValue() function.
Small note: you should be able to just use the city variable in the OnSuccess function to set its value, as opposed to calling getElementbyID again.
精彩评论