AJAX is implemented in TextBox, dynamically displayed in the database into the drop-down box.

xiaoxiao2021-03-30  192

Function: Enter content in TextBox, dynamically from the database blurry query to the drop-down box for selection 1. Create an ASPX page, HTML code

<

HTML

>

<

HEAD

>

<

Title

>

Webform1

Title

>

<

Script

Language

= "JavaScript"

>

// City ------------------------------ Function CityResult () {var city = document.getlementByid ("TextBox1"); WebForm1.GetCityList (city.value, get_city_Result_CallBack);} function get_city_Result_CallBack (response) {if (! response.value = null) {// debugger;. document.getElementById ( "DropDownList1") style.display = "block"; document .getelementByid ("DropDownList1"). Length = 0; var DS = response.value; if (ds! = null && typeof (ds) == "Object" && Ds.tables! = null) {for (var i = 0 I

Province.Options [pindex] .text; document.getlementBYID ("<% = textbox1.clientID%>"). InnerText = PTEXT;}

Script

>

HEAD

>

<

Body

>

<

FORM

id

= "Form1"

Method

= "POST"

Runat

= "Server"

>

<

ASP: TEXTBOX

Id

= "TextBox1"

Runat

= "Server"

>

ASP: TEXTBOX

>

<

Br

>

<

ASP: DropDownList

Id

= "DROPDOWNLIST1"

Runat

= "Server"

Width

= "192px"

Style

= "Display: none"

>

ASP: DropDownList

>

FORM

>

Body

>

HTML

>

2.CS code

Using

System;

Using

System.collections;

Using

System.componentmodel;

Using

System.data;

Using

System.drawing;

Using

System.Web;

Using

System.Web.SessionState;

Using

System.Web.ui;

Using

System.Web.ui.WebControls;

Using

System.Web.ui.htmlControls;

Using

System.data.sqlclient;

Namespace

Ajaxselect

{/ ** ////

/// Summary Description for Webform1. /// public class Webform1: System.Web.ui.page {protected system.web.ui.webcontrols.textbox textbox1; protected System.Web.UI.WebControls.DropDownList DropDownList1; private void Page_Load (object sender, System.EventArgs e) {Ajax.Utility.RegisterTypeForAjax (typeof (WebForm1)); if (! Page.IsPostBack) {this.TextBox1.Attributes .Add ("onchange", ",", ",",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Protected Void OnInit (Eventargs E) {/// Codegen: This Call is Required by The ASP.NET Web Form Designer. // InitializationComponent (); Base.onit (E);} / ** //// /// Required Method for Designer support - do not modify /// the contents of this method with the code editor /// private void InitializeComponent () {this.Load = new System.EventHandler (this.Page_Load);} #. endregion getCityList # region getCityList [Ajax.AjaxMethod (Ajax.HttpSessionStateRequirement.Read)] public DataSet getCityList (int provinceid) {string sql = "select * from where father like '% provinceid "% city "'"; return GetDataSet ( SQL);

} #Endregion GetDataSet # region GetDataSet public static DataSet GetDataSet (string sql) {string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings [ "ConnectionString"]; SqlDataAdapter sda = new SqlDataAdapter (sql, ConnectionString); DataSet ds = new DataSet () SDA.FILL (DS); Return DS;} #ENDREGON}} 3.

Source code download

4. Database script

Create

TABLE

[

DBO

]

.

[

CITY

]

(

[

id

]

[

int

]

NOT

NULL

,

[

CityID

]

[

nvarchar

]

(

6

COLLATE_PRC_CI_AS

NULL

,

[

CITY

]

[

nvarchar

]

(

50

COLLATE_PRC_CI_AS

NULL

,

[

Father

]

[

nvarchar

]

(

6

COLLATE_PRC_CI_AS

NULL

,

Constraint

[

PK_CITY

]

Primary

Key

Clustered

(

[

id

]

ASC

)

WITH

(Ignore_Dup_Key

=

Off

)

On

[

Primary

]

)

On

[

Primary

]

转载请注明原文地址:https://www.9cbs.com/read-130608.html

New Post(0)