AutoComplete Extender对于两个文本框 [英] AutoComplete Extender For two textboxs

查看:78
本文介绍了AutoComplete Extender对于两个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个以sql为后端的文本框.如果选择了textbox1值,则将在textbox2上更新值.任何可以告诉如何插入值.预先感谢.

I am using two textboxs with sql as backend. if textbox1 values is selected then value will be updated on textbox2. Can Any tell how to insert values. Thanks in advance.

推荐答案

告诉您如何使用数据集中的项目集合填充文本框


.vb文件上的代码

导入System.Collections.Generic
导入System.Web.Services
导入ENTech.WebControls
导入System.Data.SqlClient
导入系统
导入System.Data
导入System.Linq
导入System.Web
导入System.Web.UI
导入System.Web.UI.WebControls
局部类_Default
继承System.Web.UI.Page
私有共享函数GetCityMenuItemsFromDataReader(ByVal reader as SqlDataReader,_
ByVal usePaging为布尔值,_
ByVal pageIndex为整数,_
ByVal pageSize As Integer,_
ByVal appendCountryName作为Boolean)作为Generic.List(Of AutoSuggestMenuItem)
Dim menuItems作为New Generic.List(Of AutoSuggestMenuItem)

昏暗的城市As String
暗淡的CityCode作为字符串
昏暗状态为字符串
昏暗的国家作为弦乐

暗淡的标签为字符串

昏暗的菜单项作为AutoSuggestMenuItem
昏暗的rowIndex为整数= 0

''处理分页
昏暗的startRowIndex为整数= 0
昏暗的endRowIndex为整数= 0

如果使用usePaging然后
startRowIndex = pageIndex * pageSize
endRowIndex = startRowIndex + pageSize
如果结束


而reader.Read()
如果使用usePaging然后
''进入页面的开头
如果rowIndex< startRowIndex然后
rowIndex = rowIndex +1
继续而


''如果页面末尾则中断循环
如果rowIndex> = endRowIndex然后
While While
如果结束
如果结束
如果结束


''使用城市,国家和地区创建标签状态
城市= reader.GetString(0)
cityCode = reader.GetString(1)
国家= reader.GetString(2)

如果IsDBNull(reader.GetValue(3))然后
状态="
其他
状态= reader.GetString(3)
如果结束

标签=城市

''附加城市或国家
如果(状态<>")然后
标签& ="
其他
如果appendCountryName然后
标签& ="
如果结束
如果结束

menuItem =新的AutoSuggestMenuItem()
menuItem.Label =标签
menuItem.Value = cityCode

menuItems.Add(menuItem)

rowIndex = rowIndex +1
结束时


GetCityMenuItemsFromDataReader = menuItems
最终功能

私有共享函数GetConnectionString()作为字符串

Dim connString As String =连接字符串"

GetConnectionString = connString
最终功能



< WebMethod()> _
公共共享函数GetCitySuggestions(ByVal关键字为String,_
ByVal countryCode为字符串,_
ByVal usePaging为布尔值,_
ByVal pageIndex为整数,_
ByVal pageSize作为整数)作为字符串

Dim menuItems作为Generic.List(Of AutoSuggestMenuItem)

昏暗的connString作为字符串= GetConnectionString()
Dim cn As SqlConnection =新的SqlConnection(connString)

''可能需要重复以下3次
''而是将其保存到变量中
Dim sqlFromAndWhere As String ="FROM(产品INNER JOIN品牌ON product.productcate = brand.productcate)"& _
左外连接厂商上的product.id = vendor.id"& _
"WHERE(product.productcate像''"& keyword.Replace(''",''''")&%'')"

''如果指定了国家代码,则将其附加到WHERE子句中
如果不是String.IsNullOrEmpty(countryCode)然后
sqlFromAndWhere& ="brand.productcate ="&国家/地区代码""
如果结束


昏暗的SQL作为字符串

如果使用usePaging然后
''仅选择直到指定页面的菜单项
''在Sql Server中,使用ROW_NUMBER仅获取当前页面的值
Dim numItems As Integer =(pageIndex +1)* pageSize
sql ="SELECT product.productcate作为CityName,"& _
"product.productunit作为CityCode,"& _
"brand.productcate as CountryName",& _
"vendor.vendorname as StateName"& _
sqlFromAndWhere& _
"ORDER BY product.productcate"
其他

