在代码后面动态设置ASP.NET自动完成扩展器contextKey

时间:2012-09-11 15:37:42

标签: asp.net autocomplete

我想在我的页面中使用ASP.NET自动完成功能。我使用内部对象来加载数据,并且应该使用作为实例页面字段值的参数来查询自动完成。因为我无法在静态自动完成扩展程序方法中使用实例字段,所以我找到了sample of code,可用于实现我的目标。

我在asp.net页面中有文本框和自动填充扩展程序:

<asp:TextBox ID="PurchaseOrderItemsSearchTextBox" runat="server" EnableViewState="False"
                            SkinID="SalesReturnTextBox" AutoCompleteType="Disabled" 
                            AutoPostBack="True" OnTextChanged="PurchaseOrderItemsSearchTextBoxTextChanged" 
                            onload="PurchaseOrderItemsSearchTextBoxLoad" />
<ajax:AutoCompleteExtender runat="server" TargetControlID="PurchaseOrderItemsSearchTextBox" ID="OrderOrStockNumberExtender"
                            ServiceMethod="GetOrderOrStockNumbers" MinimumPrefixLength="2" CompletionInterval="1000" UseContextKey="True"/>

在PurchaseOrderItemsSearchTextBoxLoad方法中,我想根据上面提到的例子,通过javascript动态设置上下文密钥。

protected void PurchaseOrderItemsSearchTextBoxLoad(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox!=null)
        {                
            string sapCode = Customer.Company.SapCode;
            const string PURCHASE_ORGANIZATION = PURCHASE_ORGANIZATION_HP;
            string purchaseGroup = string.Empty;
            string firstDateTime = FirstDateTextBox.Text;
            const string SEPARATOR = "#";
            var contextStrings = new[]{sapCode,PURCHASE_ORGANIZATION,purchaseGroup,firstDateTime};
            string context =string.Join(SEPARATOR, contextStrings);
            const string ON_KEY_UP = "onkeyup";
            string attributevalue = "$find('" + OrderOrStockNumberExtender.ClientID + "').set_contextKey(" + context +
                                    ");";
            textBox.Attributes.Add(ON_KEY_UP, attributevalue);
        }
    }

但是当我运行我的页面并在应该返回字符串数组的静态web方法中设置断点时,我发现contextKey为null。

[WebMethod]
[ScriptMethod]
public static string[] GetOrderOrStockNumbers(string prefixText,int count, string contextKey)
{            
    string[] returnStrings = new string[] {"1","2"};
    return returnStrings;
}

非常感谢您提供任何有用的答案,

鲁道夫。

2 个答案:

答案 0 :(得分:1)

这些都不起作用,然后我发现您可以在页面加载下的代码中设置上下文键

MATCH p=(:A)-[*..8]-(:C)--(:D)--(:E)
WHERE ALL(n IN NODES(p)[1..-3] WHERE 'B' IN LABELS(n))
RETURN DISTINCT [m IN NODES(p) | m.Name] AS names

它有效...

答案 1 :(得分:0)

我找到了解决方案。由属性值设置的ContextKey在ASP.NET中无法工作,因为.Net 4.0在this文章中找到属性值中的撇号。

一种解决方案是在Web配置中禁用编码属性值,但它可能会产生安全问题。

<httpRuntime encoderType="HtmlAttributeEncodingNot"/>

我宁愿改变我的方法,也没有按属性设置contextKey值,而是在文本框控件的prerender事件中注册了客户端脚本。

protected void PurchaseOrderItemsSearchTextBoxPreRender(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox != null)
        {
            string sapCode = Customer.Company.SapCode;
            const string PURCHASE_ORGANIZATION = PURCHASE_ORGANIZATION_HP;
            string purchaseGroup = string.Empty;
            string firstDateTime = FirstDateTextBox.Text;
            var contextStrings = new[] {sapCode, PURCHASE_ORGANIZATION, purchaseGroup, firstDateTime};
            string context = string.Join(SEPARATOR, contextStrings);
            const string ON_KEY_UP = "onkeyup";
            const string ON_KEY_UP_VALUE = "SetOrderOrStockExtenderContextKey();";
            textBox.Attributes.Add(ON_KEY_UP, ON_KEY_UP_VALUE);

            //disable autocomplete in firefox
            DisableAutocompleteInFirefox(textBox);

            const string ORDER_OR_STOCK_NUMBER_SCRIPT_KEY = "OrderOrStockNumberScript";
            if (!ClientScript.IsClientScriptBlockRegistered(ORDER_OR_STOCK_NUMBER_SCRIPT_KEY))
            {
                Type type = GetType();
                var scriptTextBuilder = new StringBuilder();
                const string FUNCTION_NAME = "function SetOrderOrStockExtenderContextKey()";
                scriptTextBuilder.AppendLine(FUNCTION_NAME);
                const string LEFT_BRACKET = "{";
                scriptTextBuilder.AppendLine(LEFT_BRACKET);
                const string FIND_COMMAND = "$find(\'";
                scriptTextBuilder.Append(FIND_COMMAND);
                scriptTextBuilder.Append(OrderOrStockNumberExtender.ClientID);
                const string SET_COTEXT_KEY = "\').set_contextKey('";
                scriptTextBuilder.Append(SET_COTEXT_KEY);
                scriptTextBuilder.Append(context);
                const string END_OF_COMMAND = "');";
                scriptTextBuilder.AppendLine(END_OF_COMMAND);
                const string RIGHT_BRACKET = "}";
                scriptTextBuilder.AppendLine(RIGHT_BRACKET);

                string setContextKeyFunction = scriptTextBuilder.ToString();

                ClientScript.RegisterClientScriptBlock(type, ORDER_OR_STOCK_NUMBER_SCRIPT_KEY, setContextKeyFunction,
                                                       true);
            }
        }
    }

现在,contextKey由Javascript正确设置,我可以在我的GetOrderOrStockNumbers方法中使用它的值。