【问题标题】:How to populate a dropDownList from a webservice list<string>如何从 web 服务列表中填充 dropDownList<string>
【发布时间】: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


【解决方案1】:

您需要将一个 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;
    }

【讨论】:

  • 为什么我不能直接从 DropDownList 中读取值?我真的需要引入另一个对象吗?
  • 正是@E.B.下面说。这些值是从 JavaScript 添加的,并且不存在于 ViewState 中。
  • 让我知道上面的代码是否适合您。谢谢!
【解决方案2】:

@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# 中读取您选择的值。

【讨论】:

  • 我试过了,但没有任何区别。我可以从 hiddenField 中获得的唯一值是“选择移动类型”。还有其他建议吗?
  • 让我举个例子,我会尽快回复这里。
  • 我想你最终会像这样完成它。你在这里错过了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
相关资源
最近更新 更多