sql ="SELECT product.productcate作为CityName,"& _
"product.productunit作为CityCode,"& _
"brand.productcate as CountryName",& _
"vendor.vendorname as StateName"& _
sqlFromAndWhere& _
"ORDER BY product.productcate"
如果结束


Dim cmd As SqlCommand =新的SqlCommand(sql,cn)
cn.Open()

``我使用datareader是因为它通常比dataSet
要快得多 ''但是缓存的DataSet也可能起作用
昏暗的阅读器As SqlDataReader = cmd.ExecuteReader()

昏暗的appendCountryName为Boolean = String.IsNullOrEmpty(countryCode)
menuItems = GetCityMenuItemsFromDataReader(阅读器,usePaging,pageIndex,pageSize,appendCountryName)

reader.Close()

''使用分页时需要获取totalResults
昏暗的totalResults作为整数= -1

如果usePaging And pageIndex = 0则
''仅在页面索引为0时执行此操作
sql ="SELECT COUNT(*)"& sqlFromAndWhere

cmd =新的SqlCommand(sql,cn)
totalResults = CType(cmd.ExecuteScalar(),Integer)
如果结束

cn.Close()

GetCitySuggestions = AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems,totalResults)
最终功能

< WebMethod()> _
公共共享函数GetCountrySuggestions(ByVal关键字为String,ByVal usePaging为布尔值,ByVal pageIndex为整数,ByVal pageSize为整数)作为String
Dim menuItems作为New Generic.List(Of AutoSuggestMenuItem)

昏暗的connString作为字符串= GetConnectionString()
Dim cn As SqlConnection =新的SqlConnection(connString)

Dim sql As String ="SELECT productunit,productcate from product"& _
"WHERE productunit LIKE"& keyword.Replace(''",''''")& %"& _
"ORDER BY productunit"


昏暗的cmd作为新的SqlCommand(sql,cn)
cn.Open()

昏暗的阅读器As SqlDataReader = cmd.ExecuteReader()

昏暗的菜单项作为AutoSuggestMenuItem

而reader.Read()
menuItem =新的AutoSuggestMenuItem()

menuItem.Label = reader.GetString(0)
menuItem.Value = reader.GetString(1)

menuItems.Add(menuItem)
结束时


reader.Close()

cn.Close()
GetCountrySuggestions = AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems,-1)''如果不使用分页,则将忽略总结果(-1)}
最终功能


< WebMethod()> _
公共共享函数GetCountryInfo(ByVal cityCode作为字符串)作为String
昏暗的作家作为新的XJsonWriter()

昏暗的connString作为字符串= GetConnectionString()
Dim cn作为新的SqlConnection(connString)

Dim sql As String =选择product.productcate,product.productunit,brand.productcate作为CityName"& _
"FROM产品INNER JOIN品牌ON brand.id = product.id"& _
"WHERE product.productunit ="&城市法规""

昏暗的cmd作为新的SqlCommand(sql,cn)
cn.Open()

