ASP.NET中怎么从下拉列表框怎么读数据进数据库

ASP.NET中怎么从下拉列表框怎么读数据进数据库,第1张

String

connectiongstring

=

ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString

SqlConnection

sqlcon

=

new

SqlConnection(connectiongstring)

string

Brand

=

BDropDownList.SelectedValue.ToString()//下拉列表

string

CmdText

=

"SELECT

Model

FROM

[CarType]

WHERE

Brand=\'"

+

Brand

+

"\'"

SqlCommand

sqlcom

=

new

SqlCommand(CmdText,

sqlcon)

try

{

sqlcon.Open()

SqlDataReader

dr

=

sqlcom.ExecuteReader()

。。。。。。。。。。

}

catch

(SqlException

ex)

{

}

//清理

finally

{

if

(sqlcon

!=

null)

{

sqlcon.Close()

}

}

还有需要注意的是autopostback属性设置成true

思路是这样的:

首先连接数据库,然后讲下来列表框赋值,然后在下拉列表框的onchenge事件写上代码:text1.value=this.option[selectedindex].value就可以实现了!

先把下面的静态页面的代码你去测试一下就知道了

=================================================

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<title>无标题文档</title>

</head>

<body>

<form action="" method="get" name="form1">

<label>

<input name="text1" type="text" id="text1" value="aa" size="10">

</label>

<label>

<select name="select" onchange="document.form1.text1.value=this.options[this.selectedIndex].value">

<option value="1">cc</option>

<option value="2">dd</opticon>

<option value="3">33</option>

</select>

</label>

</form>

</body>

</html>

================================================

<!--#include file "conn.asp"-->

<%

set rst=server.CreateObject("adodb.recordset")

sqlt="select * from hd order by id desc"

rst.open sqlt,conn,1,1

%>

<form name="form1" method="post" action="">

<label>

<input name="text1" type="text" id="text1" size="10">

</label>

<select name="select" onchange="document.form1.text1.value=this.options[this.selectedIndex].value">

<%while not rst.eof%>

<option value="<%=rst("id")%>"><%=rst("hdmc")%></option>

<% rst.movenext

wend%>

</select>

</form>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10101123.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存