2009-08-11 14 views
0

テクノロジー:SharePointの/ MOSS 2007 - IDE:Visual Studioの2008 - 言語:C#MOSS 2007、C#の、Webパーツ - ログRetrievel LinkBut​​tonsを使用して

私はSharePointの/ MOSS 2007のWebパーツを作成しましたログファイルのリストを表示します。 ログファイルは、LinkBut​​tonとしてレンダリングされます。 LinkBut​​tonは、SPGridViewのデータソースとして設定されバインドされたDataTable内にあります。 このSPGridViewオブジェクトは、Webパーツのオーバーライドされた "CreateChildControls()"メソッド内の 'Controls'に追加されます。

私は以下の "DownloadAssistant"ヘルパークラスを利用して、指定されたファイルを表示しています。 "DownloadFile"メソッドは各LinkBut​​tonの '.Click'イベントから呼び出されます。

using System; 
using System.Web; 

/// <summary> 
/// A class that helps to build download buttons/links on the fly 
/// </summary> 
static public class DownloadAssistant 
{ 
    static public void DownloadFile(string filePath) 
    { 
     try 
     { 
      HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.AddHeader("Content-Disposition", string.Concat("attachment; filename=", filePath)); 
      HttpContext.Current.Response.ContentType = "application/octet-stream"; 
      HttpContext.Current.Response.WriteFile(filePath); 
      HttpContext.Current.Response.Flush(); 
      HttpContext.Current.Response.End(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 
} 

1つのLinkBut​​tonをクリックすると、期待どおりにダウンロードポップアップウィンドウが表示され、最初のログファイルを開くことができます。ただし、この最初のログファイルを開いた後、つまり最初のLinkBut​​tonのクリックイベントが発生した後、他の.Clickイベントをトリガーすることはできません。画面にポストバックする必要があります。他のLinkBut​​tonをクリックしても何も起こりません。

Webパーツコード:

namespace LogRetrievalWebPart 
{ 
    [Guid("fd243ec2-83e3-4bad-af5e-c5c16acbc6dd")] 
    public class LogRetrievalWebPart : System.Web.UI.WebControls.WebParts.WebPart 
    { 
     // Member variables prefixed with "m_" 
     private Label m_InfoLbl; 
     private SPGridView m_GridView; 
     private DataTable m_LogFileDataTable; 
     private DropDownList m_DirectoryDropDown; 


     private const String DROPDOWN_OPTION_DEFAULT = "---"; 
     private const String DROPDOWN_OPTION_TRACE_LOGS = "Trace Logs"; 
     private const String DROPDOWN_OPTION_BATCH_LOGS = "Batch Logs"; 
     private const String DROPDOWN_OPTION_OTHER_LOGS = "Other Logs"; 

     public LogRetrievalWebPart() 
     { 
      this.ExportMode = WebPartExportMode.All; 
     } 

     protected override void CreateChildControls() 
     { 
      EnsureChildControls(); 

      base.CreateChildControls(); 

      m_InfoLbl = new Label(); 
      Label dropDownLbl = new Label(); 
      dropDownLbl.Text = " Please select a directory: "; 
      this.Controls.Add(dropDownLbl); 


      m_DirectoryDropDown = new DropDownList(); 
      m_DirectoryDropDown.Items.Add(DROPDOWN_OPTION_DEFAULT); 
      m_DirectoryDropDown.Items.Add(DROPDOWN_OPTION_TRACE_LOGS); 
      m_DirectoryDropDown.Items.Add(DROPDOWN_OPTION_BATCH_LOGS); 
      m_DirectoryDropDown.Items.Add(DROPDOWN_OPTION_OTHER_LOGS); 
      m_DirectoryDropDown.TextChanged += new EventHandler(directoryDropdown_TextChanged); 
      m_DirectoryDropDown.AutoPostBack = true; 

      m_LogFileDataTable = new DataTable("LogFiles"); 
      AddColums(); 

      m_GridView = new SPGridView(); 
      m_GridView.AutoGenerateColumns = false; 

      BoundField idField = new BoundField(); 
      idField.DataField = "ID"; 
      idField.HeaderText = "ID"; 
      m_GridView.Columns.Add(idField); 

      TemplateField colName = new TemplateField(); 
      colName.HeaderText = "Log File Name"; 
      colName.SortExpression = "LogFileName"; 
      colName.ItemTemplate = new LinkTemplate("LogFileName", "Path"); 
      m_GridView.Columns.Add(colName); 

      this.Controls.Add(m_DirectoryDropDown); 
      this.Controls.Add(m_InfoLbl); 
      this.Controls.Add(m_GridView); 

      this.Load += new EventHandler(LogRetrievalWebPart_Load); 
      this.PreRender += new EventHandler(LogRetrievalWebPart_PreRender); 
     } 

    void LogRetrievalWebPart_Load(object sender, EventArgs e) 
    { 
     EnsureChildControls(); 
    } 

    protected void directoryDropdown_TextChanged(object sender, EventArgs e) 
    { 
     ViewState["LogRetrieval"] = null; 
     String selectedDirectoryName = m_DirectoryDropDown.SelectedItem.Text; 

     if (DROPDOWN_OPTION_TRACE_LOGS.Equals(selectedDirectoryName)) 
     { 
      m_InfoLbl.Text = " *** TRACE Logs: *** "; 
      GetLogFiles("LogFiles/TraceLogs"); 
     } 
     else if (DROPDOWN_OPTION_BATCH_LOGS.Equals(selectedDirectoryName)) 
     { 
      m_InfoLbl.Text = " *** BATCH Logs: *** "; 
      GetLogFiles("PortalExecutables/Logs"); 
     } 
     else if (DROPDOWN_OPTION_OTHER_LOGS.Equals(selectedDirectoryName)) 
     { 
      m_InfoLbl.Text = " *** OTHER Logs: *** "; 
      GetLogFiles("PortalExecutables/GMExecutables"); 
     } 
     else 
     { 
      m_InfoLbl.Text = " *** No Logs to display for this selection!!! *** "; 
     } 

     ViewState["LogRetrieval"] = m_LogFileDataTable; 
     m_GridView.DataSource = m_LogFileDataTable; 
     m_GridView.DataBind(); 
    } 

    private void GetLogFiles(string aSelectedDirectory) 
    { 
     string directoryPath = HttpContext.Current.Server.MapPath(ResolveUrl("/LogFiles/" + aSelectedDirectory)); 

     DirectoryInfo directory = new DirectoryInfo(directoryPath); 
     FileInfo[] files = directory.GetFiles(); 

     int count = 1; 
     foreach (FileInfo fileInfo in files) 
     { 
      string fullFileName = fileInfo.FullName; 
      string fileName = fileInfo.ToString(); 

      AddRow(count, fileName, fullFileName); 
      count++; 
     } 
    } 

    private void AddRow(int id, string logFileName, string fullFileName) 
    { 
     DataRow newRow = m_LogFileDataTable.Rows.Add(); 
     newRow["ID"] = id; 
     newRow["LogFileName"] = logFileName; 
     newRow["Path"] = fullFileName; 
    } 

    private void AddColums() 
    { 
     DataColumn idCol = m_LogFileDataTable.Columns.Add("ID", typeof(Int32)); 
     idCol.Unique = true; 

     m_LogFileDataTable.Columns.Add("LogFileName", typeof(String)); 
     m_LogFileDataTable.Columns.Add("Path", typeof(String)); 
    } 

    public void LogRetrievalWebPart_PreRender(object sender, EventArgs e) 
    { 
     if (this.Page.IsPostBack) 
     { 
      if (ViewState["LogRetrieval"] != null) 
      { 
       m_LogFileDataTable = (DataTable)ViewState["LogRetrieval"]; 
       m_GridView.DataSource = m_LogFileDataTable; 
       m_GridView.DataBind(); 
      } 
     } 
    } 

    public class LinkTemplate : ITemplate 
    { 
     string logFileName; 
     string logFilePath; 

     public LinkTemplate(string fieldName, string path) 
     { 
      logFileName = fieldName; 
      logFilePath = path; 
     } 

     public void InstantiateIn(Control container) 
     { 
      LinkButton link = new LinkButton(); 
      container.Controls.Add(link); 

      link.DataBinding += new EventHandler(link_DataBinding); 
      link.Click += new EventHandler(link_Click); 
     } 

     private void link_DataBinding(Object sender, EventArgs e) 
     { 
      LinkButton link = (LinkButton)sender; 
      DataRowView dataRow = (DataRowView)((SPGridViewRow)link.NamingContainer).DataItem; 

      link.Text = dataRow[logFileName].ToString(); 
      link.CommandArgument = dataRow[logFilePath].ToString(); 
     } 

     private void link_Click(object sender, EventArgs e) 
     { 
      LinkButton link = (LinkButton)sender; 
      DownloadAssistant.DownloadFile(link.CommandArgument); 
     } 
    } 
} 
} 
+0

あなたはクリックハンドラ内でブレークポイントを追加し、それは間違いなくはい二度目 –

+0

にヒットされていないことを確認、私は...それは間違いなく2回目に当たってはいない。 –

+0

あなたは前後のソースを見て、diffをやってみましたか? – TheZenker

答えて

関連する問題