4

クライアント側にテキストボックスとドロップダウンリストがあります。これらのコントロールの値を、javascriptを使用して(コードビハインドで)webmethodに渡す必要があります。テキストボックスの値を渡すことはできますが、ドロップダウンリストを渡すことはできません。

<p><asp:DropDownList id="ddlList" runat="server"></asp:DropDownList></p>
<p><asp:TextBox ID="txtSearch" runat="server"  OnTextChanged="txtSearch_TextChanged"    OnKeyPress="onKeyFunction();" AutoPostBack="True"></asp:TextBox>
<asp:GridView ID="grdLista" runat="server"></asp:GridView>

<script type="text/javascript">
    function onKeyFunction() {
        var search = document.getElementById('<%=txtSearch.ClientID %>').value;
        //var ddl = i need to pass the dropdownlist values here..
        PageMethods.callJS(search, /*ddl */, onSucess, onError);

        function onSucess(result) {
            alert(result);
        }

        function onError(result) {
            alert('Something wrong.');
        }
    }
</script>

そしてこれは私のWebMethodのコードです

[WebMethod]            
public static IEnumerable<string> callJS(string search)
{
    IEnumerable<string> results = itemList.Where(item => item.Contains(search.ToLower()));
    return (results);
}
4

2 に答える 2

2

あなたはこのようにあなたのDDLの値を得ることができます:

var liste =  document.getElementById('#ddlList');
var ddl = liste.options[liste.selectedIndex].value;

編集 :

DDLからすべての値を取得するには、オプションの値をループする必要があります。

var ddl = [];
for(var i = 0; i<3; i++) {
    ddl.push(document.getElementById("<%=ddlList.ClientID %>").options[i].value);
}

List<string>この配列は、サーバー側で:として取得できます。

[WebMethod()]
public static IEnumerable<string> callJS(List<string> ddl) { ... }
于 2012-12-03T16:02:51.743 に答える
0

ページメソッドのどこでddl値が使用されているかが明確ではありません。おそらくあなたはまだその部分を書いていません。提案として、すべての値をコンマ区切りの文字列として渡すことができます。または配列として。

Jqueryを使用すると、値は次のように取得できます。

function getDropDownList() {
        var $ddl = $('#' + 'ctl00_ContentPlaceHolder1_ddlList');
        var $array = []
        $ddl.children('option').each(function () {
            $array.push($(this)[0].value);

        })
        //return $array you can return the value here as an array;
 //return $array.join(',') or as a string;

        try {
            PageMethods.callJS(search,$array, success, failure);
        }
        catch (e) {
            alert('error');
        }

}

ページメソッドの署名を変更する必要があります。

[WebMethod]            
public static IEnumerable<string> callJS(string search, string[] ddlList)
    {



        IEnumerable<string> results = itemList.Where(item => item.Contains(search.ToLower()));
        return (results);
    }

お役に立てれば。

于 2012-12-03T17:06:29.553 に答える