簡體   English   中英

如何從Web服務列表中填充dropDownList <string>

[英]How to populate a dropDownList from a webservice list<string>

我能夠填充我的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。 到目前為止一切看起來都很好。

DropDownList的圖像,帶有填充值。

所以,這是問題所在。 當我在DropDownList中選擇一個項目並提交表單時,我無法獲得我選擇的值。 這是C#CodeBehind的片段:

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

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

  • 嘿:“選擇移動類型”
  • 嘿2:“選擇移動類型”
  • 嘿1:“選擇移動類型”

無論我在DropDownList中選擇什么值,我總是得到上面顯示的保存值。 為什么? 我錯過了什么?

您需要將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;
    }

@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#中讀取您選擇的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM