我正在尝试填充一个在数据网格中重复的VB.net下拉框。基本上我有一堆图像,用户需要从下拉列表中选择项目的颜色,以便我可以将其保存在数据库中。
Dim DDLPlayColorList = New DropDownList
DDLPlayColorList = dgImages.FindControl("DDLPlayColorList")
Using oConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Using cmd As New SqlCommand("doGetAllPlayColours", oConn)
cmd.CommandType = CommandType.StoredProcedure
oConn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader()
DDLPlayColorList.DataSource = dr
DDLPlayColorList.DataTextField = "playColour"
DDLPlayColorList.DataValueField = "playColour"
DDLPlayColorList.DataBind()
End Using
End Using
End Using
我已经尝试使用findcontrol函数来查找控件然后绑定它,但它没有什么区别。错误是
DDLPlayColorList'不是'manager_imagecolouring'的成员。
这是我的DataGrid:
<asp:DataGrid ID="dgImages" runat="server" Width="300" AutoGenerateColumns="false" ItemStyle-Width="300" ItemStyle-BorderWidth="3px">
<Columns>
<asp:TemplateColumn ItemStyle-BackColor="#eaeaea" ItemStyle-Width="100">
<ItemTemplate>
<a href="image.aspx?libid=<%#Container.DataItem("id")%>">
<asp:Image ID="imgCategoryThumb" CssClass="lightbox-thumb" runat="server" Height="100" Width="100" ImageUrl='<%#Container.DataItem("path")%>' AlternateText="" />
</a>
<asp:DropDownList id="DDLPlayColorList" runat="server" CssClass="textfield">
</asp:DropDownList>
</itemtemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
答案 0 :(得分:1)
试试这个:
Dim DDLPlayColorList As DropDownList = CType(dgImages.FindControl("DDLPlayColorList"), DropDownList)
If DDLPlayColorList IsNot Nothing Then
Using oConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Using cmd As New SqlCommand("doGetAllPlayColours", oConn)
cmd.CommandType = CommandType.StoredProcedure
oConn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader()
DDLPlayColorList.DataSource = dr
DDLPlayColorList.DataTextField = "playColour"
DDLPlayColorList.DataValueField = "playColour"
DDLPlayColorList.DataBind()
End Using
End Using
End Using
End If
在数据绑定时设置多个下拉列表的ID:
private void OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Dim DDLPlayColorList As DropDownList = CType(e.Item.FindControl("DDLPlayColorList"), DropDownList)
DDLPlayColorList.ID = "DDLPlayColorList" & e.RowIndex.ToString()
}
}