如何从Web服务列表<string>填充dropDownList

时间:2016-04-20 19:25:24

标签: c# asp.net .net ajax drop-down-menu

我可以填充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);
        }
    });
}

上面用于填充DropDownList的AJAX中使用的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:

的片段
        string hey = mobility.SelectedItem.Value;
        string hey2 = mobility.SelectedItem.Text;
        string hey1 = mobility.SelectedValue;

使用MS Visusal Studio我可以看到上述变量的值:

  • 嘿:“选择移动类型”
  • hey2:“选择移动类型”
  • hey1:“选择移动类型”

无论我在DropDownList中选择什么值,我总是得到上面显示的保存值。为什么?我错过了什么?

2 个答案:

答案 0 :(得分:0)

您需要将JavaScript函数附加到您的下拉列表中,将隐藏文本字段的值(在所选索引上已更改)设置为移动性下拉列表的值。然后在后面的C#代码中读取该隐藏文本框的值。

修改

这是一个快速而肮脏的完整工作示例

<asp:DropDownList ID="mobility" runat="server" ClientIDMode="Static">
    <asp:ListItem>-- Select a Mobility Type --</asp:ListItem>
</asp:DropDownList>

<asp:Button runat="server" ID="btnGo" text="Go" OnClick="btnGo_Click"/>
<input type="hidden" id="txtHidden" runat="server" ClientIDMode="Static"/>

<br /><br />
Value of Drop Down Is: <asp:Label runat="server" ID="lblOutput" />

<script>
    $(document).ready(function () {

        // set the value
        $("#mobility").append($("<option></option>").val("my Value").html("my Value"));

        //on change event
        $("#mobility").change(function () {
            $("#txtHidden").val($("#mobility").val());
        })

    });        
</script>

然后是

背后的代码
    protected void btnGo_Click(object sender, EventArgs e)
    {
        lblOutput.Text = txtHidden.Value;
    }

答案 1 :(得分:0)

@Joe Raio是对的, 您从javascript添加的值在ViewState对象中不存在,因此您确实需要将所选值保存到另一个可以从两侧读取的控制器。

OnChange您可以从下拉列表中将隐藏字段的值设置为所选值,然后您可以从后面的代码中读取它。

修改

我的意思是:

在标记中添加隐藏字段:

<asp:Hidden runat="server" ID="dropSelectedValue" ClientIdMode="Static" />

然后进入你的javascript成功函数,你初始化第一个值

// your code as is
$("#dropSelectedValue").val(firstValue);
// continues...

然后添加一个跟踪更改的事件:

$(function(){
$("#mobility").onchange(function(){
$("#dropSelectedValue").val($('#mobility').val());
});
});

最后,您可以通过读取隐藏字段值从c#中读取您选择的值。