Ajax AutoCompleteExtender的问题 [英] Question with Ajax AutoCompleteExtender
问题描述
我有2个文本框的第一个名为txtCountry,第二个名为txtRegion,当您键入任何国家或地区时,使用AutoCompleteExtender从数据库中获取数据,所以问题是我想在文本框country =时仅获取区域数据库中的国家,但我使用它的方法不参与参数中的txtCountry.text,我也会得到错误。
在数据库中:
有2个表叫做国家和地区
$ b与区域相关的$ b和国家表作为主键表
这是页码:
< pre lang =HTML> <%@ 页面 语言 = < span class =code-keyword> C# AutoEventWireup = true CodeBehind = WebForm1.aspx.cs 继承 = WebApplication2.WebForm1 %>
<%@ 注册 汇编 = AjaxControlToolkit 命名空间 = AjaxControlToolkit TagPrefix = cc1 < span class =code-pagedirective>%>
< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http:// www。 w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns = http://www.w3.org/1999/ xhtml >
< head runat = server >
< ; title > < / title >
< link href = Style / StyleSheet.css rel = stylesheet 类型 = text / css / >
< / head >
< body >
< 表格 < span class =code-attribute> id = form1 runat = server >
< div >
& lt; asp:ScriptManager ID = ScriptManager1 runat = server
EnablePageMethods = true >
< / asp:ScriptManager >
< asp:TextBox ID = txtCountry runat = server > < / asp:TextBox >
< cc1:AutoCompleteExtender ServiceMethod = SearchCountry
MinimumPrefixLength = 2
CompletionInterval = 100 EnableCaching = < span class =code-keyword> false CompletionSetCount = 10
TargetControlID = txtCountry
ID = AutoCompleteExtender1 runat = server FirstRowSelected = false CompletionListCssCl ass = autocomplete_completionListElement
CompletionListItemCssClass = autocomplete_listItem
CompletionListHighlightedItemCssClass = autocomplete_highlightedListItem >
< / cc1:AutoCompleteExtender >
< <跨越式s =code-leadattribute> br / >
< br / >
< asp:TextBox ID = txtRegion runat = server > < / asp:TextBox >
< cc1:Au toCompleteExtender ServiceMethod = SearchRegion
MinimumPrefixLength = 2
CompletionInterval = 100 EnableCaching < span class =code-keyword> = false CompletionSetCount = 10
TargetControlID < span class =code-keyword> = txtRegion
< span class =code-attribute>
ID = AutoCompleteExtender2 runat = server FirstRowSelected = false CompletionListCssClass = autocomplete_completionListElement
CompletionListItemCssClass = autocomplete_listItem
CompletionListHighlightedItemCssClass = autocomplete_highlightedListItem >
< / cc1:AutoCompleteExtender >
< / div >
< / form >
< / body >
< / html >
这是带有2种方法的C#代码,名为SearchCountry和SearchRegion:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Configuration;
使用 System.Data;
使用 System.Data.SqlClient;
命名空间 WebApplication2
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static 列表< string> SearchCountry( string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings [ Jobs_DBConnectionString]。ConnectionString;
使用(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = 从 +
像@SearchCountry +'%'的国家;
cmd.Parameters.AddWithValue( @ SearchCountry,prefixText);
cmd.Connection = conn;
conn.Open();
List< string> Countries = new List< string>();
使用(SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Countries.Add(sdr [ 。国家跨度>]的ToString());
}
}
conn.Close();
返回国家/地区;
}
}
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static 列表< string> SearchRegion( string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings [ Jobs_DBConnectionString]。ConnectionString;
使用(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = 从Reigon中选择*,其中Country = @Country AND +
Reigon like @SearchRegion +'%';
cmd.Parameters.AddWithValue( @ Country,txtCountry.text);
cmd.Parameters.AddWithValue( @ SearchRegion,prefixText);
cmd.Connection = conn;
conn.Open();
List< string> Reigons = new List< string>();
使用(SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Reigons.Add(sdr [ 。Reigon跨度>]的ToString());
}
}
conn.Close();
返回 Reigons;
}
}
}
}
}
错误:
错误1非静态字段,方法或属性'WebApplication2.WebForm1.txtCountry'需要对象引用C:\ Users \Abdul \ Files \ Visual Visual Studio 2010 \Projects\WebApplication2 \WebApplication2 \WebForm1.aspx.cs 62 61 WebApplication2
您必须使用<$ c向静态方法发送其他参数$ c> UseContextKey =true。
参见示例 - ASP.NET AJAX AUTOCOMPLETEEXTENDER:使用上下文通过WEBMETHOD传递其他参数 [ ^ ]
conn.ConnectionString = ConfigurationManager.ConnectionStrings [ Jobs_DBConnectionString]。ConnectionString;
您正尝试以静态方法访问对象。为了使这个工作在静态方法中,尝试将连接字符串作为参数传递。
hi
i试图使它像那样但是第二种方法不行。但问题在于这行代码
cmd.Parameters.AddWithValue( @ Country,txtCountry.text);
i无法访问txtCountry
i不认为它来自连接字符串
谢谢
I have 2 textbox's first one called txtCountry and the second one called txtRegion work with AutoCompleteExtender to fetching data from database when you type any country or any region so the problem is i want to fetch only the region when the text box country = country in the database but the method that i use it doesn't take the txtCountry.text in the parameter and also i got error i will put it .
In database:
there are 2 tables called , Country and Region
and Country table related with Region as primary key table
This is page code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Style/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">
</asp:ScriptManager>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCountry"
MinimumPrefixLength="2"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtCountry"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false" CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
</cc1:AutoCompleteExtender>
<br />
<br />
<asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchRegion"
MinimumPrefixLength="2"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="txtRegion"
ID="AutoCompleteExtender2" runat="server" FirstRowSelected = "false" CompletionListCssClass="autocomplete_completionListElement"
CompletionListItemCssClass="autocomplete_listItem"
CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
</cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html>
And this is the C# code with 2 methods called SearchCountry and SearchRegion:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchCountry(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["Jobs_DBConnectionString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from Country where " +
"Country like @SearchCountry + '%'";
cmd.Parameters.AddWithValue("@SearchCountry", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> Countries = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Countries.Add(sdr["Country"].ToString());
}
}
conn.Close();
return Countries;
}
}
}
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchRegion(string prefixText,int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["Jobs_DBConnectionString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from Reigon where Country = @Country AND " +
"Reigon like @SearchRegion + '%'";
cmd.Parameters.AddWithValue("@Country", txtCountry.text);
cmd.Parameters.AddWithValue("@SearchRegion", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> Reigons = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
Reigons.Add(sdr["Reigon"].ToString());
}
}
conn.Close();
return Reigons;
}
}
}
}
}
The error:
Error 1 An object reference is required for the non-static field, method, or property 'WebApplication2.WebForm1.txtCountry' C:\Users\Abdul\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\WebForm1.aspx.cs 62 61 WebApplication2
You have to send additional parameters to the Static Method usingUseContextKey = "true"
.
See the Example - ASP.NET AJAX AUTOCOMPLETEEXTENDER: PASS ADDITIONAL PARAMETER TO WEBMETHOD USING CONTEXTKEY[^]
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Jobs_DBConnectionString"].ConnectionString;
You are trying to access an object in a static method. To make this work in a static method, try passing the connection string as a parameter.
hi
i tried to make it like that but the second method wont work . but the probelm was in this line of code
cmd.Parameters.AddWithValue("@Country", txtCountry.text);
i can not access to txtCountry
i don't think it's from the connection string
thank you
这篇关于Ajax AutoCompleteExtender的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!