首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MOSS 2007,C#,Web部件-使用LinkButtons的日志检索

MOSS 2007,C#,Web部件-使用LinkButtons的日志检索
EN

Stack Overflow用户
提问于 2009-08-11 09:54:05
回答 1查看 1.3K关注 0票数 0

技术: SharePoint/MOSS 2007 - IDE: Visual 2008 -语言: C#

我已经创建了一个SharePoint/MOSS 2007 web部件,该部件显示日志文件列表。日志文件以LinkButtons的形式呈现在屏幕上。LinkButtons位于DataTable中,该DataTable被设置为SPGridView的数据源并绑定到它。然后将此SPGridView对象添加到web部件的重写的"CreateChildControls()“方法中的”控件“中。

我使用下面的"DownloadAssistant“助手类来显示指定的文件。它的"DownloadFile“方法是从每个LinkButton的'.Click‘事件调用的。

代码语言:javascript
复制
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;
        }
    }
}  

在屏幕上单击一个LinkButton后,我会收到预期的下载弹出窗口,然后继续打开第一个日志文件。但是,在打开这个第一个日志文件之后,即在第一个LinkButton单击事件被触发之后,我不能触发任何其他.Click事件--这是因为我需要回发回屏幕。当我单击其他任何一个LinkButtons时,什么都不会发生?

Web部件代码:

代码语言:javascript
复制
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);
        }
    }
}
}
EN

回答 1

Stack Overflow用户

发布于 2009-08-25 15:13:19

我找到了解决办法。我不得不:

  1. 将按钮的客户端单击事件设置为:"exportRequested=true;“
  2. 注册一些JavaScript:有关详细信息,请参阅:

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/55136e4e-e1f7-4a79-9b75-be09cd5594c2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1259406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档