昏暗的阅读器As SqlDataReader = cmd.ExecuteReader()
如果是reader.Read()然后
writer.WriteNameValue("countryCode",reader.GetString(0))
writer.WriteNameValue("countryName",reader.GetString(1))
writer.WriteNameValue("cityName",reader.GetString(2))
其他
引发New ArgumentException(城市代码"& cityCode&";无效","cityCode")
如果结束

reader.Close()
cn.Close()

返回writer.ToString()
最终功能
结束类
Code on .vb file

Imports System.Collections.Generic
Imports System.Web.Services
Imports ENTech.WebControls
Imports System.Data.SqlClient
Imports System
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Class _Default
Inherits System.Web.UI.Page
Private Shared Function GetCityMenuItemsFromDataReader(ByVal reader As SqlDataReader, _
ByVal usePaging As Boolean, _
ByVal pageIndex As Integer, _
ByVal pageSize As Integer, _
ByVal appendCountryName As Boolean) As Generic.List(Of AutoSuggestMenuItem)
Dim menuItems As New Generic.List(Of AutoSuggestMenuItem)

Dim city As String
Dim cityCode As String
Dim state As String
Dim country As String

Dim label As String

Dim menuItem As AutoSuggestMenuItem
Dim rowIndex As Integer = 0

''Handle paging
Dim startRowIndex As Integer = 0
Dim endRowIndex As Integer = 0

If usePaging Then
startRowIndex = pageIndex * pageSize
endRowIndex = startRowIndex + pageSize
End If


While reader.Read()
If usePaging Then
''Get to the start of the page
If rowIndex < startRowIndex Then
rowIndex = rowIndex + 1
Continue While


''Break out of the loop if end of the page
If rowIndex >= endRowIndex Then
Exit While
End If
End If
End If


''Build label using City, Country & State
city = reader.GetString(0)
cityCode = reader.GetString(1)
country = reader.GetString(2)

If IsDBNull(reader.GetValue(3)) Then
state = ""
Else
state = reader.GetString(3)
End If

label = city

''Append either city or country
If (state <> "") Then
label &= " "
Else
If appendCountryName Then
label &= " "
End If
End If

menuItem = New AutoSuggestMenuItem()
menuItem.Label = label
menuItem.Value = cityCode

menuItems.Add(menuItem)

rowIndex = rowIndex + 1
End While


GetCityMenuItemsFromDataReader = menuItems
End Function

Private Shared Function GetConnectionString() As String

Dim connString As String = "connection string"

GetConnectionString = connString
End Function



<WebMethod()> _
Public Shared Function GetCitySuggestions(ByVal keyword As String, _
ByVal countryCode As String, _
ByVal usePaging As Boolean, _
ByVal pageIndex As Integer, _
ByVal pageSize As Integer) As String

Dim menuItems As Generic.List(Of AutoSuggestMenuItem)

Dim connString As String = GetConnectionString()
Dim cn As SqlConnection = New SqlConnection(connString)

''May need to repeat the following 3 times
''Instead just save it to variable
Dim sqlFromAndWhere As String = " FROM (product INNER JOIN brand ON product.productcate=brand.productcate) " & _
" LEFT OUTER JOIN vendor ON product.id=vendor.id " & _
" WHERE (product.productcate LIKE ''" & keyword.Replace("''", "''''") & "%'')"

''Append country code to WHERE clause if specified
If Not String.IsNullOrEmpty(countryCode) Then
sqlFromAndWhere &= " brand.productcate=''" & countryCode & "''"
End If


Dim sql As String

If usePaging Then
''Select only menu items up to specified page
''In Sql Server use ROW_NUMBER to only get the values for current page
Dim numItems As Integer = (pageIndex + 1) * pageSize
sql = "SELECT product.productcate as CityName, " & _
"product.productunit as CityCode, " & _
"brand.productcate as CountryName, " & _
"vendor.vendorname as StateName " & _
sqlFromAndWhere & _
" ORDER BY product.productcate"
Else

sql = "SELECT product.productcate as CityName, " & _
"product.productunit as CityCode, " & _
"brand.productcate as CountryName, " & _
"vendor.vendorname as StateName " & _
sqlFromAndWhere & _
" ORDER BY product.productcate"
End If


Dim cmd As SqlCommand = New SqlCommand(sql, cn)
cn.Open()

''I use datareader because it is usually much faster then dataSet
''But cached DataSet may also work
Dim reader As SqlDataReader = cmd.ExecuteReader()

Dim appendCountryName As Boolean = String.IsNullOrEmpty(countryCode)
menuItems = GetCityMenuItemsFromDataReader(reader, usePaging, pageIndex, pageSize, appendCountryName)

reader.Close()

''When using paging need to get totalResults
Dim totalResults As Integer = -1

If usePaging And pageIndex = 0 Then
''Only do it when page index is 0
sql = "SELECT COUNT(*)" & sqlFromAndWhere

cmd = New SqlCommand(sql, cn)
totalResults = CType(cmd.ExecuteScalar(), Integer)
End If

cn.Close()

GetCitySuggestions = AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems, totalResults)
End Function

<WebMethod()> _
Public Shared Function GetCountrySuggestions(ByVal keyword As String, ByVal usePaging As Boolean, ByVal pageIndex As Integer, ByVal pageSize As Integer) As String
Dim menuItems As New Generic.List(Of AutoSuggestMenuItem)

Dim connString As String = GetConnectionString()
Dim cn As SqlConnection = New SqlConnection(connString)

