我有一個表單來更改一些地址信息。當在文本框中放置一些generic ZIP code
時,會觸發客戶端事件來填充兼容城市的下拉列表。ASP.NET下拉列表在服務器button_click中爲空,ddl填充客戶端
填充是正確的,問題是無論我選擇哪個值之後,因爲當我點擊按鈕來保存數據時,服務器功能button_click
中的下拉列表有SelectedItem to null
,有aso列表項計數爲0 ..
我已經把另一個下拉列表僅僅用於測試,其中有一些隨機的城市,如果我通過相同的jquery函數設置一個值,我可以正確地在服務器功能中看到它,所以問題似乎是事實上,另一個是填充客戶端..
我見過這個:Asp.Net: Restoring client-side SelectedItem of DropDownList on server-side和設置隱藏字段值的解決方法,但我想知道如果有更正確的方法來做到這一點..
<table>
<tr>
<td>
<label>Address</label><br />
<asp:TextBox ID="txtAddress" CssClass="testo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<label>Province</label><br />
<asp:TextBox ID="txtProvince" runat="server" />
</td>
<td>
<label>City</label><br />
<asp:DropDownList ID="ddlCity" runat="server" />
</td>
<td>
<label>ZIP</label><br />
<asp:TextBox ID="txtZIP" />
</td>
</tr>
<tr>
<td>
<!-- Just for test set ddl by jQuery -->
<asp:DropDownList ID="ddlTest" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:ImageButton ID="btnSave" runat="server"
OnClick="btnSave_Click" />
</td>
</tr>
</table>
$(function() {
$(document).on('keyup', 'input[id*="txtZIP"]', function() {
var input = $(this);
if (input[0].selectionStart == 5) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CleanAddress.aspx/txtZIP_TextChanged",
data: "{ZIP:'" + input.val() + "'}",
dataType: "json",
success: function (response) {
var cities = $.parseJSON(JSON.stringify(response.d));
if (comuni.length > 0) {
$("[id*=txtProvince]").val(cities[0].ID_PROVINCE);
$("[id*='ddlCity']").empty();
$.each(cities, function (key, value) {
$("[id*='ddlCity']").append($("<option />
").val(value.ID_CITY).text(value.CITY));
});
$("[id*='ddlCity']").removeAttr('disabled');
$("[id*='ddlTest']").val(9); // Just for test
$("[id*='ddlComuneDom']").val(cities[0].ID_CITY);
//alert($("[id*='ddlTest']").val());
//alert($("[id*='ddlCity']").val());
}
}
},
error: function (result) {
alert("Errore! " + result.status + " - " + result.statusText);
}
});
}
});
[WebMethod]
public static List<Comuni> txtZIP_TextChanged(string ZIP)
{
try
{
List<Cities> cities = new List<Cities>();
if ((ZIP.Trim() != "") && (ZIP.Length == 5))
{
dcListCitiesDataContext dc = new dcListCitiesDataContext();
if (dc.Cities.Where(c => c.ZIP.Equals(ZIP)).Count() > 0)
{
string province = dc.Cities.Where(c => c.ZIP.Equals(ZIP)).First().ID_PROVINCE.ToUpper();
if (province != null)
{
cities = dc.Cities.Where(c => c.ZIP.Equals(ZIP)).ToList();
return cities;
}
}
}
return cities;
}
catch (Exception ex)
{
Utility.WriteLog("CleanAddress - txtZIP_TextChanged " + " - " + ex.Message, TipoLog.Error);
throw;
}
}
protected void btnSave_Click(object sender, ImageClickEventArgs e)
{
// Here I've always ddlCity empty
string cities= "";
if (ddlCity.SelectedItem != null)
cities= ddlCity.SelectedItem.ToString();
else
cities= ddlTest.SelectedItem.ToString();
(...)
}
要獲得當前選定的文本,你應該(';'#dropDownId:selected')。text();' – BNN 2015-03-31 10:29:14
我希望價值服務器端,和客戶端選擇器#idValue無效,而''%= dropDown.ClientID%>'或$( 「[ID * = 'dropDownId']」)。 – MisterFrank 2015-03-31 12:42:25