如何在从母版页继承的页面中创建自动完成文本框和按钮 [英] How to create autocomplete textbox and button in page inherited from master page

查看:47
本文介绍了如何在从母版页继承的页面中创建自动完成文本框和按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在两个星期内搜索这个问题并且我做了但是它没有用。

代码我的网站是:

1. web.config



< configuration>

< connectionstrings> ;

< add name =constringconnectionstring =Data Source = .; Initial Catalog = employee; UID =; PWD =>

providerName =System .Data.SqlClient/>



< system.web>

< compilation debug =falsetargetframework =4.0 >

< assemblies>

< add assembly =System.Design,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = B03F5F7F11D50A3A>

< add assembly =System.Web.Extensions.Design,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31BF3856AD364E35>

< add assembly = System.Windows.Forms,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = B77A5C561934E089>











2. MasterPage.master

<%@ Master Language =C#AutoEventWireup =trueCodeFile =MasterPage.master.csInherits =MasterPage%>



<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">



< html xmlns =http://www.w3.org/1999/xhtml>

< head runat =server>

< title>< / title>



< / head>

< body>

< form id =form1runat =server>

Hi,

I search for this problem since two weeks and I make it but it didn't work.
The code of my web site is:
1. web.config

<configuration>
<connectionstrings>
<add name="constring" connectionstring="Data Source=.;Initial Catalog=employee;UID=;PWD=">
providerName="System.Data.SqlClient"/>

<system.web>
<compilation debug="false" targetframework="4.0">
<assemblies>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A">
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089">





2. MasterPage.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>

</head>
<body>
<form id="form1" runat="server">



< asp:ScriptManager ID = ScriptManager1runat =serverEnablePartialRendering =true>



< services>



< asp:ServiceReference Path =AutoComplete.asmx/>











< asp:ContentPlaceHolder id =MainCon帐篷runat =服务器>






<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">

<services>

<asp:ServiceReference Path="AutoComplete.asmx" />





<asp:ContentPlaceHolder id="MainContent" runat="server">




< / form>

< / body>

< / html>



3.Default.aspx
<%@ Page Title =Language =C#MasterPageFile =〜/ MasterPage.masterAutoEventWireup =trueCodeFile =Default.aspx.csInherits =_ Default%>



<%@ Register assembly =AjaxControlToolkitnamespace =AjaxControlToolkittagprefix =asp%>









< asp:Content ID =BodyContentContentPlaceHolderID =MainContentRunat =Server> ;

< asp:TextBox ID =TextBox1runat =serverAutoPostBack =True>

< asp:AutoCompleteExtender ID =TextBox1_AutoCompleteExtenderrunat =server

ServiceMethod =GetCompletionListMinimumPrefixLength =1CompletionSetCount =5

EnableCaching =trueUseContextKey =True

DelimiterCharacte rs =Enabled =TrueServicePath =AutoComplete.asmx

TargetControlID =TextBox1>









4. AutoComplete.cs.asmx

使用System;

使用System。 Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Data.SqlClient;

使用System.Data;

使用System.Web.Services;

使用System.Web.Services.Protocols;

使用System .Web.Script.Services;

使用System.Configuration;



名称空间AjaxToolkitExtender

{

///< summary>

///自动完成的摘要说明

///



//要允许使用ASP.NET AJAX从脚本调用此Web Service,请取消注释以下行。

[WebService(Namespace =http://tempuri.org/)]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]



公共类AutoComplete:System.Web.Services.WebService

{

[WebMethod]

公共字符串[] GetCompletionList(string prefixText,int count)

{

if(count == 0)

{

count = 10;

}

DataTable dt = GetRecords(prefixText);

List< string> items = new List< string>(count);



for(int i = 0; i< dt.Rows.Count; i ++)

{

string strName = dt.Rows [i] [0] .ToString();

items.Add(strName);

}

返回items.ToArray();

}



public DataTable GetRecords(string strName)

{

string strConn = ConfigurationManager.ConnectionStrings [constring]。ConnectionString;

SqlConnection con = new SqlConnection(strConn);

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandType = System.Data.CommandType.Text;

cmd.Parameters.AddWithValue(@ Name,strName);

cmd.CommandText =从emp中选择EMPLOYEE_NAME,其中EMPLOYEE_NAME喜欢'%'+ @ Name +'%' ;

DataSet objDs = new DataSet();

SqlDataAdapter dAdapter = new SqlDataAdapter();

dAdapter.SelectCommand = cmd;

con.Open();

dAdapter.Fill(objDs);

con.Close();

返回objDs.Tables [0] ;





}



}





}







请任何人帮助我


</form>
</body>
</html>

3.Default.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>




<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True">
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
ServiceMethod="GetCompletionList" MinimumPrefixLength="1" CompletionSetCount="5"
EnableCaching="true" UseContextKey="True"
DelimiterCharacters="" Enabled="True" ServicePath="AutoComplete.asmx"
TargetControlID="TextBox1">




4. AutoComplete.cs.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Configuration;

namespace AjaxToolkitExtender
{
/// <summary>
/// Summary description for AutoComplete
///

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class AutoComplete : System.Web.Services.WebService
{
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
if (count == 0)
{
count = 10;
}
DataTable dt = GetRecords(prefixText);
List<string> items = new List<string>(count);

for (int i = 0; i < dt.Rows.Count; i++)
{
string strName = dt.Rows[i][0].ToString();
items.Add(strName);
}
return items.ToArray();
}

public DataTable GetRecords(string strName)
{
string strConn = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@Name", strName);
cmd.CommandText = "select EMPLOYEE_NAME from emp where EMPLOYEE_NAME like '%'+@Name+'%'";
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
return objDs.Tables[0];


}

}


}



Please any one help me

推荐答案

尝试使用pagemethods通过javascript ononkeypress或onchangeevent of text box。
try using pagemethods through javascript on "onkeypress" or onchange" event of text box.


这篇关于如何在从母版页继承的页面中创建自动完成文本框和按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