C# 不执行动态添加的链接按钮单击事件
我在网格视图中动态添加了几个链接按钮,代码如下:C# 不执行动态添加的链接按钮单击事件,c#,asp.net,.net,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;
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();
}
}
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();
}
}
链接僵尸会很好地出现在网页中,但问题是当我点击它们时,页面会被刷新,什么也没有发生。事件处理程序代码永远不会执行
这个问题与页面的回发有关吗?如果是,那么我如何解决它?您需要使用网格视图的命令事件,并向CommandArgs属性添加所需的任何信息 编辑:添加示例 在我的事件处理程序中,我已从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();
}
}
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();
}
}
有关详细信息,请阅读我以前在动态添加控件时多次看到这种行为。确保绑定datagrid的代码(反过来执行gvTicketStatus_RowDataBound事件)在每次页面回发之后运行。这需要发生,以便linkbutton控件在回发后保持其单击事件。Hi ivowiblo,请详细说明一下好吗?感谢您的回复。运行时可能会捕获一些异常。您是否使用了try,catch?检查代码中是否存在异常。您还可以将访问说明符更改为protected,private也可以,但最好是protected,以防您需要将此事件用于另一个按钮。