[英]ASP.NET clicking on button after changing textbox, firing client event and server event
I have a popup for change an address with 3 text box (Address, Province and ZIP) and one DropDownList for the City. 我有一个弹出式窗口,用于更改带有3个文本框(地址,省和ZIP)和一个城市的DropDownList的地址。 Changing the ZIP code it change the province text box with an ajax call, and I want to add later populate city ddl, if I can get it work.
更改邮政编码后,它会用ajax调用更改省份文本框,如果可以,我想在以后添加填充城市ddl。 The problem is that if I now change the ZIP code, then click outside of it and then click the save button is working good, I've the correct value in the server side function, but if I change the ZIP code and then click directly to the save button I could see the Province text changing correctly in the interface too, but in the btnSave_Click it still have the old value.
问题是,如果我现在更改邮政编码,然后在其外部单击,然后单击“保存”按钮工作正常,则服务器端功能中的值正确,但是如果更改邮政编码然后直接单击到保存按钮,我也可以在界面中看到省文本的正确更改,但是在btnSave_Click中,它仍然具有旧值。 I've tried to make not async the ajax call, but still nothing..
我试图使ajax调用不异步,但是还是没有。
ASPX: ASPX:
<table>
<tr>
<td>
<label>Address</label>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<label>Province</label>
<asp:TextBox ID="txtProvince" runat="server"></asp:TextBox>
</td>
<td>
<label>City</label>
<asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
</td>
<td>
<label>ZIP Code</label>
<asp:TextBox ID="txtZIP" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="3"><asp:Label id="lblError" runat="server" style="color:Red" Visible="false" /></td>
</tr>
<tr>
<td colspan="3" style="text-align: center">
<asp:ImageButton ID="btnResidenza" ImageUrl="/Images/Button/salva.png" runat="server"
OnClick="btnSave_Click" />
</td>
</tr>
</table>
jQuery: jQuery的:
<script type="text/javascript" language="javascript">
$(function () {
$(document).on('change', 'input[id*="txtZIP"]', function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CleanAddress.aspx/txtZIP_TextChanged",
data: "{ZIP:'" + $("[id*='txtZIP']").val() + "'}",
dataType: "json",
success: function (response) {
$("[id*=txtProvince]").val(response.d);
},
error: function (result) {
alert("Errore! " + result.status + " - " + result.statusText);
}
});
});
});
</script>
Web Method: 网络方法:
[System.Web.Services.WebMethod]
public static string txtZIP_TextChanged(string ZIP)
{
if ((ZIP.Trim() != "") && (ZIP.Length == 5))
{
dcListCityDataContext dc = new dcListCityDataContext();
if (dc.City.Where(c => c.ZIP.Equals(ZIP)).Count() > 0)
{
string province = dc.City.Where(c => c.ZIP.Equals(CAP)).First().ID_PROVINCE.ToUpper();
if (province != null)
{
return provincia;
}
}
}
return "";
}
Server Save Click: 服务器保存单击:
protected void btnSave_Click(object sender, ImageClickEventArgs e)
{
try
{
string result = null;
// HERE IS THE PROBLEM, BECAUSE I HAVE THE WRONG VALUE
// IN txtProvince.Text IF I CHANGE THE ZIP CODE AND CLICK
// DIRECTLY ON THE SAVE BUTTON
Save(txtProvince.Text, ddlCity.SelectedItem.ToString(), txtZip.Text, txtAddress.Text);
lblError.Text = "";
}
catch (Exception ex)
{
(...)
}
}
It seem like this like Jquery onchange works. 看起来像Jquery onchange的作品。 May be it fire only when losing focus.
可能只有在失去焦点时才会触发。 May be you can try use event OnkeyUp .
也许您可以尝试使用事件OnkeyUp 。 This will update the field on any key pressed in the corresponding field.
这将在相应字段中按下的任何键上更新该字段。
Updated on comment: There are performance penalty using OnKeyUp but for small application, it's just fine !!! 更新评论:使用OnKeyUp会降低性能,但对于小型应用程序来说,这很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.