我正在尝试使用asp在datagridview中使用图像按钮。 我有一个带有一些附件文件的数据集。 加载datagridview时,检查附件文件并添加一个imagebutton以下载该文件。 但是Click事件不会触发..
我正在使用rowdatabound:
protected void GridViewOutputs_RowDataBound(object sender, GridViewRowEventArgs e)
{
int index = Convert.ToInt32(e.Row.RowIndex);
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Diagnostics.Debug.WriteLine(e.Row.Cells[4].Text);
int SelectedActivityID = Convert.ToInt32(e.Row.Cells[8].Text);
List<AttachedFile> attachedFiles = new List<AttachedFile>();
attachedFiles = Check_SelectedIndex_Attachment(SelectedActivityID);
string fileName = "None";
int fileIndex = 1;
foreach (AttachedFile currentFile in attachedFiles)
{
fileName = currentFile.Name;
System.Diagnostics.Debug.WriteLine(fileIndex + " : " + fileName);
//ImageButton imgbtn = (ImageButton)e.Row.FindControl("imgbtn");
ImageButton imgbtn = new ImageButton();
imgbtn.ID = "imgbtn" + fileIndex;
System.Diagnostics.Debug.WriteLine(imgbtn.ID.ToString());
//imgbtn.Command += new CommandEventHandler(imgbtn_Command_Download);
//imgbtn.Click += imgbtn_Click_Download;
if (fileName.Contains("pdf"))
{
imgbtn.ImageUrl = "~/Images/pdf icon.png";
}
else if (fileName.Contains(".doc"))
{
imgbtn.ImageUrl = "~/Images/word icon.png";
}
else if (fileName.Contains(".ppt"))
{
imgbtn.ImageUrl = "~/Images/powerpoint icon.png";
}
else if (fileName.Contains(".xls"))
{
imgbtn.ImageUrl = "~/Images/excel icon.png";
}
else if (fileName.Contains(".vsd"))
{
imgbtn.ImageUrl = "~/Images/visio icon.png";
}
imgbtn.CommandName = fileName;
imgbtn.ToolTip = fileName;
e.Row.Cells[9].Controls.Add(imgbtn);
fileIndex++;
}
}
}
我尝试了rowCommand(Onrowcommand)
protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
//if button is clicked
string commandName = e.CommandName;
System.Diagnostics.Debug.WriteLine(commandName);
if (commandName == "Clicked")
{
//go find the index on the gridview
int selectedIndex = GridViewOutputs.SelectedIndex;
if (int.TryParse(e.CommandArgument.ToString(), out selectedIndex))
{
//do something with database
}
}
if (commandName.Contains("."))
{
try
{
string fileName = commandName;
System.Diagnostics.Debug.WriteLine(fileName);
FileManager fileManager = new FileManager();
AttachedFile file = fileManager.GetAttachedFileWithContentName(fileName);
if (file.Bytes.Length > 0)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = file.ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.BinaryWrite(file.Bytes);
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{ }
}
}
我也尝试了Click事件
private void imgbtn_Click_Download(object sender, ImageClickEventArgs e)
{
ImageButton imgbtn = (ImageButton)(sender);
string commandName = imgbtn.CommandName;
System.Diagnostics.Debug.WriteLine(commandName);
if (commandName.Contains("."))
{
try
{
string fileName = commandName;
System.Diagnostics.Debug.WriteLine(fileName);
FileManager fileManager = new FileManager();
AttachedFile file = fileManager.GetAttachedFileWithContentName(fileName);
if (file.Bytes.Length > 0)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = file.ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.BinaryWrite(file.Bytes);
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{ }
}
}
我也尝试了Command事件
private void imgbtn_Command_Download(object sender, CommandEventArgs e)
{
string commandName = e.CommandName;
System.Diagnostics.Debug.WriteLine(commandName);
if (commandName.Contains("."))
{
try
{
string fileName = e.CommandName;
System.Diagnostics.Debug.WriteLine(fileName);
FileManager fileManager = new FileManager();
AttachedFile file = fileManager.GetAttachedFileWithContentName(fileName);
if (file.Bytes.Length > 0)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = file.ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.BinaryWrite(file.Bytes);
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{ }
}
}
显示图像按钮很麻烦,但是当我单击时,我无法使用。
请帮助我。