将GridView导出到txt文件时数据丢失

时间:2020-06-08 17:45:05

标签: c# html asp.net gridview

我要导出3列。来自Gridview的AppID,信用文件和创建日期,但是当我打开文件时,唯一看到的是AppID和创建日期。贷方文件数据丢失。我尝试仅导出信用文件本身,但只显示了标头,但再次没有文本。

这是我的html

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CrifCredit.aspx.cs" Inherits="crifaccess.CrifCredit" %>

<!DOCTYPE html>

<html lang="en">
    <head>
        <link href="CSS/Stylesheet1.css" rel="stylesheet" />

    <title>CRIF Credit File</title>
        <meta charset="utf-8" />
        <style type="text/css">
            .auto-style1{
                margin-left:10px;
                margin-top:0px;
            }

        </style>
</head>
<body>
    <nav>
        <div class="w3-sidebar w3-bar-block w3-card w3-animate left" style="display:none" id="mySidebar">
            <button class="close_button"
                onclick="w3_close()">Close</button><br />
                 <a href="Default.aspx" class="w3-bar-item w3-button">Home</a><br />
                <a href="Application.aspx" class="w3-bar-item w3-button">Application</a><br />
                <a href="Vehicle.aspx" class="w3-bar-item w3-button">Vehicle</a><br />
                <a href="AddOn.aspx" class="w3-bar-item w3-button">NADA</a><br />
                <a href="Visa.aspx" class="w3-bar-item w3-button">Visa</a><br />
                <a href="AppNotes.aspx" class="w3-bar-item w3-button">CRF Notes</a><br />
                <a href="CritNotes.aspx" class="w3-bar-item w3-button">CRI Notes</a><br />
                <a href="CritApp.aspx" class="w3-bar-item w3-button">CRI Loan</a><br />
                <a href="CrifCredit.aspx" class="w3-bar-item w3-button">CRF Credit</a><br />
        </div>
         <div id="main">
            <div class="w3-teal">
                <button id="openNav" class="w3-button w3-teal w3-xlarge" onclick="w3_open()">Navigation</button>
                <div class="w3-container">

                </div>
            </div>
             </div>
    </nav>
    <form id="form1" runat="server">
        <div id="wrapper">
            <div id="rightcol">
                <header>
                    <img src="Images/GCSCU_Color.gif" height="90" width="120" align="left" class="logo" /><h1>CRIF/Criterion Access</h1>
                </header>
                <main>
                    <h5>
                        Please enter the SSN associated with the CRIF Application.
                    </h5>
                </main>
                <div class="ssninput">
                    <asp:TextBox ID="SearchSSNCredit" runat="server" BorderColor="#0033CC" MaxLength="9" CssClass="auto-style1"></asp:TextBox>
                    <asp:Button ID="ClickSearchCredit" runat="server" Text="Search for Application Number" style="margin-left:16px; margin-bottom:0px" />
                    <asp:Button ID="ClearSSNCredit" runat="server" Text="Clear" style="margin-left:10px; margin-bottom:0px" OnClick="btnClear_Click20" />
                    <br />
                    <br />

                    <div class="table">
                        <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Horizontal">
                            <AlternatingRowStyle BackColor="#F7F7F7" />
                            <Columns>
                                <asp:BoundField DataField="Creation_Date" HeaderText="Creation_Date" ReadOnly="True" SortExpression="Creation_Date" Visible="False" />
                                <asp:BoundField DataField="DecisionID" HeaderText="DecisionID" ReadOnly="True" SortExpression="DecisionID" Visible="False" />
                                <asp:BoundField DataField="Decision_Name" HeaderText="Decision_Name" ReadOnly="True" SortExpression="Decision_Name" Visible="False" />
                                <asp:BoundField DataField="AppID" HeaderText="AppID" InsertVisible="False" ReadOnly="True" SortExpression="AppID" />
                                <asp:BoundField DataField="Applicant_FullName" HeaderText="Applicant_FullName" ReadOnly="True" SortExpression="Applicant_FullName" Visible="False" />
                                <asp:BoundField DataField="SSN" HeaderText="SSN" ReadOnly="True" SortExpression="SSN" />
                                <asp:BoundField DataField="Account_Number" HeaderText="Account_Number" ReadOnly="True" SortExpression="Account_Number" />
                                <asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" Visible="False" />
                                <asp:BoundField DataField="Product_Type" HeaderText="Product_Type" SortExpression="Product Type" />
                                <asp:BoundField DataField="DealerID" HeaderText="DealerID" SortExpression="DealerID" Visible="False" />
                                <asp:BoundField DataField="Dealer_Name" HeaderText="Dealer_Name" SortExpression="Dealer_Name" Visible="False" />
                                <asp:BoundField DataField="Credit_Score" HeaderText="Credit_Score" ReadOnly="True" SortExpression="Credit_Score" Visible="False" />
                                <asp:BoundField DataField="Credit_Grade" HeaderText="Credit_Grade" SortExpression="Credit_Grade" Visible="False" />
                                <asp:BoundField DataField="DTI" HeaderText="DTI" ReadOnly="True" SortExpression="DTI" Visible="False" />
                                <asp:BoundField DataField="Loan_Amount" HeaderText="Loan Amount" ReadOnly="True" SortExpression="Loan_Amount" />
                                <asp:BoundField DataField="Principal_Amount" HeaderText="Principal_Amount" SortExpression="Principal_Amount" Visible="False" />
                                <asp:BoundField DataField="LTV" HeaderText="LTV" ReadOnly="True" SortExpression="LTV" Visible="False" />
                                <asp:BoundField DataField="Date_Funded" DataFormatString="{0:d}" HeaderText="Date Funded" ReadOnly="True" SortExpression="Date_Funded" />
                                <asp:BoundField DataField="Financed_Amount" HeaderText="Financed_Amount" SortExpression="Financed_Amount" Visible="False" />
                            </Columns>
                            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                            <SortedAscendingCellStyle BackColor="#F4F4FD" />
                            <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
                            <SortedDescendingCellStyle BackColor="#D8D8F0" />
                            <SortedDescendingHeaderStyle BackColor="#3E3277" />
                        </asp:GridView>
                        <br />
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GraniteCitySteelCUConnectionString %>" SelectCommand="GetAppIDCreditFile" SelectCommandType="StoredProcedure">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="SearchSSNCredit" Name="SSN" PropertyName="Text" Type="String" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </div>
                    <br />
                    <br />
                    <div class="ssninput">

                        <asp:TextBox ID="SearchAppCredit" runat="server" BorderColor="#0033CC" MaxLength="15" CssClass="auto-style1" Width="140px"></asp:TextBox>
                        <asp:Button ID="ClickSearchCredit2" runat="server" Text="Search for Application Information" style="margin-left16px; margin-bottom:0px" />
                        <asp:Button ID="ClearSearchCredit2" runat="server" Text="Clear" style="margin-left:10px; margin-bottom:0px" OnClick="btnClear_Click21" />
                        <br />
                        <br />
                        <div class="table">
                            <asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="AppID,ServiceRequestID,ServiceRequestCreditFileID" DataSourceID="SqlDataSource2" GridLines="Horizontal">
                                <AlternatingRowStyle BackColor="#F7F7F7" />
                                <Columns>
                                    <asp:BoundField DataField="AppID" HeaderText="AppID" ReadOnly="True" SortExpression="AppID" />



                                    <asp:TemplateField HeaderText="Credit File" SortExpression="CreditFile">

                                        <ItemTemplate>
                                            <div style="overflow:hidden; text-overflow:ellipsis; white-space:nowrap; width:1000px">


                                            <asp:Label ID="credit" runat="server" Text='<%# Bind("CreditFile") %>'></asp:Label>

                                            </div>
                                        </ItemTemplate>
                                        <HeaderStyle Wrap="false" Width="100" HorizontalAlign="Left" />
                                        <ItemStyle Wrap="false" Width="100" /><ItemStyle />

                                    </asp:TemplateField>
                                    <asp:BoundField DataField="CreationDate" DataFormatString="{0:d}" HeaderText="Creation Date" SortExpression="CreationDate" />


                                </Columns>
                                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                                <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                                <SortedAscendingCellStyle BackColor="#F4F4FD" />
                                <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
                                <SortedDescendingCellStyle BackColor="#D8D8F0" />
                                <SortedDescendingHeaderStyle BackColor="#3E3277" />
                            </asp:GridView>
                            <br />
                            <asp:Button ID="ExportTxt" Text="Export Credit File to Notepad" runat="server" OnClick="ExportTextFile" />
                            <br />
                            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GraniteCitySteelCUConnectionString %>" SelectCommand="GetCreditFile" SelectCommandType="StoredProcedure">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="SearchAppCredit" Name="AppID" PropertyName="Text" Type="String" />
                                </SelectParameters>
                            </asp:SqlDataSource>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <script>
            function w3_open() {
                document.getElementById("main").style.marginLeft = "5%";
                document.getElementById("mySidebar").style.display = "inline-block";
                document.getElementById("openNav").style.display = "none";
            }

            function w3_close() {

                document.getElementById("main").style.marginLeft = "0%";
                document.getElementById("mySidebar").style.display = "none";
                document.getElementById("openNav").style.display = "inline-block";
            }
        </script>
        <br />
    </form>
