2009-08-11 8 views
0

Technologie: SharePoint/MOSS 2007 - IDE: Visual Studio 2008 - Langue: C#MOSS 2007, C#, Web Part - Connexion Retrievel utilisant linkbuttons

J'ai créé une partie Web SharePoint/MOSS 2007 qui affiche une liste de fichiers journaux. Les fichiers journaux sont affichés à l'écran en tant que LinkButtons. Les LinkButtons se trouvent dans un DataTable défini comme source de données d'un SPGridView et lié à celui-ci. Cet objet SPGridView est ensuite ajouté aux contrôles dans la méthode "CreateChildControls()" du composant WebPart substituée. J'utilise la classe d'assistance "DownloadAssistant" suivante pour afficher un fichier spécifié. Sa méthode "DownloadFile" est appelée à partir de l'événement '.Click' de chaque LinkButton.

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; 
     } 
    } 
} 

Après un LinkButton est cliqué sur l'écran, je reçois le téléchargement fenêtre pop-up comme prévu et peut aller de l'avant et d'ouvrir le premier fichier journal. Cependant, après l'ouverture de ce premier fichier journal, c'est-à-dire après le déclenchement du premier événement Click LinkButton, je ne peux déclencher aucun autre événement .Click - c'est comme cela que j'ai besoin de revenir à l'écran. Lorsque je clique sur l'un des autres LinkButtons rien ne se passe?

Le Web Code partie:

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

Avez-vous ajouté un point de rupture dans le gestionnaire de clic et vérifiez qu'il est certainement pas d'être frappé sur la deuxième fois –

+0

Oui , J'ai ... ce n'est définitivement pas touché la deuxième fois. –

+0

avez-vous essayé de regarder la source avant et après et faire un diff? – TheZenker

Répondre

Questions connexes