用选定的值填充列表框 [英] Populate listbox with selected values
问题描述
我有一个用于Web应用程序的表单,该表单使用列表框控件选择类别.我没有问题,可以使用表中的数据填充列表框中的值并将其提取以保存.问题是当我去编辑时,找不到找到将相关记录标记为已选择的方法.
下面的第一个子项是填充表单中列表框的子项.第二个子项尝试从表中加载值,该表将主表中的记录与类别表中的记录相关联.
第二个子选项仅将阅读器中的最后一条记录标记为选中.我已经广泛搜索了应该是一个简单任务的解决方案,却一无所获.我可以用内联代码蒙住眼睛.这很令人沮丧.
I have a form for a web application that selects categories using a listbox control. I have no problem populating the values in the listbox with data from a table and extracting that to save. The problem is when I go to edit, I cannot find a way to mark the associated records as selected.
The first sub below is the sub that populates the listbox in the form. The second sub attempts to load values from a table that associates records from a primary table to records in the categories table.
The second sub only marks the last record in the reader as selected. I have searched extensively for a solution to what should be a simple task and have found nothing. I could do this blindfolded with inline code; this is frustrating.
Protected Sub LoadCat()
Dim myCommand As New SqlCommand
Dim myReader As SqlDataReader = Nothing
Dim strConnection As String
strConnection = ConfigurationManager.AppSettings("ConnectionString")
Dim sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath
Dim oInfo = New System.IO.FileInfo(sPath)
Dim sRet = oInfo.Name
Try
myConnection = New SqlConnection(strConnection)
myConnection.Open()
'opening the connection
With myCommand
.CommandType = CommandType.StoredProcedure
.CommandText = "MCC_LoadRecCat"
.Connection = myConnection
myReader = .ExecuteReader()
End With
Catch ex As Exception
Dim EmailClass As New EmailClass
EmailClass.EmailError(ex.Message.ToString, sRet, Session("ErrorEmail"), Session("EmailUser"), Session("EmailPassword"), Session("MailServer"))
Response.Redirect("Error.aspx")
Exit Sub
End Try
reccatID.DataSource = myReader
reccatID.DataTextField = "reccatName"
reccatID.DataValueField = "reccatID"
reccatID.DataBind()
myReader.Close()
myCommand.Dispose()
myConnection.Close()
End Sub
Protected Sub PopulateCat()
Dim myCommand As New SqlCommand
Dim myReader As SqlDataReader = Nothing
Dim strConnection As String
strConnection = ConfigurationManager.AppSettings("ConnectionString")
Dim sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath
Dim oInfo = New System.IO.FileInfo(sPath)
Dim sRet = oInfo.Name
Dim MyID = Request("ID")
ViewState("recipeID") = Request("ID")
Try
myConnection = New SqlConnection(strConnection)
myConnection.Open()
'opening the connection
With myCommand
.CommandType = CommandType.StoredProcedure
.CommandText = "MCC_GetRecipeCat2"
.Parameters.Add("@recipeID", SqlDbType.Int).Value = MyID
.Connection = myConnection
myReader = .ExecuteReader()
End With
Catch
End Try
While myReader.Read
reccatID.SelectedValue = myReader("catID")
End While
myReader.Close()
myCommand.Dispose()
myConnection.Close()
End Sub
推荐答案
您要一遍又一遍地设置选定的值.因此,每次都取消选择最后一个.我相信这里有一个SelectedValues属性,您可以添加一个集合.如果没有,则不能选择多个项目,
You''re setting the selectedvalue over and over. Thus, each time, you unselect the last one selected. I believe there''s a SelectedValues property, a collection you can add to. If there''s not, then you can''t select more than one item,
这篇关于用选定的值填充列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!