0

私はAjaxTabContinerを使用しており、タブの1つにListViewがあります。このListViewは、データベース内の特定のテーブル内のすべてのアイテムを表示します。そのListViewには、ListViewで選択したアイテムのWord文書を作成する方法があります。アイテムの選択は、ListViewの最初の列にあるチェックボックスを介して行われます。すべてがうまくいきます。

ここで、そのテーブルのアイテム数を数えたいと思います。私はメソッドを書きました、それはうまくいきます、しかしそれはワード文書を作成するメソッドを今うまく機能させます。たとえば、その特定のタブで、タブ内のアイテムの数を表示したいとします。選択したアイテムを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 に答える