Dim sql As String = "SELECT productunit, productcate FROM product " & _
" WHERE productunit LIKE ''" & keyword.Replace("''", "''''") & "%''" & _
" ORDER BY productunit"


Dim cmd As New SqlCommand(sql, cn)
cn.Open()

Dim reader As SqlDataReader = cmd.ExecuteReader()

Dim menuItem As AutoSuggestMenuItem

While reader.Read()
menuItem = New AutoSuggestMenuItem()

menuItem.Label = reader.GetString(0)
menuItem.Value = reader.GetString(1)

menuItems.Add(menuItem)
End While


reader.Close()

cn.Close()
GetCountrySuggestions = AutoSuggestMenu.ConvertMenuItemsToJSON(menuItems, -1) ''Total results (-1) is ignored if paging is not used }
End Function


<WebMethod()> _
Public Shared Function GetCountryInfo(ByVal cityCode As String) As String
Dim writer As New XJsonWriter()

Dim connString As String = GetConnectionString()
Dim cn As New SqlConnection(connString)

Dim sql As String = "SELECT product.productcate, product.productunit, brand.productcate as CityName " & _
" FROM product INNER JOIN brand ON brand.id=product.id " & _
" WHERE product.productunit=''" & cityCode & "''"

Dim cmd As New SqlCommand(sql, cn)
cn.Open()

Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.Read() Then
writer.WriteNameValue("countryCode", reader.GetString(0))
writer.WriteNameValue("countryName", reader.GetString(1))
writer.WriteNameValue("cityName", reader.GetString(2))
Else
Throw New ArgumentException("City code ''" & cityCode & "'' is not valid", "cityCode")
End If

reader.Close()
cn.Close()

Return writer.ToString()
End Function
End Class


.aspx上的代码

Code on .aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register TagPrefix="Custom" Namespace="ENTech.WebControls" Assembly="AutoSuggestMenu" %>
<%@ Register namespace="ENTech.WebControls" tagprefix="WebControls" %>
<!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>
    <style type="text/css">
        .note { FONT-SIZE: 8pt; COLOR: gray; FONT-FAMILY: Verdana, helvetica, 'sans serif' }
    </style>
    <script src="javascript/autocomplete.js" type="text/javascript"></script>
    <link href="Custom.css" rel="stylesheet" type="text/css" />
    <link href="asm_includes/AutoSuggestMenu.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style3
        {
            width: 40px;
        }
        .style4
        {
            width: 40px;
            font-weight: bold;
        }
        .style5
        {
            text-align: left;
        }
    p.MsoNormal
    {margin-top:0in;
    margin-right:0in;
    margin-bottom:10.0pt;
    margin-left:0in;
    line-height:115%;
    font-size:large;
    font-family:"Calibri","sans-serif";
            width: 99px;
            height: 20px;
        }
        .style6
        {
            width: 156px;
            text-align: left;
        }
        .style7
        {
            width: 155px;
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:TextBox ID="t1" runat="server"></asp:TextBox>
                    <span style="font-size:11.0pt;line-height:115%;
font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
Calibri;mso-fareast-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:&quot;Times New Roman&quot;;mso-bidi-theme-font:minor-bidi;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA">
                    <custom:AutoSuggestMenu ID="asmCity" runat="server" KeyPressDelay="50"

                        MaxSuggestChars="6" OnClientTextBoxUpdate="onCityTextBoxUpdate"

                        OnGetSuggestions="onGetCitySuggestions" TargetControlID="t1"

                        UsePageMethods="false" />
                    </span>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:TextBox ID="t2" runat="server"></asp:TextBox>
                    <span style="font-size:11.0pt;line-height:115%;
font-family:&quot;Calibri&quot;,&quot;sans-serif&quot;;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
Calibri;mso-fareast-theme-font:minor-latin;mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:&quot;Times New Roman&quot;;mso-bidi-theme-font:minor-bidi;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA">
                    <custom:AutoSuggestMenu ID="asmCountry" runat="server"

                        OnGetSuggestions="GetCountrySuggestions" TargetControlID="t2"

                        Visible="False" />
                    </span>
                    <asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="True">
                    </asp:ScriptManager>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


这篇关于AutoComplete Extender对于两个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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