在代码隐藏中动态设置ASP.NET自动完成扩展程序contextKey
我想在我的页面中使用ASP.NET自动完成。我使用我的内部对象来加载数据,应该使用作为实例页面字段值的参数来查询autocomplete。因为我不能在静态自动完成扩展程序方法中使用实例字段,所以我找到了一个可以用来实现我的目标的方法 我在asp.net页面中有文本框和自动完成扩展程序:在代码隐藏中动态设置ASP.NET自动完成扩展程序contextKey,asp.net,autocomplete,Asp.net,Autocomplete,我想在我的页面中使用ASP.NET自动完成。我使用我的内部对象来加载数据,应该使用作为实例页面字段值的参数来查询autocomplete。因为我不能在静态自动完成扩展程序方法中使用实例字段,所以我找到了一个可以用来实现我的目标的方法 我在asp.net页面中有文本框和自动完成扩展程序: <asp:TextBox ID="PurchaseOrderItemsSearchTextBox" runat="server" EnableViewState="False"
<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"/>
但当我运行页面并在静态web方法中设置断点(该方法应返回字符串数组)时,我发现contextKey为null
[WebMethod]
[ScriptMethod]
public static string[] GetOrderOrStockNumbers(string prefixText,int count, string contextKey)
{
string[] returnStrings = new string[] {"1","2"};
return returnStrings;
}
非常感谢您的回答
鲁道夫。我找到了解决办法。按属性值设置的ContextKey在ASP.NET中无法工作,因为我在文章中发现.NET 4.0在属性值中转义撇号 一种解决方案是在web配置中禁用编码属性值,但这可能会造成安全问题
<httpRuntime encoderType="HtmlAttributeEncodingNot"/>
现在contextKey由Javascript正确设置,我可以在GetOrderOrStockNumbers方法中使用它的值。这些都不起作用,然后我发现您可以在页面加载下在代码中设置contextKey
ExtenderID.ContextKey = Control.SelectedValue;
而且它是有效的
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);
}
}
}
ExtenderID.ContextKey = Control.SelectedValue;