Referencing a field on an asp.net page from a Web Service (asmx) page

只愿长相守 提交于 2019-12-24 22:35:48

问题


I'm using a Web Service page to make a textbox an "auto complete" control. In order to do that, I'm using a web service.

My code in the web service looks like this:

    public string[] ISGetCompletionList(string prefixText)
    {
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CLTDPL"].ConnectionString;
        SqlConnection conn = new SqlConnection(connectionString);

        List<string> Payers = new List<string>();
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "SELECT [PAYER_ID], [PAYER_TYPE] FROM [mos_Payer] WHERE " +
            "PAYER_TYPE like '%' + @SearchText + '%' ORDER BY PAYER_TYPE ASC";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    Payers.Add(string.Format("{0}|{1}", sdr["PAYER_TYPE"], sdr["PAYER_ID"]));
                }
            }
            conn.Close();
        }
        return Payers.ToArray();
    }

Everything works perfectly. However, now I need to filter the CommandText by one of the fields. So, I know I need to change the cmd.CommandText line to something like:

cmd.CommandText = "SELECT [PAYER_ID], [PAYER_TYPE] FROM [mos_Payer] WHERE " +
"PAYER_TYPE like '%' + @SearchText + '%' and PAYER_DATE = " + MyForm.PayerDate + 
" ORDER BY PAYER_TYPE ASC";

I'm trying to figure out how I'd reference that field on my aspx page.

The javascript that sends all that info over looks like this:

    function SetAutoComplete() {
        $("#<%=txtPayer.ClientID %>").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '<%=ResolveUrl("~/Autocomplete.asmx/ISGetCompletionList") %>',
                     data: "{ 'prefixText': '" + request.term + "'}",
                     dataType: "json",
                     type: "POST",
                     contentType: "application/json; charset=utf-8",
                     success: function (data) {
                         response($.map(data.d, function (item) {
                             return {
                                 label: item.split('|')[0],
                                 val: item.split('|')[1]
                             }
                         }))
                     },
                     error: function (response) {
                         alert(response.responseText);
                     },
                     failure: function (response) {
                         alert(response.responseText);
                     }
                 });
             },
            select: function (e, i) {
                $("#<%=hfPayer.ClientID %>").val(i.item.val);
             },
             minLength: 1
        });
   };

回答1:


Can't remember if you can do this, but try adding another param to your method.

public string[] ISGetCompletionList(string prefixText, string payerDate)

then in the ajax call

data: "{ 'prefixText': '" + request.term + "', 'payerDate': '" + formDate + "' }", etc.

or,

add the info to prefixText in the ajax call, similar to how the webmethod is combining data:

data: "{ 'prefixText': '" + request.term + "|" + formDate + "' }", etc

then split it in code behind.



来源:https://stackoverflow.com/questions/48253675/referencing-a-field-on-an-asp-net-page-from-a-web-service-asmx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!