</body>
</html>

这是我的C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace crifaccess
{
    public partial class CrifCredit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();


                GridView4.DataBind();
            }
        }

        protected void ExportTextFile (object sender, EventArgs e)
        {
            //build the text file data
            string txt = string.Empty;

            foreach (TableCell cell in GridView4.HeaderRow.Cells)
            {
                //add the header row for text file
                txt += cell.Text + "\t\t";
            }

            //add new line
            txt += "\r\n";

            foreach (GridViewRow row in GridView4.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    //add the data rows
                    txt += cell.Text + "\t\t";
                }

                //add new line
                txt += "\r\n";
            }

            //download the text file
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=creditfile.txt");
            Response.Charset = "";
            Response.ContentType = "application/text";
            Response.Output.Write(txt);
            Response.Flush();
            Response.End();

        }

        protected void btnClear_Click20 (object sender, EventArgs e)
        {
            SearchSSNCredit.Text = "";
        }

        protected void btnClear_Click21 (object sender, EventArgs e)
        {
            SearchAppCredit.Text = "";
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblCredit = (Label)e.Row.FindControl("credit");
                string tooltip = lblCredit.Text;
                e.Row.Cells[4].Attributes.Add("title", tooltip);
            }
        }



    }
}

1 个答案:

答案 0 :(得分:0)

我能够通过删除ItemTemplate字段来解决此问题