将所选行从Gridview导出到Excel时出错

时间:2014-06-15 12:51:37

标签: asp.net vb.net excel gridview

我正在尝试将选定的列从GridView导出到Excel,每次运行代码时都会弹出一个新的.dll文件名错误。可能是什么问题?请找到以下代码供您参考。

<asp:Panel ID="general" runat="server" BorderColor="#333333" BorderStyle="Double" CssClass="emailpnl" Width="800px">
    <asp:GridView ID="bestpractgrv" runat="server" AutoGenerateColumns="False" OnLoad="bestpractgrv_Load" CellPadding="3" GridLines="Vertical" Width="100%" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" AllowPaging="True" OnPageIndexChanging = "OnPaging">
        <AlternatingRowStyle BackColor="Gainsboro" />
    <Columns>
       <asp:TemplateField>
           <HeaderTemplate>
                    <asp:CheckBox ID="chkboxSelectAll" runat="server" onclick="CheckAllEmp(this);" />
           </HeaderTemplate>
           <ItemTemplate>
               <asp:CheckBox ID ="boolcb" runat="server" />
           </ItemTemplate>
       </asp:TemplateField>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="idlbl" runat="server" Text='<%# Bind("iID")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Discipline ID">
            <ItemTemplate>
                <asp:Label ID="discplnlbl" runat="server" Text='<%# Bind("iDisciplineID")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date Created">
            <ItemTemplate>
                <asp:Label ID="dateclbl" runat="server" Text='<%# Bind("dDateCreated")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
                <asp:Label ID="bptitlelbl" runat="server" Text='<%# Bind("cBPTitle")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
    </asp:Panel>

VB代码

Private Sub PopulateCheckBoxArray()

        Dim CheckBoxArray As ArrayList

        If ViewState("CheckBoxArray") IsNot Nothing Then
            CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)
        Else
            CheckBoxArray = New ArrayList()
        End If


        Dim CheckBoxIndex As Integer
        Dim CheckAllWasChecked As Boolean = False
        Dim chkAll As CheckBox = DirectCast(bestpractgrv.HeaderRow.Cells(0).FindControl("chkboxSelectAll"), CheckBox)
        Dim checkAllIndex As String = "chkboxSelectAll-" & bestpractgrv.PageIndex

        If chkAll.Checked Then

            If CheckBoxArray.IndexOf(checkAllIndex) = -1 Then
                CheckBoxArray.Add(checkAllIndex)
            End If

        Else
            If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
                CheckBoxArray.Remove(checkAllIndex)
                CheckAllWasChecked = True

            End If

        End If

        For i As Integer = 0 To bestpractgrv.Rows.Count - 1

            If bestpractgrv.Rows(i).RowType = DataControlRowType.DataRow Then

                Dim chk As CheckBox = DirectCast(bestpractgrv.Rows(i).Cells(0).FindControl("boolcb"), CheckBox)

                CheckBoxIndex = bestpractgrv.PageSize * bestpractgrv.PageIndex + (i + 1)

                If chk.Checked Then

                    If CheckBoxArray.IndexOf(CheckBoxIndex) = -1 AndAlso Not CheckAllWasChecked Then

                        CheckBoxArray.Add(CheckBoxIndex)

                    End If

                Else

                    If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 OrElse CheckAllWasChecked Then

                        CheckBoxArray.Remove(CheckBoxIndex)

                    End If

                End If

            End If

        Next

        ViewState("CheckBoxArray") = CheckBoxArray

    End Sub

我在下一行

上收到错误
Dim chkAll As CheckBox = DirectCast(bestpractgrv.HeaderRow.Cells(0).FindControl("chkboxSelectAll"), CheckBox)

这是错误消息

  

类型&#39; System.NullReferenceException&#39;的例外情况发生在App_Web_pqx0e3wy.dll中,但未在用户代码中处理

每次编译后,dll名称都会更改,而 App_Web_XXXX.dll 保持不变

0 个答案:

没有答案