下面的示例说明如何通过数据绑定创建 DropDownList 控件。[Visual Basic]
<html>
<head>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim myList As New ArrayList()
myList.Add("Item 1")
myList.Add("Item 2")
myList.Add("Item 3")
myList.Add("Item 4")
myList.Add("Item 5")
DropDownList1.DataSource = myList
DropDownList1.DataBind()
End If
End Sub 'Page_Load
Sub Button_Click(sender As Object, e As EventArgs)
Label1.Text = "You selected " & DropDownList1.SelectedItem.Text & "."
End Sub 'Button_Click
</script>
</head>
<body>
<form runat="server">
<h3>DropDownList Data Bind Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="Submit"
OnClick="Button_Click"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
[C#]
<html>
<head>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
ArrayList myList = new ArrayList();
myList.Add ("Item 1");
myList.Add ("Item 2");
myList.Add ("Item 3");
myList.Add ("Item 4");
myList.Add ("Item 5");
DropDownList1.DataSource = myList;
DropDownList1.DataBind();
}
}
void Button_Click(Object sender, EventArgs e)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text + ".";
}
</script>
</head><body>
<form runat="server">
<h3>DropDownList Data Bind Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="Submit"
OnClick="Button_Click"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
[JScript]
<html>
<head>
<script language="JScript" runat="server">
function Page_Load(sender : Object, e : EventArgs)
{
if(!IsPostBack)
{
var myList : ArrayList = new ArrayList();
myList.Add ("Item 1");
myList.Add ("Item 2");
myList.Add ("Item 3");
myList.Add ("Item 4");
myList.Add ("Item 5");
DropDownList1.DataSource = myList;
DropDownList1.DataBind();
}
}
function Button_Click(sender : Object, e : EventArgs)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text + ".";
}
</script>
</head>
<body>
<form runat="server">
<h3>DropDownList Data Bind Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="Submit"
OnClick="Button_Click"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
<html>
<head>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim myList As New ArrayList()
myList.Add("Item 1")
myList.Add("Item 2")
myList.Add("Item 3")
myList.Add("Item 4")
myList.Add("Item 5")
DropDownList1.DataSource = myList
DropDownList1.DataBind()
End If
End Sub 'Page_Load
Sub Button_Click(sender As Object, e As EventArgs)
Label1.Text = "You selected " & DropDownList1.SelectedItem.Text & "."
End Sub 'Button_Click
</script>
</head>
<body>
<form runat="server">
<h3>DropDownList Data Bind Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="Submit"
OnClick="Button_Click"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
[C#]
<html>
<head>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
ArrayList myList = new ArrayList();
myList.Add ("Item 1");
myList.Add ("Item 2");
myList.Add ("Item 3");
myList.Add ("Item 4");
myList.Add ("Item 5");
DropDownList1.DataSource = myList;
DropDownList1.DataBind();
}
}
void Button_Click(Object sender, EventArgs e)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text + ".";
}
</script>
</head><body>
<form runat="server">
<h3>DropDownList Data Bind Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="Submit"
OnClick="Button_Click"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
[JScript]
<html>
<head>
<script language="JScript" runat="server">
function Page_Load(sender : Object, e : EventArgs)
{
if(!IsPostBack)
{
var myList : ArrayList = new ArrayList();
myList.Add ("Item 1");
myList.Add ("Item 2");
myList.Add ("Item 3");
myList.Add ("Item 4");
myList.Add ("Item 5");
DropDownList1.DataSource = myList;
DropDownList1.DataBind();
}
}
function Button_Click(sender : Object, e : EventArgs)
{
Label1.Text = "You selected " + DropDownList1.SelectedItem.Text + ".";
}
</script>
</head>
<body>
<form runat="server">
<h3>DropDownList Data Bind Example</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="Submit"
OnClick="Button_Click"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>
解决方案 »
- 请问一下如何获取当前页面某个webcontrol的引用
- VS2003 使用自定义的TEXT控件引起输入多个#字符只返回一个#?
- 简单的问题!
- asp.net(vb) 打印panel的内容
- objectdatasource,sqldatasource区别,请问老大
- aspx偏僻问题:如果使得aspx文件换扩展名后能得以执行?如index.aspx换成index.gif?
- 类似QQ的消息提示框,如何实现在框内消息的分页功能
- 求文件上传大小超限的解决方案
- c#怎么实现asp:button怎么实现按钮在未执行时不能重复提交,执行结束后可以继续提交
- DataGrid删除记录的源码??
- 在datagrid模板中绑定的label控件怎么解决显示文本宽度的固定
- HtmlinputRadioButton.aspx组件怎么用?
DropDownList1.DataSource = myList;//myList可以是datareader或者DataTable...
<script runat="server">
void Page_Load(Object sender,EventArgs e){
ArrayList al = new ArrayList();
al.Add( "select1" );
al.Add( "select2" );
al.Add( "select3" );
al.Add( "select4" );
DropDownList1.DataSource = al ;
DropDownList1.DataBind();
}
</script><html>
<head>
</head>
<body>
<asp:DropDownList id="DropDownList1" runat="server" />
</body>
</html>
Sub Dropdownlist_bind(ByVal ddl As DropDownList, ByVal item_name As String, ByVal table_name As String)
Dim cmdSelectWhId As SqlCommand
Dim dtrWhInfo As SqlDataReader
Dim conManInfo As SqlConnection
conManInfo = New SqlConnection("server=localhost;uid=sa;pwd=sa;database=wuliu")//连接数据库根据实际适当改动
conManInfo.Open()
cmdSelectWhId = New SqlCommand("select " + item_name + " from " + table_name, conManInfo)
dtrWhInfo = cmdSelectWhId.ExecuteReader
ddl.DataSource = dtrWhInfo
ddl.DataTextField = item_name
ddl.DataBind()
conManInfo.Close()
End Sub
将数据库(pubs)中表(Authors)的列(au_lanme)值绑定到dropdownlist上
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then Dim conpubs As SqlConnection
Dim cmdselect As SqlCommand
Dim dtrresults As SqlDataReader
Dim intfield As Integer conpubs = New SqlConnection("Server=yinfan;uid=sa;pwd=sa;database=pubs")
conpubs.Open()
cmdselect = New SqlCommand("select au_lname from Authors", conpubs)
dtrresults = cmdselect.ExecuteReader()
DropDownList1.source=dtrresults
DropDownList1.datatextField="au_lname"
DropDownList1.DataBind() dtrresults.close()
conpubs.close()
End If
End Sub
Dim NComm1 As OleDbDataAdapter = New OleDbDataAdapter(str, objconn)
objconn.Open()
Dim DS1 As DataSet = New DataSet()
NComm1.Fill(DS1, "AllTable1")
AllTables.DataSource = DS1.Tables("AllTable1").DefaultView//AllTables是一个dropdownlis的id
AllTables.DataTextField = "tablenamec"
AllTables.DataValueField = "tablenamee"
AllTables.DataBind()
objconn.Close()
AllTables.Items.Add("")
AllTables.SelectedIndex = AllTables.Items.Count - 1
...
End If