Checkboxes in Gridview using IE

2019-08-12 06:35发布

问题:

I'm running into a strange issue. I'm trying to see what checkboxes are selected within the gridview. My code looks like it follows the examples I found on the internet, and works in FF & Chrome, but fails in IE9. In IE9, the checked property never returns true. I've stepped through the code, and have verified that it's looking at the correct checkbox, but IE will always return false.

Does anyone have any ideas? Below is my markup & codebehind.

    <asp:GridView ID="gvParts" runat="server" 
      AllowSorting="True" AutoGenerateColumns="False"
      CellPadding="5" DataKeyNames="Rec_ID" DataSourceID="dsParts" 
      PageSize="50" Width="100%">
        <Columns>
            <asp:TemplateField HeaderText="Select">
                <HeaderTemplate>
                    Select
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server"/>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" Width="75px" />
            </asp:TemplateField>
            <asp:BoundField DataField="Arcft_Make" HeaderText="Make" 
                SortExpression="Arcft_Make" >
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Arcft_Model" HeaderText="Model" 
                SortExpression="Arcft_Model" >
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Source_Name" 
                            HeaderText="Source_Name" 
                            SortExpression="Source_Name" 
                            DataFormatString="{0:d}" 
                            HtmlEncode="false">
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Part_Number" 
                            HeaderText="Part Number" 
                            SortExpression="Part_Number" >
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Vendor_Part_Number" 
                            HeaderText="Vendor Number" 
                            SortExpression="Vendor_Part_Number" >
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Vendor_Name" 
                            HeaderText="Vendor Name" 
                            SortExpression="Vendor_Name" >
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Descr" 
                            HeaderText="Description" 
                            SortExpression="Descr">
                <HeaderStyle HorizontalAlign="Left" />
                <ItemStyle Width="300px" HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Date_Added" 
                            HeaderText="Date_Added" 
                            SortExpression="Date_Added">
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Rec_ID" 
                            ReadOnly="True" Visible="False">
                <ItemStyle Width="0px" />
            </asp:BoundField>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
        <SelectedRowStyle BackColor="#FFFFCC" />
        <AlternatingRowStyle BackColor="#CCFFFF" />
    </asp:GridView>

`

And

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
                         Handles btnAdd.Click
    Dim Rec_IDs As New List(Of String)
    Dim Rec_ID As Int32
    Rec_IDs = Session("Rec_IDs")
    For Each Row As GridViewRow In gvParts.Rows
        If CType(Row.FindControl("chkSelect"), CheckBox).Checked Then
            Rec_ID = gvParts.DataKeys(Row.RowIndex).Value
            If Not Rec_IDs.Contains(Rec_ID) Then
                Rec_IDs.Add(Rec_ID)
            End If
            CType(Row.FindControl("chkSelect"), CheckBox).Checked = False
        End If
    Next
    Session("Rec_IDs") = Rec_IDs
    lblCount.Text = String.Format("You have {0} records selected",   
                                   Rec_IDs.Count.ToString)
End Sub

回答1:

OK, solved. My gridview wasn't inside my form tags. I guess IE didn't like that.