Gridview中的CheckedChanged事件复选框以更新隐藏的标签

时间:2015-11-22 05:48:46

标签: asp.net vb.net loops gridview

我有一个gridview,其中第一列是复选框,下一列是ID。我在页面上隐藏了需要获取所选复选框的当前ID的标签。标签是特定的,hlbl_pain1,hlbl_pain2,hlbl_pain3。不知道哪个标签获取哪个ID我只需要用ID填充3个隐藏标签中的每一个。就在CheckedChanged上,3个标签都以相同的值更新。我有问题循环。这是在VB。

这是我的网格:

<asp:GridView ID="gv_pain" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="ds_grid_pain"  ForeColor="Black" GridLines="Vertical" style="text-align: left; font-size: x-small; ">
                        <AlternatingRowStyle BackColor="#CCCCCC" />
                        <Columns>
                            <asp:TemplateField HeaderText="Select">
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBoxPain" runat="server" AutoPostBack="True" Checked="false" OnCheckedChanged="CheckBoxPain_CheckedChanged"  />  
                                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("DrugID")%>' />
                                    </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="DrugID" InsertVisible="False" SortExpression="DrugID">
                                <EditItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("DrugID") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_DrugID" runat="server" Text='<%# Bind("DrugID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="NickName" HeaderText="NickName" SortExpression="NickName" ItemStyle-Width="150px">
                            <ItemStyle Wrap="False" />
                            </asp:BoundField>
                            <asp:BoundField DataField="DrugName" HeaderText="DrugName" SortExpression="DrugName" ItemStyle-Width="500px">
                            <ItemStyle Wrap="False" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Quan1" HeaderText="Quan1" SortExpression="Quan1">
                            <ItemStyle Wrap="False" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Quan2" HeaderText="Quan2" SortExpression="Quan2">
                            <ItemStyle Wrap="False" />
                            </asp:BoundField>
                        </Columns>
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>

以下是我的标签:

 <asp:Label ID="hlbl_pain1" runat="server"  Visible="true"></asp:Label>
                                <asp:Label ID="hlbl_pain2" runat="server"  Visible="true"></asp:Label>
                                <asp:Label ID="hlbl_pain3" runat="server"  Visible="true"></asp:Label>

以下是我在代码隐藏中的CheckedChanged中要做的事情:

Protected Sub CheckBoxPain_CheckedChanged(sender As Object, e As System.EventArgs)

    Dim row As GridViewRow

For Each row In gv_pain.Rows

    Dim cbPain As CheckBox = row.FindControl("CheckBoxPain")
    If cbPain.Checked = True Then
        Dim lblDrugID As Label = row.FindControl("lbl_DrugID")
        hlbl_pain1.Text = lblDrugID.text
    End If


    Dim cbPain1 As CheckBox = row.FindControl("CheckBoxPain")
    If cbPain1.Checked = True Then
        Dim lblDrugID As Label = row.FindControl("lbl_DrugID")
        hlbl_pain2.Text = lblDrugID.text
    End If


    Dim cbPain2 As CheckBox = row.FindControl("CheckBoxPain")
    If cbPain2.Checked = True Then
        Dim lblDrugID As Label = row.FindControl("lbl_DrugID")
        hlbl_pain3.Text = lblDrugID.text
    End If

    Next

End Sub

0 个答案:

没有答案