Implementing dropdown control dynamic Add options in ASP Web pages

Source: Internet
Author: User
Tags dsn implement odbc
Dynamic | control | Web Page One, the problem of the proposed
Active Server Pages (ASP) is a server-side scripting environment that combines the capabilities of HTML pages, script commands, and ActiveX components to create and run dynamic, interactive Web applications. However, in the production of ASP Web pages, when using the dropdown drop-down selection box control, it is usually possible to select only fixed values and not to add new options, which can cause great inconvenience to practical applications. To enhance the ability of the dropdown control to have dynamic add options, this article provides a way to combine the TextBox control with the dropdown control to enable the dropdown control to implement the ability to add options freely.

Second, the idea of problem solving
Create a simple database and configure the data source to establish a connection to the database. When the ASP page is accessed, the contents of the database are written sequentially into the dropdown control's selection. If you add a new option to the dropdown control, first enter the new option value in the TextBox control and add the value to the database. When you open the page again, you implement the ability to dynamically add options to the dropdown control.
Third, the realization of the problem
1. Build a simple database, for example, SQL Server, such as the vehicle type as a field field1, in the database to build a vehicle type table table1;
2. Configuring the DSN data source connection database
(1) Configuring the DSN data source
A, in the running Web server, open the Control Panel
b, double-click the ODBC icon, and then select System DSN
C, click Add, add a DSN entry, select an ODBC driver such as SQL Server, and click Finish
D, in the Data Source Name box, configure the DataSource name such as car, and then click Select to select the appropriate database.
E, test whether the connection succeeded
(2) connecting to the database. Use the ActiveX server widget to create a connection to the database. The database access widget uses ADO (Active Data object) to provide a connection object that you can use to establish and manage connections between applications and ODBC databases. The connection object has a variety of properties and methods that you can use to open and close the database connection and issue a query request to update the information. For example, the following script file Dbconn.inc creates a connection object, and then opens the database connection:
Set Ors=server.createobject ("Adodb.connection")
oRS.Open "dsn=car;uid=sa;pwd=****"
3. Add dropdown and TextBox controls to the ASP Web page firstweb.asp. Add data from the database to the DropDown1 control's Options bar when the page is refreshed, as shown in the following code:
<!--#include file= "Dbconn.inc"--> "to introduce the database connection file, this file and firstweb.asp should
' In the same directory
<body>
<form name= "Form1" method= "Post" action= "firstweb.asp" > "by Post Method Form1
' Information sent to the WEB server
The start of the <tr> ' line
<th align= "right" nowrap width= "35%" > Model:</th>
&LT;TD align= "left" nowrap width= "65%" >
<select name= "DropDown1" >
<option value= "" Selected> Please choose </option>
<% sql= "Select field1 from table1" To add data from the database to DropDown1 control
Set Orstemp=ors.execute (SQL) ' pieces in the options bar
While not orstemp.eof%>
<option value=<%=orstemp (0)%>><%=orstemp (0)%></option>
<%orstemp.movenext
Wend
%>
</select> ' Introduction of dropdown dropdown box
<input name= "TextBox1" value= ">" Introduction textbox Text Input Box
</tr>
</form>
<tr >
&LT;TD colspan=2 align= "center" > ' onclick event via the Submit button will form1 the
' Content submission
<input type= "image" src=http://www.163design.net/a/f/"Img/submit.gif" Onclick=document.form1.submit () >
</td>
</tr>
</body>
4. In response to the OnClick event, add the value added in TextBox1 to the database and add the following code to the current page:
<% If Request ("TextBox1") <> "" Then
Sql= "SELECT table1 from th000 where field1=" &request ("TextBox1")
Set Orstempt=ors.execute (SQL)
If Orstempt.eof Then
sqlt= "INSERT INTO table1 (field1) VALUES ('" &request ("TextBox1") & ")"
Ors.execute (SQLT)
End If
End If%>



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.