我们可以将.asmx链接到asp.net控件吗?

本文关键字:net asp 控件 链接 asmx 我们 | 更新日期: 2023-09-27 17:48:56

我有一个.asmx文件,我用它来给我的AutoCompleteExtender (Ajax AutoCompleteExtender从 Ajax工具箱)的数据。AutoCompleteExtender与SQL数据库中的存储过程通信。

问题在于:用户可以选择一个过滤器来搜索数据库(姓名、地址、标题等)。该过滤器与DropDownList一起应用。如果我想让AutoComplete正常工作,我必须在AutoComplete上应用滤镜。我目前尝试使用下拉列表的SelectedIndex来应用过滤器。

ie:如果用户选择地址,我必须不给名字的自动完成建议。

如果我在.asmx文件中默认设置了一个过滤器,自动完成工作(即:名称),sql过程没有任何问题,aspx页面也没有。我想知道是否有一种方法可以让我在。asmx文件中获得下拉列表的selecteindex或做同样事情的任何替代方法。

代码

TextBox + AutoCompleteExtender:

<asp:TextBox ID="txtValue" runat="server"  style="margin-bottom: 0px"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtenderSearchValue" runat="server" ServicePath="AutoComplete.asmx" 
    ServiceMethod="GetSuggestions"  TargetControlID="txtValue" MinimumPrefixLength="1" CompletionSetCount="10"
    EnableCaching="true" UseContextKey="true" ShowOnlyCurrentWordInCompletionListItem="true"></asp:AutoCompleteExtender>

在.asmx中获取AutoCompleteExtender数据的函数:

//will get all the suggestions from what the user typed.
public string[] GetSuggestions(string prefixText, int count, string contextKey)
{
    string name = null;
    string surname = null;
    string givenName = null;
    string title = null;
    string phone = null;
    string department = null;
    string location = null;
    DataTable dt = null;
    List<string> suggestions = new List<string>();
    dt = new DataTable("Users");
    //pr_SEL_Usr
    SqlCommand cmd = new SqlCommand(ConfigManager.SelUser);
    cmd.CommandType = CommandType.StoredProcedure;
    //set the parameters
    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = name;
    cmd.Parameters.Add("@Surname", SqlDbType.VarChar).Value = surname;
    cmd.Parameters.Add("@GivenName", SqlDbType.VarChar).Value = givenName;
    cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
    cmd.Parameters.Add("@Phone", SqlDbType.VarChar).Value = phone;
    cmd.Parameters.Add("@Division", SqlDbType.VarChar).Value = department;
    cmd.Parameters.Add("@Location", SqlDbType.VarChar).Value = location;
    cmd.Parameters.Add("@User_cd", SqlDbType.VarChar).Value = null;
    dt = DBUtils.Execute(cmd);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        if (i < count)
            suggestions.Add(dt.Rows[i][5].ToString());
        else
            break;
    }
    return suggestions.ToArray();
}

我已经尝试添加一个变量索引到函数和使用一个案例来设置参数,但没有工作。我在网上搜索了一种方法来做到这一点,但无济于事。net

我们可以将.asmx链接到asp.net控件吗?

在您的过滤器下拉列表中设置AutoPostBack="true"

SelectedIndexChanged事件添加到过滤器下拉列表中。在该事件方法中,设置AutoCompleteExtenderContextKey属性。

protected void FilterDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    AutoCompleteExtenderSearchValue.ContextKey = FilterDropDownList.SelectedValue;
}

现在您的GetSuggestions ASMX方法应该通过contextKey参数接收过滤器下拉列表值。

您的web服务无法引用页面上的控件。当你的JavaScript调用服务时,page对象已经不存在了。

你可以让你的web服务使用会话状态。然后,您可以添加一个新的调用,将过滤器设置从页面传递到服务。当自动完成扩展程序调用服务时,该过滤器信息仍将处于Session状态,即您在前一次调用中存储它的位置。