在网格视图中使用图像和下拉列表绑定复选框

时间:2014-08-14 09:23:27

标签: c# asp.net web-services gridview

我有一个gridview,其中包含图像(从数据库动态填充)和包含两个值的下拉列表。第一列包含复选框。我想在按钮单击时将所选复选框的图像和下拉值插入新表。什么是合适的方式? 这是网格视图:

<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" AllowPaging="true" 
            EmptyDataText="No images found" OnPageIndexChanging="gvDetails_PageIndexChanging" PageSize="5">
            <Columns>

                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckUncheckAll"/>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID ="CheckBox2" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Image">                   
                    <ItemTemplate>
                        <asp:Image ID="imgPreview" ImageUrl='<%#
                        "ImageHandler.ashx?imgID="+ Eval("ID") %>' runat="server"
                            Height="80px" Width="80px" />
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Dropdown" ItemStyle-Width="50px">
                    <ItemTemplate>
                        <asp:DropDownList ID="dpdListEstatus" runat="server" OnSelectedIndexChanged="dpdListEstatus_SelectedIndexChanged" AutoPostBack="true">
                            <asp:ListItem>A</asp:ListItem>
                            <asp:ListItem>B</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

1 个答案:

答案 0 :(得分:1)

使用gridview行循环

查找复选框控件

检查其Checked属性是否为真

如果是,请调用insert语句

设置您想要从图像和下拉列表中获取的值。当然你也需要在它上面使用findcontrol。

Dim cbSelect As CheckBox, imgToInsert As Image, ddlStatus As DropDownList
For Each r As GridViewRow In gvDetails.Rows
    cbSelect = r.Cells(0).FindControl("CheckBox2")

    If cbSelect.Checked Then
        imgToInsert = r.Cells(1).FindControl("imgPreview")
        ddlStatus = r.Cells(2).FindControl("dpdListEstatus")

        'Insert statement goes here...
    End If
Next r