1
我有一個我經常使用的dropdownlist。 我寫了一個方法來加載這個下拉菜單。 雖然當我調用該方法時,只傳遞了該項目的文本部分。 這是方法從函數或方法加載的Dropdownlist
Public Shared Function ddlLoadResumes() As DropDownList
Dim connString As String = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
Dim conn As SqlConnection = New SqlConnection(connString)
Dim ddSelectResumeList As New DropDownList
Dim dtResumes As New DataTable()
Dim comm As SqlCommand = New SqlCommand("usr_SelectResumeList", conn)
comm.CommandType = CommandType.StoredProcedure
comm.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = Membership.GetUser().UserName
Try
Dim daResumes As New SqlDataAdapter(comm)
conn.Open()
daResumes.Fill(dtResumes)
ddSelectResumeList.DataSource = dtResumes
ddSelectResumeList.DataTextField = "ResumeName"
ddSelectResumeList.DataValueField = "Res_ID"
ddSelectResumeList.DataBind()
' Handle the error
Catch ex As Exception
Finally
conn.Close()
End Try
Return ddSelectResumeList
End Function
,這是我怎麼稱呼它
ddSelectResume.DataSource = MailClass.ddlLoadResumes.Items
ddSelectResume.DataBind()
Dim ltitem As ListItem
For Each ltitem In ddSelectResume.Items
Response.Write(ltitem.Value + "-----" + ltitem.Text)
Next returns only the text part
我同時需要DataTextField和DataValueField。 謝謝你提前
你是什麼意思,它不起作用,@Tzvei?也許你可以嘗試加載Items集合? 'ddSelectResume.Items = MailClass.ddlLoadResumes.Items'。 – jadarnel27
我的意思是ddSelectResume在bind()後是空的。 ddSelectResume需要一個數據源 – Tzvei
@Tzvei你爲什麼要調用'Bind()'?它已經綁定在'ddlLoadResumes'中。 – jadarnel27