2012-07-09 51 views
0

我使用Ajax TabContiner,並在其中一個選項卡中有一個ListView。此ListView顯示數據庫中特定表內的所有項目。通過該ListView,可以爲ListView中的選定項目創建Word文檔。項目的選擇將通過ListView的第一列中提供的CheckBox。一切運作良好。爲什麼ListView中的計數項目方法不適用於創建Word文檔方法?

現在,我想統計該表中的項目數。我寫的方法,它工作正常,但它使現在創建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(); 
    } 
+0

如果您收到任何錯誤,請在此處發帖 – HatSoft 2012-07-09 08:20:44

+0

無錯。我說Counting Items方法可以讓應用程序打開Word文檔,但沒有數據(或項目)。爲什麼?!! – 2012-07-09 08:36:43

+0

乍一看,你的計數器方法改變了'All_SafetySuggestions_Tab.HeaderText'。在'create_word_table'方法中,有'if'語句由HeaderText決定。如果你的「count」方法破壞了某些東西,我會去看看。 – Smudge202 2012-07-09 08:43:14

回答

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(); 
} 
+0

感謝您的幫助。對此,我真的非常感激。 – 2012-07-09 10:42:14

相關問題