【发布时间】:2016-08-13 16:38:29
【问题描述】:
我能够填充我的 DropDownList 并从列表中选择一个值。但是,我永远无法获得我选择的值。让我告诉你我在做什么。我有以下 DropDownList
<asp:DropDownList ID="mobility" runat="server" AppendDataBoundItems="true">
我使用以下 AJax 填充上述 DropDownList。
function load_mobilityList() {
$.ajax({
url: '<%=ResolveUrl("/api/facility/getFacilityMobility_LOV") %>',
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
success: function (Result) {
//Result = Result.d;
$.each(Result, function (key, value) {
$("#mobility").append($("<option></option>").val
(key).html(value));
});
},
error: function (Result) {
alert("Error");
},
failure: function (Result) {
alert(response.responseText);
}
});
}
上面的 AJAX 中用于填充 DropDownList 的 URL 正在调用 Web 服务。这是模块。模块返回一个“列表”,如果可能的话,我真的很想保持这种状态。
public List<String> getFacilityMobility_LOV()
{
string constr = ConnectionStringsExpressionBuilder.GetConnectionString("ConnectionString");
OracleConnection con = new OracleConnection(constr);
con.Open();
string sqlStrg = "SELECT mobility_type FROM lu_mobility_type order by upper(mobility_type)";
// create command object and set attributes
OracleCommand cmd = new OracleCommand(sqlStrg, con);
List<string> myList = new List<string>();
using (OracleDataReader rd = cmd.ExecuteReader())
{
var idx1 = rd.GetOrdinal("mobility_type");
while (rd.Read())
{
myList.Add(rd.GetString(0));
}
}
con.Close();
return myList;
}
好的,现在 DropDownList 已按预期填充。到目前为止,一切看起来都不错。
Image of DropDownList with populated values.
所以,这就是问题所在。当我在 DropDownList 中选择一个项目并提交表单时,我无法获得我选择的值。这是 C# CodeBehind 的 sn-p:
string hey = mobility.SelectedItem.Value;
string hey2 = mobility.SelectedItem.Text;
string hey1 = mobility.SelectedValue;
使用 MS Visusal Studio 我可以看到上述变量的值:
- 嘿:“选择移动类型”
- hey2:“选择移动类型”
- hey1:“选择移动类型”
无论我在 DropDownList 中选择什么值,我总是会得到上面显示的保存值。为什么?我错过了什么?
【问题讨论】:
-
您是否需要在服务器 b/c 上加载 asp:dropdownlist 它需要处于视图状态?
标签: c# asp.net .net ajax drop-down-menu