0

我正在尝试从 jquery ajax 调用填充的 asp.net 下拉列表控件中获取选定的项目。我可以循环浏览屏幕上的项目,查看页面源代码中附加的选项,但是当我尝试从 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));            
            }
4

1 回答 1

-1
string selectedValue = Request.Form[ddlLanguage.UniqueID];
于 2015-11-25T05:35:11.630 回答