![](/img/trans.png)
[英]ASP Dropdownlist is not recognized in C# code when it is in GridView
[英]dropdownlist values from jquery are not recognized in c# code behind
我试图从asp.net dropdownlist控件中获取选定的项目,该控件是从jquery ajax调用中填充的。 我能够遍历屏幕上的项目,请参阅页面源代码中随附的选项,但是当我尝试从c#后端代码获取值时,我得到的是空白值。 这就是我所拥有的:
$('#<%=Button2.ClientID %>').click(function () {
var Dropdown2 = $('#<%=ddlListAgents.ClientID %>');
Dropdown2.empty();
$.ajax({
type: "POST",
url: "WebForm3.aspx/fetchData",
data: "{selectedAgent: '" + $('#<%=txtAgentNameText.ClientID %>').val() +"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
Dropdown2.append(new Option("--Select Agent --", 0));
$.each(response.d, function (index, item) {
Dropdown2.append(new Option(item.Name, item.ID));
});
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});
打电话后一切都设置好,我可以浏览选项。 但是,当我单击按钮并尝试在此代码(测试代码)中获取选定的值时:
protected void btnSelectAgentAdd_ServerClick(object sender, EventArgs e)
{
foreach (ListItem item in ddlListAgents.Items)
{
if (item.Selected.Equals(true))
{
item.Value.ToString();
}
else
{
item.Value.ToString();
}
}
string one = ddlListAgents.SelectedValue;
}
在第一个循环之后,代码将从for循环中退出,并且ddlListAgents.SelectedValue等于空字符串。
这是我用来获取数据集的方法:
[WebMethod]
public static List<AgentName> fetchData(string selectedAgent)
{
Dictionary<string, string> list = new Dictionary<string, string>();
BL.Client client = new BL.Client();
var agents = new List<AgentName>();
if (selectedAgent != string.Empty)
{
list = client.GetAgentNamesForPopupSearch(selectedAgent);
}
foreach (KeyValuePair<string, string> val in list)
{
agents.Add(new AgentName { ID = Convert.ToInt32(val.Key), Name = val.Value });
}
return agents;
}
欢迎任何建议。 在此先感谢Laziale
编辑:添加页面加载事件:
if (!IsPostBack)
{
Dictionary<string, string> list = new Dictionary<string, string>();
BL.Client client = new BL.Client();
list = client.GetAgentNamesForPolicies2(Convert.ToInt32(4));
ddlAgentName.DataSource = list;
ddlAgentName.DataTextField = "Value";
ddlAgentName.DataValueField = "Key";
ddlAgentName.DataBind();
ddlAgentName.Items.Insert(0, new ListItem("-- Select Agent --", string.Empty));
}
string selectedValue = Request.Form[ddlLanguage.UniqueID];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.