读书人

DropDownList1有关问题

发布时间: 2012-04-04 16:38:51 作者: rapoo

DropDownList1问题


DropDownList1.DataValueField = uns.Tables(0).Columns(1).ToString
DropDownList1.DataTextField = uns.Tables(0).Columns(1).ToString
DropDownList1.DataBind()

uns是一个dataset
为什么 DropDownList1.SelectedValue.Trim
取到的都三第一列的数

[解决办法]
示例
[Visual Basic, C#, JScript] 下面的示例说明如何使用 DataValueField 属性指定 DataSource 中的字段,该字段将绑定到 HtmlSelect 控件中各项的 ListItem.Value 属性。
[Visual Basic]
<%@ Page Language= "VB " AutoEventWireup= "True " %>
<%@ Import Namespace= "System.Data " %>
<%@ Import Namespace= "System.Data.SqlClient " %>

<html>

<head>

<script runat= "server ">

Sub Page_Load (sender As Object, e As EventArgs)

' Bind the HtmlSelect control to a data source when the page is initially loaded.
If Not IsPostBack Then

' Open a connection to the database and run the query.
' Note that the connection string may vary depending on your
' database server settings.
Dim ConnectString As String = "server=localhost;database=pubs;integrated security=SSPI "
Dim QueryString As String = "select * from authors "

Dim myConnection As SqlConnection = New SqlConnection(ConnectString)
Dim myCommand As SqlDataAdapter = New SqlDataAdapter(QueryString, myConnection)

' Create a dataset to store the query results.
Dim ds As DataSet = New DataSet()
myCommand.Fill(ds, "Authors ")

' Bind the HtmlSelect control to the data source.
Select1.DataSource = ds
Select1.DataTextField = "au_fname "
Select1.DataValueField = "au_fname "
Select1.DataBind()

End If

End Sub

Sub Button_Click (sender As Object, e As EventArgs)

Dim i As Integer

Label1.Text = "You selected: "

For i = 0 To Select1.Items.Count - 1

If Select1.Items(i).Selected Then
Label1.Text = Label1.Text & " <br>    - " & Select1.Items(i).Text
End If

Next i

End Sub

</script>

</head>

<body>

<form runat= "server ">

<h3> HtmlSelect Example </h3>

Select items from the list. <br>
Use the Control or Shift key to select multiple items. <br> <br>

<select id= "Select1 "
Multiple= "True "
runat= "server "/>

<br> <br>

<button id= "Button1 "
OnServerClick= "Button_Click "
runat= "server ">

Submit

</button>

<br> <br>

<asp:Label id= "Label1 "
runat= "server "/>

</form>

</body>

</html>

读书人网 >VB Dotnet

热点推荐