1

我正在尝试填充在数据网格中重复的 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' 的成员。

这是我的数据网格:

<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>
4

1 回答 1

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()
    }
}
于 2012-04-05T12:26:33.783 回答