0

我正在使用 Ajax TabContiner,并且在其中一个选项卡中我有一个 ListView。此 ListView 显示数据库中特定表内的所有项目。使用该 ListView,有一种方法可以为 ListView 中的选定项目创建 Word 文档。项目的选择将通过 ListView 第一列中提供的 CheckBox。一切正常。

现在,我想计算该表中的项目数。我写了这个方法,它工作得很好,但它使创建word文档的方法现在工作得很好。例如,在那个特定的选项卡中,我想在其中显示选项卡中项目的数量。如果我想在 Word 文档中打印一些选定的项目。Word 文档将打开,但没有任何数据。我不知道为什么。如果我删除了计数项目方法,它工作得很好。谁能帮我解决这个问题?

ASP.NET 代码:

    <asp:TabContainer CssClass="ajax__tab_darkblue-theme" ID="TabContainer1" 
                runat="server" ActiveTabIndex="0" 
                             Width="90%" OnInit="counter">


            <asp:TabPanel ID="All_SafetySuggestions_Tab" HeaderText="All" runat="server">
            <ContentTemplate>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1">
                            <LayoutTemplate>
                                <div ><table id="thetable" width="97%" cellpadding="0px" cellspacing="0px" style="margin:0px 0px 0px 0px; border:2px solid #003366; font-size:13px; font-weight:bold;">
                                    <thead>
                                        <tr style="background-color:#C6D7B5;">
                                            <%--<th style="border-right:2px solid white;border-bottom:2px solid #003366; ">LL No.</th>--%>
                                            <th style="border-bottom:2px solid #003366; ">
                                                <asp:CheckBox ID="CheckBox1" runat="server" CssClass="chkBoxPosition" OnCheckedChanged="CheckBoxHeader_All" AutoPostBack="true" />
                                            </th>    
                                            <th style="border-bottom:2px solid #003366; ">Title</th>
                                            <th style="border-bottom:2px solid #003366; ">Description</th>
                                            <th style="border-bottom:2px solid #003366; ">Type</th>
                                            <th style="border-bottom:2px solid #003366; ">Username</th>
                                            <th style="border-bottom:2px solid #003366; ">Name</th>
                                            <th style="border-bottom:2px solid #003366; ">Division</th>
                                            <th style="border-bottom:2px solid #003366; ">Submitted Date</th>
                                            <th style="border-bottom:2px solid #003366; ">Status</th>
                                        </tr>
                                    </thead>
                                    <tbody><tr id="itemPlaceholder" runat="server"></tr></tbody>
                                </table></div>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <asp:CheckBox ID="CheckBox2" runat="server" />
                                        </td>
                                    <td>
                                        <asp:Label runat="server" ID="lblTitle" Text='<%#Eval("Title") %>'></asp:Label>
                                        </td>
                                    <td>
                                        <asp:Label runat="server" ID="lblDescription" Text='<%#Eval("Description")%>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label runat="server" ID="lblType" Text='<%#Eval("Type")%>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label runat="server" ID="lblUsername" Text='<%#Eval("Username") %>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label runat="server" ID="lblName" Text='<%#Eval("Name") %>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label runat="server" ID="lblDivision" Text='<%#Eval("DivisionShortcut") %>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:Label runat="server" ID="lblSubmittedDate" Text='<%#Eval("DateSubmitted")%>'></asp:Label>
                                    </td>
                                    <td>
                                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
                                                                OnClick="lnkSuggestionStatus_Click">
                                        </asp:LinkButton>
                                    </td>
                                </tr>
                            </ItemTemplate>                
                    </asp:ListView>
    ...
</asp:TabPanel>
        </asp:TabContainer>

        <asp:Label ID="Label1" runat="server" Text="Lessons as Table"></asp:Label>
        <asp:ImageButton ID="wordBtn" runat="server" ImageUrl="images/Icons/MS_Word_Icon.png" Width="30px" Height="30px" OnClick="creat_word_table"></asp:ImageButton>

代码隐藏:

