动态添加的链接按钮单击事件不会执行

时间:2012-03-22 07:05:31

标签: c# asp.net .net

我已使用以下代码在网格视图中动态添加了一些链接按钮:

protected void gvTicketStatus_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string compositeFiles = e.Row.Cells[3].Text;

            // split the string into individual files using delemeter "?"
            string[] fileSet = compositeFiles.Split('?');

            e.Row.Cells[3].Text = "";

            foreach (string str in fileSet)
            {
                if (str != null)
                {
                    // add a link button to the cell of the data grid.                                             

                    LinkButton lb = new LinkButton();
                    lb.Text = "Download File";
                    lb.ID = str; // str is file URL
                    lb.Click += new EventHandler(lbStatus_click);

                    e.Row.Cells[3].Controls.Add(lb);

                }
            }

        }
    }

在我的事件处理程序中,我已从ID中读取URL并将文件作为八位字节流下载。

private void lbStatus_click(object sender, EventArgs e)
    {
        string fileName = ((Control)sender).ID;
        FileInfo file = new FileInfo(fileName);

        if (fileName != string.Empty && file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-disposition", "attachment; filename=" + fileName.Substring(fileName.LastIndexOf("\\") + 1));
            Response.AddHeader("content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Close();
        } 

    }

链接显示在网页中很好,但问题是当我点击它们时,页面只会刷新并且没有任何反应。事件处理程序代码永远不会被执行。

此问题是否与页面的回发有关?如果是,那么我该如何解决呢?

2 个答案:

答案 0 :(得分:0)

您需要接受网格视图的命令事件,并将所需的任何信息添加到CommandArgs属性中。

编辑:添加了示例

public void Page_Load(object sender, EventArgs e ){
    gvTicketStatus.RowCommand += new EventHandler(RowCommand);
}

protected void gvTicketStatus_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string compositeFiles = e.Row.Cells[3].Text;

            // split the string into individual files using delemeter "?"
            string[] fileSet = compositeFiles.Split('?');

            e.Row.Cells[3].Text = "";

            foreach (string str in fileSet)
            {
                if (str != null)
                {
                    // add a link button to the cell of the data grid.                                             

                    LinkButton lb = new LinkButton();
                    lb.Text = "Download File";
                    lb.CommandName = "download"; //this is useful if you need to add more links with different commands.
                    lb.CommandArgument = str;// str is file URL
                    e.Row.Cells[3].Controls.Add(lb);
                }
            }

        }
    }

在我的事件处理程序中,我已从ID中读取URL并将文件作为八位字节流下载。

private void RowCommand(object sender, GridViewCommandEventArgs e)
{
        string fileName = e.CommandArgument;
        FileInfo file = new FileInfo(fileName);

        if (fileName != string.Empty && file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-disposition", "attachment; filename=" + fileName.Substring(fileName.LastIndexOf("\\") + 1));
            Response.AddHeader("content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.Close();
        } 

    }

有关详情,请参阅http://msdn.microsoft.com/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.80).aspx

答案 1 :(得分:0)

在动态添加控件之前,我已多次看到此行为。确保绑定datagrid的代码(后者执行gvTicketStatus_RowDataBound事件)在每个页面回发后运行。这需要发生,以便linkbutton控件在回发后保持其click事件。