【问题标题】:Set ASP.NET autocomplete extender contextKey dynamically in code behind在后面的代码中动态设置 ASP.NET 自动完成扩展器 contextKey
【发布时间】:2012-09-04 14:19:36
【问题描述】:

我想在我的页面中使用 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 为空。

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

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

鲁道夫。

【问题讨论】:

    标签: asp.net autocomplete


    【解决方案1】:

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

    ExtenderID.ContextKey = Control.SelectedValue;
    

    而且它有效...

    【讨论】:

      【解决方案2】:

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

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

      <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 方法中使用它的值。

      【讨论】:

        猜你喜欢
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 2017-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多