public void counter(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
        string[] commands = {
                                "SELECT COUNT(*) FROM SafetySuggestionsLog"
                            };
        int[] SSCount = new int[commands.Length];
        try
        {
            for (int i = 0; i < commands.Length; i++)
            {
                SqlCommand cmd = new SqlCommand(commands[i], conn);
                conn.Open();
                SSCount[i] = (int)cmd.ExecuteScalar();
                conn.Close();
            }
            All_SafetySuggestions_Tab.HeaderText += " (" + SSCount[0] + ")";
        }
        catch (Exception ex) { string ee = ex.Message; }
    }


    public void creat_word_table(object sender, EventArgs e)
    {
        ListView lv = new ListView();

        if (TabContainer1.ActiveTab.HeaderText == "Last Three Months") { lv = Last_Three_Months_ListView; }
        else if (TabContainer1.ActiveTab.HeaderText == "All") { lv = ListView1; }
        //lv.Items.Clear();

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/msword";
        string tab_name = TabContainer1.ActiveTab.HeaderText;
        string strFileName = tab_name + ".doc";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        StringBuilder strHTMLContent = new StringBuilder();

        strHTMLContent.Append(" <h1 title='Heading' align='Center' style='font-family:verdana;font-size:80%;color:black'><u>" + tab_name + "</u> </h1>".ToString());
        strHTMLContent.Append("<br>".ToString());
        strHTMLContent.Append("<table cellspacing='0' width='100%' style='border:2px solid #003366; font-size:17px; font-weight:bold;'>".ToString());
        strHTMLContent.Append("<tr>".ToString());
        strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Title</th>".ToString());
        strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Description</th>".ToString());
        strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Type</th>".ToString());
        strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Username</th>".ToString());
        strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Name</th>".ToString());
        strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Division</th>".ToString());
        strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Submitted Date</th>".ToString());
        strHTMLContent.Append("</tr>".ToString());

        for (int i = 0; i < lv.Items.Count; i++)
        {
            CheckBox chk = lv.Items[i].FindControl("CheckBox2") as CheckBox;
            if (chk.Checked)
            {
                strHTMLContent.Append("<tr>".ToString());
                strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblTitle")).Text + "</td>".ToString());
                strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblDescription")).Text + "</td>".ToString());
                strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblType")).Text + "</td>".ToString());
                strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblUsername")).Text + "</td>".ToString());
                strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblName")).Text + "</td>".ToString());
                strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblDivision")).Text + "</td>".ToString());
                strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblSubmittedDate")).Text + "</td>".ToString());
                strHTMLContent.Append("</tr>".ToString());
            }



        }

        strHTMLContent.Append("</table>".ToString());
        strHTMLContent.Append("<br><br>".ToString());

        HttpContext.Current.Response.Write(strHTMLContent);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();
    }
4

1 回答 1

0

根据您的评论中的要求,这里是您的示例的修改,可以规避由更改标题文本引起的任何问题(这是我对问题的猜测)。

public void counter(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
    string[] commands = {
                            "SELECT COUNT(*) FROM SafetySuggestionsLog"
                        };
    int[] SSCount = new int[commands.Length];
    try
    {
        for (int i = 0; i < commands.Length; i++)
        {
            SqlCommand cmd = new SqlCommand(commands[i], conn);
            conn.Open();
            SSCount[i] = (int)cmd.ExecuteScalar();
            conn.Close();
        }
        All_SafetySuggestions_Tab.HeaderText += " (" + SSCount[0] + ")";
    }
    catch (Exception ex) { string ee = ex.Message; }
}


public void creat_word_table(object sender, EventArgs e)
{
    ListView lv = new ListView();
    // THIS IS WHERE I'VE MADE A CHANGE.  INSTEAD OF == USE STARTSWITH TO ACCOUNT FOR
    // YOUR COUNT METHOD APPENDING HEADER NAMES WITH A COUNT.
    if (TabContainer1.ActiveTab.HeaderText.StartsWith("Last Three Months")) { lv = Last_Three_Months_ListView; }
    else if (TabContainer1.ActiveTab.HeaderText.StartsWith("All")) { lv = ListView1; }
    //lv.Items.Clear();

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentType = "application/msword";
    string tab_name = TabContainer1.ActiveTab.HeaderText;
    string strFileName = tab_name + ".doc";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
    StringBuilder strHTMLContent = new StringBuilder();

    strHTMLContent.Append(" <h1 title='Heading' align='Center' style='font-family:verdana;font-size:80%;color:black'><u>" + tab_name + "</u> </h1>".ToString());
    strHTMLContent.Append("<br>".ToString());
    strHTMLContent.Append("<table cellspacing='0' width='100%' style='border:2px solid #003366; font-size:17px; font-weight:bold;'>".ToString());
    strHTMLContent.Append("<tr>".ToString());
    strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Title</th>".ToString());
    strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Description</th>".ToString());
    strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Type</th>".ToString());
    strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Username</th>".ToString());
    strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Name</th>".ToString());
    strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Division</th>".ToString());
    strHTMLContent.Append("<th style='border-bottom:2px solid #003366; '>Submitted Date</th>".ToString());
    strHTMLContent.Append("</tr>".ToString());

    for (int i = 0; i < lv.Items.Count; i++)
    {
        CheckBox chk = lv.Items[i].FindControl("CheckBox2") as CheckBox;
        if (chk.Checked)
        {
            strHTMLContent.Append("<tr>".ToString());
            strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblTitle")).Text + "</td>".ToString());
            strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblDescription")).Text + "</td>".ToString());
            strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblType")).Text + "</td>".ToString());
            strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblUsername")).Text + "</td>".ToString());
            strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblName")).Text + "</td>".ToString());
            strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblDivision")).Text + "</td>".ToString());
            strHTMLContent.Append("<td style='border:1px solid #003366;width: 100px'>" + ((Label)lv.Items[i].FindControl("lblSubmittedDate")).Text + "</td>".ToString());
            strHTMLContent.Append("</tr>".ToString());
        }



    }

    strHTMLContent.Append("</table>".ToString());
    strHTMLContent.Append("<br><br>".ToString());

    HttpContext.Current.Response.Write(strHTMLContent);
    HttpContext.Current.Response.End();
    HttpContext.Current.Response.Flush();
}
于 2012-07-09T10:32:29.697 回答