Implement Dynamic Data linkage with Asp+javascript, do not refresh

Source: Internet
Author: User
Tags array end implement odbc variable
Javascript| News | data | Refresh/////////////by xxrl (Confucius Yue Cheng, Meng Yue take e)

Chinese: King Jianhua

Email:jjh_115@eyou.com

linkage, linkage, linkage .... Troubled a lot of netizens nerves, in the csdn-asp plate, always see netizens cry for help, save what? Linkage! Why linkage such attention, its practicality is justifiable, users can also be recognized, but if the data is large and interrelated, the problem comes, how to identify and show is a very distressed problem. Is there a good solution? The answer is yes, because we have ASP and JavaScript, haha, then let's get moving!

We want to get the data, since to achieve the effect of linkage, it is certain that the data is related, then we use such an example to illustrate our approach

Prepare the condition:

SQL SERVER 2000 Chinese Enterprise Edition, iis5.0+,ie5.0+, of course, it's best to have a good editor, VS. NET is good, of course, if you are a supporter of Notepad, then I have no way. L

We in the personnel management of the departmental level of linkage methods, understand the management of the departmental level can be defined as such, but also the definition of the actual enterprise rules, * * Chemical Plant/** System/** Department, for example, is

XX Chemical Plant/Marketing System/Marketing department, in the text, Firstorganization table corresponds to the "XX chemical plant", secondorganization corresponds to the "Marketing system", thirdorganization corresponding to the "marketing department"



In SQL SERVER 2000 to create a new two tables, or three tables, in order for us to play a greater function of linkage, we built three tables, hehe.

Database name: Xxrl_study, username xxrl_study, password xxrl_study

Then build an ODBC data source, you can not, but I use, hehe

ODBC Name: Xxrl_ ODBC, with user name Xxrl_study, password xxrl_study connection, point to Xxrl_study database, default Chinese settings, test ――>ok

New table:

First table Firstorganization

The SQL script is as follows:

if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Firstorganization] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)

drop table [dbo]. [Firstorganization]

Go



CREATE TABLE [dbo]. [Firstorganization] (

[ID] [int] IDENTITY (1, 1) not NULL,

[OrganizationName] [varchar] (MB) COLLATE chinese_prc_ci_as not NULL,

[Description] [varchar] (8000) COLLATE chinese_prc_ci_as NULL

) on [PRIMARY]

Go

Second table Secondorganization

The SQL script is as follows:

if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Secondorganization] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)

drop table [dbo]. [Secondorganization]

Go



CREATE TABLE [dbo]. [Secondorganization] (

[ID] [int] IDENTITY (1, 1) not NULL,

[ParentID] [INT] Not NULL,

[OrganizationName] [varchar] (MB) COLLATE chinese_prc_ci_as not NULL,

[Description] [varchar] (8000) COLLATE chinese_prc_ci_as NULL,

[OrderNumber] [INT] Null

) on [PRIMARY]

Go



Third Table Thirdorganization

The SQL script is as follows:

if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Thirdorganization] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)

drop table [dbo]. [Thirdorganization]

Go



CREATE TABLE [dbo]. [Thirdorganization] (

[ID] [int] IDENTITY (1, 1) not NULL,

[ParentID] [INT] Not NULL,

[OrganizationName] [varchar] (MB) COLLATE chinese_prc_ci_as not NULL,

[Description] [varchar] (8000) COLLATE chinese_prc_ci_as NULL,

[OrderNumber] [INT] Null

) on [PRIMARY]

Go



OK,ODBC, we have built, the database has been built, as for the configuration of the site, I think we all should know, here no longer stated. Here My Site IP is http://200.100.100.88 (internal network IP)

Finally began to write programs, good tired, poof, poof, just help my colleagues to move things, rest for a while, coffee-ing ...

Well, in order to facilitate the IIS of his old people, we are too lazy to knock on the extra letters, we are smart, not hard-working programmers, the programmer has been tired enough, but also not convenient for ourselves? Hey, I applaud, clap, faint, where's the firecracker? The child of what family is also bad good tube ... J

New Default.asp file, we are writing the following program,

Set up a database connection object,

Set Objconn=server. CreateObject ("Adodb.connection")

objconn. Open "Xxrl_ ODBC", "Xxrl_study", "Xxrl_study"

And then open the 3 datasets we're going to be working on.

Dim sql1,sql2,sql3

Sql1= "SELECT * from Firstorganization"

SQL2 = "SELECT * FROM Secondorganization"

SQL3 = "SELECT * FROM Thirdorganization"

'/////////////////////////Open first Organization library

Set Rs1=server. CreateObject ("Adodb.recordset")

Rs1. Open sql1,objconn,1,1



Set Rs2=server. CreateObject ("Adodb.recordset")

Rs2. Open sql2,objconn,1,1



Set Rs3=server. CreateObject ("Adodb.recordset")

Rs3. Open sql3,objconn,1,1



Build a Select type of HTTP control, which is included in the form form as follows:

' The initial value of the////////////////////control is read from the firstorganization, firstorganization in this case, only one of the data is XX Chemical factory code as follows:

<form name=form1 style= "margin:0;" method= "POST" >

<select name=firstorganization>

<%

If Rs1. RecordCount >0 Then

Response.Write "<option value=" &rs1 ("id") & ">" &rs1 ("OrganizationName") & "</option>"

Else

Response.Write "Not Configured"

End If

%>

</select>

</form>

Okay, here's the head of our linkage function.

Now we're going to build a second select control, in the second select control, we use the onchange function, and for a detailed explanation of this function, see MSDN. The code is as follows (included in the form):

<select name=secondorganization onchange= "Changelocationi (Form1. Secondorganization.options[form1. Secondorganization.selectedindex].value); " >

<option value= "" >-------------</option>

<%

If rs2. Recordcount<0 Then

Response.Write "<option value=" "" > There is no department in the library! </option> "

Else

While not rs2.eof

Response.Write "<option value=" &rs2 ("id") & ">" &rs2 ("OrganizationName") & "</option>"

Rs2.movenext

Wend

End If

%>

</select>

OK, the second one is done, so let's build a third select also in the form form, as follows:

<select name=thirdorganization>

</select>

Don't forget to close the dataset Oh,

Rs1.close

Set rs1 = Nothing

Rs2.close

Set rs2 = Nothing

Rs3.close

Set Rs3 = Nothing

OK, the page elements are all ready, let's begin our real linkage tour.

This example uses ASP and JavaScript to interact with the data in the database, because we create a new script script before the form form, because we want to interact, so we have to write so that we can interact well

<%= "<script language=javascript>"%>

</script>

Then we can write our program in the script block.

Our basic operation is based on the two-level drop-down to draw three-level Drop-down, the second class we have taken out of the database, the following we want to take three-level organization data, as follows

<%= "<script language=javascript>"%>

<% ' ASP block

Dim sql_getthirdorganization

sql_getthirdorganization = "SELECT * from thirdorganization ORDER BY id DESC"

Set rs_getthirdorganization = Server. CreateObject ("Adodb.recordset") Rs_getthirdorganization.open sql_getthirdorganization,objconn,1,1

%>

var temp,temp_2;//////////////javascript block

TEMP=0;///////////////////FOR Loop Variable Initialization

Related = new Array ();//////////////////////array that holds the ID, name, and corresponding parent ID of the three-level organization

<%

temp_2 = 0 ' A temporary variable used to store the number of three levels of organization

While not rs_getthirdorganization.eof ' Loop level III organization

%>

Three-dimensional corresponding third-level organization ID, the name of the third level organization

Parent ID of the third level organization (that is, the second level ID)

Related[<%=temp_2%>] = new Array ("<%=rs_getthirdorganization" ("id")%> "," <%=rs_getthirdorganization ( "OrganizationName")%> "," <%=rs_getthirdorganization ("ParentID")%> ");

<%

temp_2 = temp_2 + 1

Rs_getthirdorganization.movenext

Wend

%>

temp = <%=temp_2%>;

function changelocation (ID) {////

var id = ID; Index value of item selected for acceptance of level two menu

document.form1.thirdorganization.length=0; Initializes the length of the third-level menu, starting with the subscript 0

var i = 0;

Initializes the value of the text and Value property of the third level menu, the first parameter value is Text "--------", and the second is////////value null value

Document.form1.thirdorganization.options[0]=new Option ('-------', ');

Loop array, comparing the number of the third dimension (parent ID) of the array with the numbers passed by the function.

for (i=0;i< temp;i++) {

if (related[i][2]==id) {/////////If equal, it is shown that a subset of the second-level organization is entered in the third level

and assigns the value of the subset (third level) to the third Select,

Document.form1.thirdorganization.options[document.form1.thirdorganization.length] = new Option (Related[i][1], Related[i][0]);

}

}

}

</script>

Finally at the end of the page do not forget to add objconn.close Oh, hehe

The main thing described above is a third select that is based on selecting a second select, so if we define a function in the third select control as follows:

<select name=thirdorganization onchange= "Fnchangeagain (Form1. Thirdorganization.options[form1. Thirdorganization.selectedindex].value) ">

</select>

This way, and then write a similar function in the script block, function content with the Changelocation function, is not achieved three-level linkage, so on and so on, oh, 10-level linkage can be done, but only trouble, of course, you can also use other methods, or a table to correspond to the algorithm , this casually, I This example is also from my actual development of a little experience accumulation, mainly for thinking clearly, for the people do not understand the program good maintenance, so will organize separate, because I never maintenance, hey.

Summer is over, hehe, wish you all good health! I wish the future of the Mid-Autumn Festival happy, I want to be a person in the field again.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.